Spring和Mybatis都是框架,那么两者联合使用的话究竟是Mybatis接管Spring呢还是Spring接管Mybatis呢?
Mybatis框架是一个持久层的ORM框架,而Spring是一个综合性的框架,所以两者的整合就是将Spring框架接管Mybatis中的SessionFactory工厂的创建,同时再通过读取mapper配置内容创建dao的代理实现类,并把它们都存入IOC容器。
Spring和Mybatis都有独立的配置文件,整合时我们有两种选择。
- 第一种是保留两个框架的配置文件
- 第二种是只保留Spring配置文件,把Mybatis相关的配置都写在Spring的配置文件中
相比较来说,第二种方式更加简洁明了。
1、.pom依赖文件
<dependencies>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>5.1.9.RELEASEversion>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatisartifactId>
<version>3.5.2version>
dependency>
<dependency>
<groupId>org.mybatisgroupId>
<artifactId>mybatis-springartifactId>
<version>2.0.1version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.20version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.47version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
dependencies>
2、pojo类
/**
* 账户的实体类
*/
public class Account {
private Integer id;
private String name;
private Double money;
//set,get等方法省略
}
3、持久层接口
/**
* 账户的持久层接口
*/
public interface AccountDao {
/**
* 更新账户
*/
void update(Account account);
/**
* 根据名称查询账户
*/
Account findByName(String name);
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Mybatis配置文件,不做详细介绍
4、SQL映射文件
<?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.hou.dao.AccountDao">
<!--配置根据名称查询-->
<select id="findByName" resultType="account" parameterType="string">
select * from account where name = #{name}
</select>
<!--配置更新-->
<update id="update" parameterType="account">
update account set name=#{name},money=#{money} where id=#{id}
</update>
</mapper>
5、Mybatis核心配置文件
<configuration>
<properties resource="jdbc.properties">properties>
<typeAliases>
<package name="com.spring.pojo">package>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
dataSource>
environment>
environments>
<mappers>
<package name="com.spring.dao">package>
mappers>
configuration>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Spring框架搭建
6、业务层接口及实现类
/**
* 账户的业务层接口
*/
public interface AccountService {
/**
* 保存账户
*/
void update(Account account);
/**
* 根据名称查询账户
*/
Account findByName(String name);
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public Account findByName(String name) {
return accountDao.findByName(name);
}
}
7、Spring核心配置文件——配置Service层bean对象
<bean id="accountService" class="com.hou.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao">property>
bean>
8、Spring核心配置文件——配置dao层bean对象
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource">property>
<property name="typeAliasesPackage" value="com.hou.pojo">property>
bean>
<bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hou.dao">property>
bean>
**9、测试
public class TestSpring {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService = ac.getBean(AccountService.class);
Account account = accountService.findByName("热巴");
System.out.println("account = " + account);
}
}
1、实体类
/**
* 账户的实体类
*/
public class Account {
private Integer id;
private String name;
private Double money;
//省略set,get,toString等方法
}
2、业务层接口
/**
* 账户的业务层接口
*/
public interface AccountService {
/**
* 保存
*/
void save(Account account);
/**
* 根据id删除
*/
void delete(Integer id);
/**
* 更新账户
*/
void update(Account account);
/**
* 根据id查询
*/
Account findById(Integer id);
/**
* 根据名称查询账户
*/
Account findByName(String name);
/**
* 查询所有
*/
List<Account> findAll();
}
3、业务层接口实现类
/**
* 账户业务接口实现类
*/
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao) {
this.accountDao = accountDao;
}
@Override
public void save(Account account) {
accountDao.save(account);
}
@Override
public void delete(Integer id) {
accountDao.delete(id);
}
@Override
public void update(Account account) {
accountDao.update(account);
}
@Override
public Account findById(Integer id) {
return accountDao.findById(id);
}
@Override
public Account findByName(String name) {
return accountDao.findByName(name);
}
@Override
public List<Account> findAll() {
return accountDao.findAll();
}
}
4、持久层接口
/**
* 账户持久层接口
*/
public interface AccountDao {
/**
* 保存
*/
void save(Account account);
/**
* 根据id删除
*/
void delete(Integer id);
/**
* 更新账户
*/
void update(Account account);
/**
* 根据id查询
*/
Account findById(Integer id);
/**
* 根据名称查询账户
*/
Account findByName(String name);
/**
* 查询所有
*/
List<Account> findAll();
}
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Mybatis→SQL映射文件
5、SQL映射配置
<mapper namespace="com.hou.dao.AccountDao">
<insert id="save" parameterType="account">
insert into account values(#{id},#{name},#{money})
insert>
<delete id="delete" parameterType="int" >
delete from account where id=#{id}
delete>
<update id="update" parameterType="account">
update account set name=#{name},money=#{money} where id=#{id}
update>
<select id="findById" parameterType="int" resultType="account">
select * from account where id=#{id}
select>
<select id="findByName" parameterType="string" resultType="account">
select * from account where name=#{name}
select>
<select id="findAll" resultType="account">
select * from account
select>
mapper>
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓Spring与Mybatis整合配置
6、Mybatis配置
<context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}">property>
<property name="url" value="${jdbc.url}">property>
<property name="username" value="${jdbc.username}">property>
<property name="password" value="${jdbc.password}">property>
bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource">property>
<property name="typeAliasesPackage" value="com.hou.pojo">property>
bean>
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.hou.dao">property>
bean>
7、Spring配置
<bean id="accountService" class="com.hou.service.impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao">property>
bean>
8、测试
/*
测试类
*/
public class CRUDTest {
@Test
public void findByName(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
Account account = accountService.findByName("迪丽热巴");
System.out.println("account = " + account);
}
@Test
public void findAll(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("ApplicationContext.xml");
AccountService accountService = applicationContext.getBean(AccountService.class);
List<Account> accountList = accountService.findAll();
for (Account account : accountList) {
System.out.println("account = " + account);
}
}
}