MyBatis搭配spring和事务配置

我这里做的例子是mybatis。

一:需要的jar
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.39version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>4.3.2.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.3.0version>
        dependency>
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.4.1version>
        dependency>
二:配置
  • applicationContext.xml
    
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans        
    http://www.springframework.org/schema/beans/spring-beans.xsd    
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx.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
    http://code.alibabatech.com/schema/dubbo        
    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

    
    
    <import resource="classpath:dubbo.xml" />

    
    <context:component-scan base-package="com.we" />
    
    <import resource="classpath:jdbc.xml" />
    
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true">tx:annotation-driven>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:mybatis.xml">property>
    bean>
    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.we.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    bean>
beans>
  • jdbc.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" 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.1.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd 
    http://www.springframework.org/schema/task   
    http://www.springframework.org/schema/task/spring-task-3.1.xsd   
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop.xsd">


    <context:property-placeholder location="classpath:jdbc.properties"
        ignore-unresolvable="true" />

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        init-method="init" destroy-method="close">
        
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        
        <property name="initialSize" value="1" />
        <property name="minIdle" value="1" />
        <property name="maxActive" value="20" />
        
        <property name="maxWait" value="60000" />
        
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 'x'" />
        <property name="testWhileIdle" value="true" />
        <property name="testOnBorrow" value="false" />
        <property name="testOnReturn" value="false" />
        
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
        
        <property name="filters" value="stat" />
    bean>
beans>
  • mybatis.xml


<configuration>
    <mappers>
        <mapper resource="com/we/dao/PersonDao.xml" />
    mappers>
configuration>
3代码
package com.we.dao;

import com.we.entry.Person;

public interface PersonDao {

    public Person get(int id);

    // public List<Person> getAll();
    //
    // public int insert(Person person);
    //
    public int updateById(Person user);
    //
    // public int delete(int id);
}




<mapper namespace="com.we.dao.PersonDao">
    <select id="get" parameterType="com.we.entry.Person" resultMap="result">
        select * from person WHERE id=#{id}
    select>

    <resultMap type="com.we.entry.Person" id="result">
        <id column="id" property="id" />
        <result column="name" property="name" />
    resultMap>

    <update id="updateById" parameterType="com.we.entry.Person">
        update person set
        name=#{name}  where id=#{id}
    update>
mapper>
4测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class MyTest {

    @Resource
    private PersonDao userDao;

    @Test
    public void testAddition() {
        assertEquals(userDao.get(1) != null, true);
    }

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        PersonService o = (PersonService) ctx.getBean("ps");
        o.testTx();


    }

}

下面是事务相关的代码

@org.springframework.stereotype.Service("ps")
public class PersonServiceImpl implements PersonService {

    @Autowired
    private PersonDao personDao;

    // 可以通过加上该注解,和去掉对比测试。
    @Transactional
    public void testTx() {
        Person p = new Person();
        p.setId(1);
        p.setName("3333");
        personDao.updateById(p);
        int i = 1 / 0;// 使代码抛出异常
        p.setId(2);
        personDao.updateById(p);
    }
}

你可能感兴趣的:(spring,mybatis)