Mybatis-Spring整合
整合步骤:
- 编写数据源配置
- 编写 sqlSessionFactory配置
- 编写sqlSessionTemplate配置
- 实现接口实现类
- 将实现类,注入到Spring
- 测试使用即可
代码实现
步骤1,2,3实现
<!--数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定Mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
</bean>
<!--sqlSession配置-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
步骤4实现
- 方式一
public class UserMapper implements Mapper {
private SqlSessionTemplate sqlSessionTemplate;
public void setSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate) {
this.sqlSessionTemplate = sqlSessionTemplate;
}
public List<user> selectUser() {
Mapper mapper = sqlSessionTemplate.getMapper(Mapper.class);
return mapper.selectUser();
}
}
- 方式二(使用getSqlSession()来获取SqlSession对象)
public class UserMapper2 extends SqlSessionDaoSupport implements Mapper {
public List<user> selectUser() {
Mapper mapper = getSqlSession().getMapper(Mapper.class);
return mapper.selectUser();
}
}
步骤5实现
<?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: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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
<import resource="beans.xml"/>
<bean id="userMapper" class="com.practice.dao.UserMapper">
<property name="sqlSessionTemplate" ref="sqlSession"/>
</bean>
<bean id="userMapper2" class="com.practice.dao.UserMapper2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
步骤6实现
public class myTest {
@Test
public void test(){
ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
Mapper userMapper = context.getBean("userMapper2", Mapper.class);
List<user> users = userMapper.selectUser();
for (user user : users) {
System.out.println(user);
}
}
}
Spring实现事务管理(xml文件配置)
- 配置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:aop="http://www.springframework.org/schema/aop"
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/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--数据源配置-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
<!--sqlSessionFactory配置-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--绑定Mabatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
</bean>
<!--sqlSession配置-->
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
<!--配置声明式事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource" />
</bean>
<!--配置事务通知;-->
<tx:advice id="txad" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!--配置事务切入-->
<aop:config>
<aop:pointcut id="pointcut" expression="execution(* com.practice.dao.*.*(..))"/>
<aop:advisor advice-ref="txad" pointcut-ref="pointcut"/>
</aop:config>
- 余下步骤,便是整合步骤的4,5,6步,
本文详细介绍Mybatis与Spring框架的整合步骤,包括数据源、sqlSessionFactory和sqlSessionTemplate的配置,实现接口的方式,以及如何在Spring中注入实现类进行测试。同时,还提供了两种实现接口的具体代码示例,以及如何通过Spring的ApplicationContext获取并使用这些组件。
1024

被折叠的 条评论
为什么被折叠?



