0%

spring整合mybatis

一.整合需要的jar

二.数据库关系表设计

1
2
3
4
5
6
7
8
CREATE TABLE `t_user` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` tinyint(4) DEFAULT NULL,
`born_date` date DEFAULT NULL,
`head_img` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

三.entity实体类

1
2
3
4
5
6
7
public class User {
private Long id;
private String name;
private Integer age;
private Date bornDate;
private String headImg;
}

四.实现mybatis的配置文件

(1)数据库配置文件db.properties

1
2
3
4
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://121.0.0.1:3306/smbms?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
jdbc.username=root
jdbc.password=xxxxxxx

***(2)创建核心配置:mybatis-config.xml**

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?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="db.properties"></properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
</configuration>

***(3)创建dao : UserDao**

***(4)创建mapper文件: UserMapper.xml**

1
2
3
4
5
6
7
8
9
10
11
12
<?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.bogedev.spring_mybatis.dao.TUserDao">
<resultMap type="com.bogedev.spring_mybatis.entity.TUser" id="TUserMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="bornDate" column="born_date" jdbcType="TIMESTAMP"/>
<result property="headImg" column="head_img" jdbcType="VARCHAR"/>
</resultMap>
</mapper>

五.编写dao层代码

1
2
3
public interface TUserDao {
public List<TUser> selectAll();
}

六.修改映射文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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.bogedev.spring_mybatis.dao.TUserDao">

<resultMap type="com.bogedev.spring_mybatis.entity.TUser" id="TUserMap">
<result property="id" column="id" jdbcType="INTEGER"/>
<result property="name" column="name" jdbcType="VARCHAR"/>
<result property="age" column="age" jdbcType="INTEGER"/>
<result property="bornDate" column="born_date" jdbcType="TIMESTAMP"/>
<result property="headImg" column="head_img" jdbcType="VARCHAR"/>
</resultMap>
<select id="selectAll" resultType="com.bogedev.spring_mybatis.entity.TUser">
select * from t_user
</select>
</mapper>

七.创建和修改spring配置文件

1
2
3
4
5
6
7
8
9
10
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">

</beans>

spring和mybatis的整合包由spring提供 mybatis-spring.jar

具体整合步骤

(1)配置加载数据库配置文件

1
2
3
<!--DataSource: 使用Spirng的数据源替换Mybatis的配置, 可以使用c3p0,dbcp,druid,或者spring提供的jdbc-->
<!-- 加载数据库配置信息 -->
<context:property-placeholder location="classpath:db.properties" system-properties-mode="NEVER"/>

(2)连接池的配置

1
2
3
4
5
6
7
8
<!-- 连接池对象 -->
<bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="username" value="${jdbc.username}"/>
<property name="url" value="${jdbc.url}"/>
<property name="password" value="${jdbc.password}"/>
</bean>

(3)配置SqlSessionFactory,关联MyBatis

1
2
3
4
5
6
7
8
9
10
11
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 1:连接池 -->
<property name="dataSource" ref="myDataSource"/>
<!-- 2:绑定MyBatis总配置文件 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 3:配置别名扫描 -->
<property name="typeAliasesPackage" value="com.bogedev.spring_mybatis.entity"/>
<!-- 4:加载mapper文件 -->
<property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
</bean>

八.这里我们分别演示三种方式整合

方式一:注册sqlSessionTemplate,关联sqlSessionFactory

(1)添加配置

1
2
3
4
5
<!--配置sqlSession-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<!--只能通过构造器注入sqlSessionFactory,因为没有set方法-->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>

(2)增加Mapper(Dao)接口的实现类;私有化sqlSessionTemplate

1
2
3
4
5
6
7
8
9
10
11
12
public class UserDaoImpl implements UserDao{

private SqlSessionTemplate sqlSession;

public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}

public List<User> selectAll() {
return sqlSession.getMapper(UserDao.class).selectAll();
}
}

(3)测试

1
2
3
ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("spring.xml");
TUserDao userdao=(TUserDao) context.getBean("userDao");
System.out.println(userdao.selectAll());

方式二:让Dao继承Support类 , 直接利用 getSqlSession()获得 , 然后直接注入SqlSessionFactory . 比起方式1 , 不需要管理SqlSessionTemplate , 而且对事务的支持更加友好

实现类代码

1
2
3
4
5
6
7
public class TuserDaoImpl  extends SqlSessionDaoSupport implements TUserDao {
@Override
public List<TUser> selectAll() {
return getSqlSession().getMapper(TUserDao.class).selectAll();
}
}

spring文件中的配置

1
2
3
<bean id="userDao" class="com.bogedev.spring_mybatis.dao.impl.TuserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>

方式三:前两种的整合实现方式,都创建了Dao接口的实现类,通过实现类来获取dao对象;
使用整合方式三,我们告诉Spring,让他来帮我们创建dao的代理对象即可

(1)配置MapperFactoryBean,Spring帮我们创建 Mapper的代理对象

1
2
3
4
5
6
7
8
<!-- 配置MapperFactoryBean,Spring帮我们创建 Mapper的代理对象-->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
<!--给哪个接口创建代理对象-->
<property name="mapperInterface" value="com.bogedev.spring_mybatis.dao.TUserDao"/>
</bean>


(2)或者采用下面扫描的方式

1
2
3
4
5

<!--扫描接口包路径,生成包下所有接口的代理对象,并且放入spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sunny.mapper"/>
</bean>