在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>
謝謝觀看!