文章目录
将 Spring 与 MyBatis 进行整合,主要解决的问题就是将 SqlSessionFactory 对象交由 Spring 来管理。所以核心就是将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合。
详细步骤:
在学习了MyBatis与Spring的基础上,创建新的项目以查询管理员为例,将Spring与MyBatis进行整合完成操作。
1.创建数据库管理员表
2.新建一个JavaEE项目
3.加入相关依赖的jar包
在pom.xml配置文件中,导入以下jar包:
<dependencies>
<!--mybatis所需依赖-->
<!--mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.2</version>
</dependency>
<!--mysql-connector-java-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- 阿里数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<!--junit 单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>provided</scope>
</dependency>
<!--Spring所需依赖-->
<!-- spring-context -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!--spring-aop-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
<!-- spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.2.RELEASE</version>
</dependency>
</dependencies>
4.创建相应的包,类和接口
如图:
Admin模型类:
package com.ffyc.ssm.model;
import org.springframework.stereotype.Component;
@Component(value = "admin")
public class Admin {
private Integer id;
private String account;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "Admin{" +
"id=" + id +
", account='" + account + '\'' +
", password='" + password + '\'' +
'}';
}
}
LoginService类:
package com.ffyc.ssm.service;
import com.ffyc.ssm.dao.LoginDao;
import com.ffyc.ssm.model.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service(value = "loginService")
@Transactional
public class LoginService {
@Autowired
LoginDao loginDao;//注入dao层的对象 接口的代理对象
public Admin login(Admin admin) {
Admin a = loginDao.login(admin);
return a;
}
}
LoginDao接口:
package com.ffyc.ssm.dao;
import com.ffyc.ssm.model.Admin;
import org.springframework.stereotype.Repository;
@Repository(value = "loginDao")
public interface LoginDao {
Admin login(Admin admin);
}
5.创建mapper映射文件
在main包中的resources包中创建mappers包,专门放mapper映射文件;
如图:
<?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.ffyc.ssm.dao.LoginDao">
<select id="login" resultType="com.ffyc.ssm.model.Admin">
select * from admin where account=#{account} and password=#{password}
</select>
</mapper>
6.创建MyBatis配置文件
在main包中的resources包中创建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">
<!--mybatis核心配置文件-->
<configuration>
<!--常用设置-->
<settings>
<!--启用日志功能,在运行时,可以将实际执行的sql细节打印到控制台-->
<setting name="logImpl" value="STDOUT_LOGGING"/>
<!--开启驼峰命名映射-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!-- <setting name="autoMappingBehavior" value="FULL"/>-->
<!--全局开启二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!--配置类型简称-->
<typeAliases>
<package name="com.ffyc.mybatispro.model"/>
</typeAliases>
</configuration>
7.创建Spring配置文件
在main包中的resources包中创建spring.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.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.xsd">
<!--开启注解扫描-->
<context:component-scan base-package="com.ffyc.ssm"></context:component-scan>
<!--开启自动代理-->
<aop:aspectj-autoproxy/>
</beans>
8.配置数据库连接以及事务管理配置
①在main包中的resources包中创建config.properties文件,用来存储数据库连接信息;
classDriverName=com.mysql.cj.jdbc.Driver
url=(数据库连接地址)
uname=(用户名)
pwd=(密码)
在main包中的resources包中创建db.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--jdbc功能的配置-->
<!--加载config.properties文件-->
<context:property-placeholder location="classpath:config.properties"></context:property-placeholder>
<!--spring统一管理数据库连接对象-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${classDriverName}"></property>
<property name="url" value="${url}"></property>
<property name="username" value="${uname}"></property>
<property name="password" value="${pwd}"></property>
<property name="initialSize" value="10"></property>
<property name="maxActive" value="20"></property>
</bean>
<!-- 配置 spring 事务管理类, 并注入数据源 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启注解事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
9.集成MyBatis配置
此步骤是Spring集成MyBatis的关键:
9.1 导入jar包(mybatis-spring)
<!--spring集成mybatis插件包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.1</version>
</dependency>
9.2 创建配置文件
在main包中的resources包中创建spring-mybatis.xml:
记得将数据库配置文件db.xml导入spring-mybatis.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.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.xsd">
<!--导入数据库连接以及事务管理配置-->
<import resource="classpath:db.xml"></import>
<!--让spring框架生成sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--注入数据源-->
<property name="dataSource" ref="dataSource"></property>
<!--配置mybatis配置文件-->
<property name="configLocation" value="mybatis-config.xml"></property>
<!--扫描sql映射文件-->
<property name="mapperLocations" value="mappers/*Mapper.xml">
</property>
</bean>
<!--让spring框架生成接口代理对象-->
<bean id="mapperFactory" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ffyc.ssm.dao"></property>
<!--value="sqlSessionFactory"与 id="sqlSessionFactory"相同-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory">
</property>
</bean>
</beans>
9.3 在spring.xml核心配置文件中导入spring-mybatis.xml
以上所有操作结束后,该项目的目录为:
10.测试
在保证以上操作无误后,可以编写一个测试类:
执行后的结果为:
测试无异常说明Spring集成MyBatis成功,且查询管理员操作正确。