SqlSessionFactory是创建SqlSession的工厂,但是创建过程中需要反复加载全局配置文件,这一点是十分耗时的,我们希望SqlSessionFactory对于一个数据库而言只有一个实例。单例模式的好处是可以重复的使用这个唯一的对象
创建方式
对构造函数进行私有化
对类加锁, 防止多线程环境造成对象不唯一
代码如下, openSqlSession返回一个SqlSession
public class SqlSessionFactoryUtil {
private static SqlSessionFactory sqlSessionFactory;
private static final Class CLASS_LOCK = SqlSessionFactoryUtil.class;
/**
* 私有化构造
*/
private SqlSessionFactoryUtil(){
}
/*
* 单实例对象
*/
public static SqlSessionFactory initSqlSessionFactory(){
String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
}catch (IOException e){
e.printStackTrace();
}
synchronized (CLASS_LOCK){
if(sqlSessionFactory == null){
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
}
return sqlSessionFactory;
}
public static SqlSession openSqlSession(){
if (sqlSessionFactory == null){
initSqlSessionFactory();
}
return sqlSessionFactory.openSession();
}
}
pojo类
public class Student {
private Integer stuId;
private String stuName;
private Integer stuAge;
private String stuMajor;
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Integer getStuAge() {
return stuAge;
}
public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
}
public String getStuMajor() {
return stuMajor;
}
public void setStuMajor(String stuMajor) {
this.stuMajor = stuMajor;
}
@Override
public String toString() {
return "Student{" +
"stuId=" + stuId +
", stuName='" + stuName + '\'' +
", stuAge=" + stuAge +
", stuMajor='" + stuMajor + '\'' +
'}';
}
}
创建接口和对应的Mapper.xml
public interface StudentMapper {
public Student getStudent(Integer stuid);
public Integer deleteStudent(Integer stuid);
public Integer insertStudent(Student sut);
}
StudentMapper.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.cyh.mapper.StudentMapper">
<select id="getStudent" parameterType="java.lang.Integer" resultType="com.cyh.pojo.Student">
select * from student where stuId = #{stuId}
</select>
<insert id="insertStudent" parameterType="com.cyh.pojo.Student">
insert into student(stuName, stuAge, stuMajor) values(#{stuName},#{stuAge},#{stuMajor})
</insert>
<delete id="deleteStudent" parameterType="java.lang.Integer">
delete FROM student WHERE stuid = #{stuid}
</delete>
</mapper>
测试
测试方法通过SqlSessionFactoryUtil创建sqlSession, 在得到指定的Mapper进行操作
@Test
public void test() throws IOException {
SqlSession sqlSession = null;
try {
sqlSession = SqlSessionFactoryUtil.openSqlSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student stu = new Student();
stu.setStuName("王五");
stu.setStuAge(12);
stu.setStuMajor("软件");
studentMapper.insertStudent(stu);
studentMapper.deleteStudent(1);
sqlSession.commit();
} catch (Exception e){
e.printStackTrace();
}finally {
if (sqlSession != null){
sqlSession.close();
}
}
}
添加log4j
在maven中添加依赖
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
在resources目录下新建log4j.properties内容如下
log4j.rootLogger=DEBUG, Console
#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
执行测试类方法控制台打印如下:
2018-03-13 09:35:48,595 [main] DEBUG [com.cyh.mapper.StudentMapper.insertStudent] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@5de164e4]
2018-03-13 09:35:48,597 [main] DEBUG [com.cyh.mapper.StudentMapper.insertStudent] - ==> Preparing: insert into student(stuName, stuAge, stuMajor) values(?,?,?)
2018-03-13 09:35:48,637 [main] DEBUG [com.cyh.mapper.StudentMapper.insertStudent] - ==> Parameters: 王五(String), 12(Integer), 软件(String)
2018-03-13 09:35:48,642 [main] DEBUG [com.cyh.mapper.StudentMapper.deleteStudent] - ooo Using Connection [com.mysql.jdbc.JDBC4Connection@5de164e4]
2018-03-13 09:35:48,642 [main] DEBUG [com.cyh.mapper.StudentMapper.deleteStudent] - ==> Preparing: delete FROM student WHERE stuid = ?
2018-03-13 09:35:48,642 [main] DEBUG [com.cyh.mapper.StudentMapper.deleteStudent] - ==> Parameters: 1(Integer)