Mybatis配置
一、poox.xml文件(依赖)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>mybatis</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
</project>
二、log4j.properties 文件 日志配置
log4j.rootLogger=DEBUG, Console
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apache=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
三、mybatis-config.xml文件 (主配置文件)
<?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="jdbc.properties"/>
<typeAliases>
<package name="com.mybatis.bean"/>
</typeAliases>
<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>
<package name="com.mybatis.mapper"/>
</mappers>
</configuration>
四、配置jdbc.properties 文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatisdb?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456
五、配置Mybatis中 生产sqlssion的对象工具类
public class MyBatisUtil {
private static SqlSessionFactory sessionFactory = null;
private static ThreadLocal<SqlSession> threadlocal = new ThreadLocal<SqlSession>();
static {
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sessionFactory = new SqlSessionFactoryBuilder().build(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSessionFactory getSessionFactory() {
return sessionFactory;
}
private static void newSession() {
SqlSession session = sessionFactory.openSession();
threadlocal.set(session);
}
public static SqlSession getSession(){
SqlSession session = threadlocal.get();
if(session==null){
newSession();
session = threadlocal.get();
}
return session;
}
public static void closeSession(){
SqlSession session = threadlocal.get();
if(session!=null){
session.close();
threadlocal.set(null);
}
}
}
六、配置mapper.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mybatis.mapper.UserMapper">
<select id="queryUsers" resultType="com.mybatis.bean.Users">
select * from users
</select>
<select id="queryByPage" resultType="Users">
select * from users limit #{start} ,#{end}
</select>
</mapper>
七、测试
public class MybatisTest {
SqlSession session=null;
UserMapper um=null;
@Before
public void init(){
session = MyBatisUtil.getSession();
um = session.getMapper(UserMapper.class);
}
@Test
public void testQuery(){
List<Users> list = um.queryUsers();
for (Users user : list) {
System.out.println(user);
}
}
@After
public void destory(){
MyBatisUtil.closeSession();
}
}