MyBatis-01-Environmental Construction

Now I still learn MyBatis, and record the learning process here.

The first thing to bring is the environment construction of MyBatis.

Development tools: MyEclipse8.0

Database: MySql5.6

 

1. Create a new Java project in MyEclipse, named here: mybatis. At the same time, create two new source packages: config (for storing configuration files) and test (for testing). Create a new lib folder to store the required jar packages. In this example, junit is used for testing, so the library of junit4 needs to be added. The added format is as follows:



 

2. Add the jar package (mybatisjar package and database driver package) to the lib folder:



 3. Add the log configuration file:

        Since mybatis uses log4j for log processing, the log4j.properties configuration file needs to be added.

     

 

      The contents of the log4j.properties file are as follows:

# log4j log configuration file
# Global logging configuration
log4j.rootLogger=DEBUG, stdout


# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

Fourth, add the database configuration file:

     

 The contents of the db.properties file are as follows:

# Configure database related parameters

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=111111

 

 5. Create a mapper folder to store the mapping files of mybatis:

 

 

 6. Create mybatis global configuration file: mybatis-config.xml



 

mybatis-config.xml file content:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
    <!-- 加载数据库配置文件 -->
    <properties resource="db.properties"/>

    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!-- 配置数据源信息 -->
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>
   
    <!-- 加载映射文件 -->
    <mappers>
        <mapper resource="mapper/User.xml"/>
    </mappers>
</configuration>

 

I have included a map file in the mappers tag here. This will be explained in a later article.

 

This is the end of the environment construction of mybatis.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326445253&siteId=291194637