-
Spring整合Mybatis:
1.导入Java相关Jar包
-
juint
-
mybatis
-
mysql数据库
-
spring相关的
-
aop织入
-
mybatis-spring[new]
<dependencies>
<!-- junit单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.10.RELEASE</version>
</dependency>
<!-- Spring操作数据库时,需要spring-jdbc包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
</dependencies>
2.编写配置文件
3.测试
spring-整合mybatis步骤:
-
编写数据源配置。
-
sqlSessonFactory
-
sqlSessonTemplate
-
给接口加实现类
-
将自己写的实现类,注入到Spring中
-
测试
编写数据源配置 sqlSessonFactory sqlSessonTemplate:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://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">
<!-- DataSource:使用Spring的数据源 替换Mybatis的配置 c3p0连接池 dbcp druid
这里使用spring提供的JDBC :org.springframework.jdbc.datasource DriverManagerDataSource
-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<!-- 配置连接数据库的驱动 和 MySQL:连接的其他参数-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/health?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="abc123"/>
</bean>
<!-- sqlSessionFactory
Spring来构建mybatis中的sqlSessionFactory
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源参数-->
<property name="dataSource" ref="dataSource"/>
<!-- 绑定mybatis配置文件 使我的spring-dao.xml文件和 mybatis-config.xml文件连接起来 -->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 注册映射器-->
<property name="mapperLocations" value="classpath:com/yu/mapper/*.xml"/>
</bean>
<!-- SqlSessionTemplate :就是我们要使用的 sqlSession 需要带有参数-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- SqlSessionTemplate 没有set方法所以
只能使用构造方法来设置参数 -->
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
给接口加实现类
package com.yu.mapper;
import com.yu.pojo.StuInfo;
import org.mybatis.spring.SqlSessionTemplate;
import java.util.List;
public class StuInfoMapperImpl implements StuInfoMapper {
//在原来我们所有的操作都是使用 sqlsession,但是现在我们使用 sqlSessionTemlpate 是一样的效果
private SqlSessionTemplate sqlSession;
public void setSqlSession(SqlSessionTemplate sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public List<StuInfo> selectAll() {
// StuInfoMapper mapper = sqlSession.getMapper(StuInfoMapper.class);等同于sqlSession.getMapper(StuInfoMapper.class).selectAll()
// 通过SqlsessionTemplate来操作获取对应的mapper.xml配置文件中的方法来执行获取返回值
return sqlSession.getMapper(StuInfoMapper.class).selectAll();
}
}
将自己写的实现类,注入到Spring中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="spring-dao.xml"/>
<!-- 把 StuInfoMapperImpl 注册到spring容器中来 然后再获取相应的值 -->
<bean id="stuInfoMapper" class="com.yu.mapper.StuInfoMapperImpl">
<!-- 给StuInfoMapperImpl 中配置 我们已经设置好了的 sqlsessionTemplate-->
<property name="sqlSession" ref="sqlSessionTemplate"/>
</bean>
</beans>
测试
@Test
public void test02(){
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StuInfoMapper stuInfoMapper = context.getBean("stuInfoMapper", StuInfoMapper.class);
List<StuInfo> stuInfos = stuInfoMapper.selectAll();
stuInfos.forEach(StuInfo-> System.out.println(StuInfo));
}