本文大致包含了以下几方面的讲解:
通过 StudentMapper.xml 将类和表建立起映射关系;
将 studentService 及属性 studentMapper 注入 Spring IoC 容器,StudentService 接口及实现类 StudentServiceImpl 的实现过程;
通过 Spring 产生 MyBatis 最终操作需要的动态 Mapper 对象(StudentMapper 对象);
在 Spring IoC 容器中如何创建 MyBatis 的核心类 SqlSessionFactory,并将 Spring 配置的 SqlSessionFactory 对象交给Mapper(Dao 层)。
在 Java EE 项目开发中访问数据库是 SUN 公司提出的 JDBC 规范,但是因为它需要的冗余代码比较多,加上流程和资源难以控制,所以使用 JDBC 开发的模式很快就走到了尽头。SUN 公司早年推出的 EJB,虽然能够支持持久化,但是因为配置极为烦琐,所以很快就被新兴的 Hibernate 框架取代。
对于全映射框架 Hibernate,在以管理系统为主的时代,它的模型十分有利于公司业务的分析与理解。但是在移动互联网时代,更关注大数据和大并发下的性能问题。全表映射规则下的 Hibernate 无法满足 SQL 优化和互联网灵活多变的业务。MyBatis 已经成为移动互联网时代的主流持久层框架。MyBatis 是一个不屏蔽 SQL 且提供动态 SQL、接口式编程和简易 SQL 绑定 POJO 的半自动化框架,而且非常容易定制 SQL,以提高网站性能。 Spring 整合 MyBatis 主要开发步骤包括: Spring 整合 MyBatis,需要通过 Spring 管理 MyBatis 的 SqlSessionFactory,因此产生 SqlSessionFactory 所需要的数据库信息,需要放入 Spring 配置文件 applicationContext.xml 中。如下例所示:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
default-autowire="byName"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 加载 db.properties -->
<bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:db.properties" />
</bean>
<!-- 配置数据库信息 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${driverClassName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${username}"></property>
<property name="password" value="${password}"></property>
<property name="maxActive" value="${maxActive}"></property>
<property name="maxIdle" value="${maxIdle}"></property>
</bean>
<!-- 在 Spring IoC 容器中创建 MyBatis 的核心类 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载 MyBatis 配置文件 -->
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
<bean id="studentMapper" class="org.bjwykj.dao.impl.StudentDaoImpl">
<!-- 将 Spring 配置的 sqlSessionFactory 对象交给 Mapper(Dao 层) -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="org.bjwykj.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
</beans>
单独把数据库配置信息放到 db.properties 配置文件中, 实例使用的是 Oracle 数据库,加载 db.properties 配置文件,如下例所示:
driverClassName=oracle.jdbc.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:ORCL
username=BJWYKJ
password=123456
maxActive=10
maxIdle=6
在 Spring IoC 容器中创建 MyBatis 的核心类 SqlSessionFactory,并加载 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>
<!-- 加载映射文件 StudentMapper.xml -->
<mappers>
<mapper resource="org/bjwykj/mapper/StudentMapper.xml"/>
</mappers>
</configuration>
通过 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="org.bjwykj.mapper.StudentMapper" >
<insert id="addStudent" parameterType="org.bjwykj.entity.Student" >
INSERT INTO student
(
id,
stuno,
stuname
)
VALUES (
#{id},
#{stuno},
#{stuname}
)
</insert>
</mapper>
建立 StudentMapper.xml 中涉及的实体类和数据库表,如下例所示:
package org.bjwykj.entity;
public class Student {
private String id;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
private int stuno;
public int getStuno() {
return stuno;
}
public void setStuno(int stuno) {
this.stuno = stuno;
}
private String stuname;
public String getStuname() {
return stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
}
CREATE TABLE “STUDENT” (
“ID” VARCHAR2(40 BYTE) NOT NULL,
“STUNO” NUMBER NOT NULL,
“STUNAME” VARCHAR2(60 BYTE)
)
将 studentService 及属性 studentMapper 注入 Spring IoC 容器,StudentService.java 接口及实现类 StudentServiceImpl.java,如下例所示:
package org.bjwykj.service;
import org.bjwykj.entity.Student;
public interface StudentService {
void addStudent(Student student);
void deleteStudentByID(String id);
}
package org.bjwykj.service.impl;
import org.bjwykj.dao.impl.StudentDaoImpl;
import org.bjwykj.mapper.StudentMapper;
import org.bjwykj.service.StudentService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.bjwykj.entity.Student;
public class StudentServiceImpl implements StudentService{
private StudentMapper studentMapper;
//将 studentMapper 注入 Spring IoC 容器中
public void setStudentMapper(StudentMapper studentMapper) {
this.studentMapper = studentMapper;
}
public void addStudent(Student student) {
studentMapper.addStudent(student);
}
public void deleteStudentByID(String id) {
System.out.println("delete method");
}
}
使用 Spring 整合 MyBatis 开发程序,通过 Spring 产生 MyBatis 最终操作需要的动态 Mapper 对象(StudentMapper 对象)。在 Spring IoC 容器中创建 MyBatis 的核心类 SqlSessionFactory,如下例所示:
<!-- 在 Spring IoC 容器中创建 MyBatis 的核心类 SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 加载 MyBatis 配置文件 -->
<property name="configLocation" value="classpath:config.xml"></property>
</bean>
将 Spring 配置的 sqlSessionFactory 对象交给 Mapper(Dao 层),如下例所示:
<bean id="studentMapper" class="org.bjwykj.dao.impl.StudentDaoImpl">
<!-- 将 Spring 配置的 sqlSessionFactory 对象交给 Mapper(Dao 层) -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="studentService" class="org.bjwykj.service.impl.StudentServiceImpl">
<property name="studentMapper" ref="studentMapper"></property>
</bean>
package org.bjwykj.mapper;
import org.bjwykj.entity.Student;
public interface StudentMapper {
public void addStudent(Student student);
}
Spring 产生 MyBatis 最终操作需要的动态 Mapper 对象的方法,通过 Dao 层实现类继承 SqlSessionDaoSupport 类,SqlSessionDaoSupport 类提供了属性 SqlSession,如下例所示:
package org.bjwykj.dao.impl;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.bjwykj.entity.Student;
import org.bjwykj.mapper.StudentMapper;
import org.mybatis.spring.support.SqlSessionDaoSupport;
//@Component("studentDao")
//@Repository
//@Service
//@Controller
public class StudentDaoImpl extends SqlSessionDaoSupport implements StudentMapper{
@Override
public void addStudent(Student student) {
SqlSession session = super.getSqlSession();
StudentMapper studentdao = session.getMapper(StudentMapper.class);
studentdao.addStudent(student);
System.out.println("add student");
}
}
Spring 整合 MyBatis,需要的 jar 包:mybatis-spring.jar、spring-tx.jar、spring-jdbc.jar、spring-expression.jar、spring-context-support.jar、spring-core.jar、spring-context.jar、spring-beans.jar、spring-aop.jar、spring-web.jar、commons-logging.jar、commons-dbcp.jar、ojdbc.jar、mybatis.jar、log4j.jar、commons-pool.jar 通过 Test 类验证 Spring 整合 MyBatis,如下例所示:
package org.bjwykj.test;
import org.bjwykj.entity.Student;
import org.bjwykj.service.StudentService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
//spring 上下文对象 context
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=(StudentService)context.getBean("studentService");
Student student = new Student();
student.setId("20210112001");
student.setStuname("zhangming");
student.setStuno(101);
studentService.addStudent(student);
}
}