Mybatis-Spring整合、Spring实现事务管理

Mybatis-Spring整合

整合步骤:

  1. 编写数据源配置
  2. 编写 sqlSessionFactory配置
  3. 编写sqlSessionTemplate配置
  4. 实现接口实现类
  5. 将实现类,注入到Spring
  6. 测试使用即可

代码实现

步骤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>
    
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
bean>
    
<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
    <constructor-arg index="0" ref="sqlSessionFactory"/>
bean>

步骤4实现

  1. 方式一
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();
    }
}

  1. 方式二(使用getSqlSession()来获取SqlSession对象)
public class UserMapper2 extends SqlSessionDaoSupport implements Mapper {

    public List<user> selectUser() {
        Mapper mapper = getSqlSession().getMapper(Mapper.class);
        return mapper.selectUser();
    }
}

步骤5实现


<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文件配置)

  1. 配置xml文件

<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>
    
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="mapperLocations" value="classpath:com/practice/dao/*.xml"/>
	bean>
    
	<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>
  1. 余下步骤,便是整合步骤的4,5,6步,

你可能感兴趣的:(Mybatis-Spring整合、Spring实现事务管理)