在spring中,FactoryBean用于构造复杂的对象,如Connection,SqlSession等,本文以构建SqlSession为例
首先准备如下代码:
数据库配置文件:databases.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?useSSL=true
username=root
password=xxxxxxxx
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 60000毫秒/1000等于60秒 -->
maxWait=5000
一个Entity:StudentInfo.java
package org.example.entity;
import java.sql.Date;
public class StudentInfo {
private String student_id;
private String student_name;
private String student_major;
private String student_gender;
private java.sql.Date student_birthday;
public StudentInfo() {
}
public StudentInfo(String student_id, String student_name, String student_major, String student_gender, Date student_birthday) {
this.student_id = student_id;
this.student_name = student_name;
this.student_major = student_major;
this.student_gender = student_gender;
this.student_birthday = student_birthday;
}
@Override
public String toString() {
return "StudentInfo{" +
"student_id='" + student_id + '\'' +
", student_name='" + student_name + '\'' +
", student_major='" + student_major + '\'' +
", student_gender='" + student_gender + '\'' +
", student_birthday=" + student_birthday +
'}';
}
public String getStudent_id() {
return student_id;
}
public void setStudent_id(String student_id) {
this.student_id = student_id;
}
public String getStudent_name() {
return student_name;
}
public void setStudent_name(String student_name) {
this.student_name = student_name;
}
public String getStudent_major() {
return student_major;
}
public void setStudent_major(String student_major) {
this.student_major = student_major;
}
public String getStudent_gender() {
return student_gender;
}
public void setStudent_gender(String student_gender) {
this.student_gender = student_gender;
}
public Date getStudent_birthday() {
return student_birthday;
}
public void setStudent_birthday(Date student_birthday) {
this.student_birthday = student_birthday;
}
}
StduentInfo对应的dao层接口:StudentDao.java
package org.example.dao;
import org.example.entity.StudentInfo;
import java.util.List;
public interface StudentDao {
/**
* 根据id查询用户信息
*
* @param id
* @return
*/
public StudentInfo queryStudentById(String id);
/**
* 查询所有用户信息
*
* @return
*/
public List<StudentInfo> queryStudentAll();
/**
* 新增用户
*
* @param studentInfo
*/
public void insertUser(StudentInfo studentInfo);
/**
* 更新用户信息
*
* @param studentInfo
*/
public void updateStudent(StudentInfo studentInfo);
/**
* 根据id删除用户信息
*
* @param id
*/
public void deleteStudent(String id);
}
StudentDao.java的mybatis的映射:StudentDaoMapper.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="org.example.dao.StudentDao">
<select id="queryStudentById" resultType="org.example.entity.StudentInfo">
SELECT *
FROM test.student_info
WHERE student_id = #{student_id};
</select>
<select id="queryStudentAll" resultType="org.example.entity.StudentInfo">
SELECT *
FROM test.student_info;
</select>
<insert id="insertUser" parameterType="org.example.entity.StudentInfo">
INSERT INTO test.student_info (student_id, student_name, student_major, student_gender, student_birthday)
VALUES (#{student_id}, #{student_name}, #{student_major}, #{student_gender}, #{student_birthday});
</insert>
<update id="updateStudent" parameterType="org.example.entity.StudentInfo">
UPDATE test.student_info
SET student_id=#{student_id},
student_name=#{student_name},
<if test="student_major!=null">student_major=#{student_major},</if>
student_gender=#{student_gender},
student_birthday=#{student_birthday}
WHERE student_id = #{student_id};
</update>
<delete id="deleteStudent">
DELETE
FROM test.student_info
WHERE student_id = #{student_id};
</delete>
</mapper>
最后就是MyBatis的配置文件:mybatis-config.xml(将mapper添加其中)
<?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="mybatis/database.properties"/>
<!-- 环境,可以配置多个,default:指定采用哪个环境 -->
<environments default="test">
<!-- id:唯一标识 -->
<environment id="test">
<!-- 事务管理器,JDBC类型的事务管理器 -->
<transactionManager type="JDBC"/>
<!-- 数据源,池类型的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="${driverClassName}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<!-- Mapper配置-->
<mappers>
<mapper resource="mybatis/mapper/StudentDaoMapper.xml"/>
</mappers>
</configuration>
经过以上准备,我们现在就可以通过mybatis-config.xml来获取SqlSessionFactory对象了
方法一:实现FactoryBean接口
新建SqlSessionFactoryBean.java ,使其实现FactoryBean接口
package org.example.factoryBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.FactoryBean;
import java.io.InputStream;
/**
* 生产SqlSessionFactory对象
*/
public class SqlSessionFactoryBean implements FactoryBean<SqlSessionFactory> {
//完成复杂对象的生产,并返回该对象
@Override
public SqlSessionFactory getObject() throws Exception {
InputStream inputStream= Resources.getResourceAsStream("mybatis/mybatis-config.xml");
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
//返回复杂对象的类对象
@Override
public Class<?> getObjectType() {
return SqlSessionFactory.class;
}
//控制复杂对象的创建模式,(返回true为单例模式,返回false为多例(原型)模式)
@Override
public boolean isSingleton() {
return true;
}
}
在applicationContext.xml中配置如下<bean>
即可在工厂中获取该对象
方法二:静态工厂方法
修改对象SqlSessionFactoryBean.java
package org.example.factoryBean;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
/**
* 生产SqlSessionFactory对象
*/
public class SqlSessionFactoryBean {
//完成复杂对象的生产,并返回该对象
public static SqlSessionFactory getObject() throws Exception {
InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
return sqlSessionFactory;
}
}
在applicationContext.xml中配置如下<bean>
谢谢观看!