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" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <context:property-placeholder location="classpath:config.properties" /> <bean name="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName"><value>${jdbc.driverClass}</value></property> <property name="url"><value>${jdbc.jdbcUrl}</value></property> <property name="username"><value>${jdbc.user}</value></property> <property name="password"><value>${jdbc.password}</value></property> </bean> <!-- SqlSessionFactory配置 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!--指定mybatis mapper xml路径--> <property name="mapperLocations" value="classpath*:com/app/config/mybatis/**/*Mapper.xml"/> <!--指定mybatis config xml路径--> <property name="configLocation" value="classpath:/mybatis-config.xml"></property> </bean> <!--Mapper对象,相当于DAO--> <bean name="testUserMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <!--它只是一个接口,具体的sql配在相应的TestUserMaper.xml中--> <property name="mapperInterface" value="com.app.test.dbt.mapper.TestUserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!--server服务,事务加到这上面--> <bean name="TestUserService" class="com.app.test.dbt.service.TestUserService"> <property name="testUserMapper" ref="testUserMapper"/> </bean> <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--事务配置--> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="insert*" propagation="REQUIRED" isolation="DEFAULT" /> <tx:method name="delete*" propagation="REQUIRED" isolation="DEFAULT" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="TransactionPointCut" expression="execution(* com.app.test.dbt.service.*Service.*(..))" /> <aop:advisor advice-ref="txAdvice" pointcut-ref="TransactionPointCut" /> </aop:config> </beans>
mybatis config xml配置:
mybatis-config.xml:
<?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> <settings> <setting name="defaultStatementTimeout" value="120"/> </settings> </configuration>
Mapper sql配置:
TestUserMapper.xml:
<?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.app.test.dbt.mapper.TestUserMapper"> <insert id="insertUser" parameterType="com.app.test.dbt.entity.TestUserModel"> INSERT INTO test_user (id, name) VALUES(#{id}, #{name}) </insert> <delete id="deleteUser" parameterType="int"> delete from test_user where id= #{id} </delete> <update id="updateName" timeout="20"> update test_user set name=#{param1} where id= #{param2} </update> </mapper>
Mapper 接口:
TestUserMapper.java
/** * 方法名与上面的(TestUserMapper.xml) id一至 * 上面的Namespace == java包名+类名 */ public interface TestUserMapper { public void insertUser(TestUserModel user); public void deleteUser(int userId); public int updateName(String name,int userId); }
service类:
public class TestUserService { TestUserMapper testUserMapper; public TestUserMapper getTestUserMapper() { return testUserMapper; } public void setTestUserMapper(TestUserMapper testUserMapper) { this.testUserMapper = testUserMapper; } public void insertUser(){ int initValue=1; int times = 10; for(int i=0;i<times;i++){ TestUserModel testUserModel = new TestUserModel(); byte id = (byte)(initValue+i); testUserModel.setId(id); testUserModel.setName("me"); this.testUserMapper.insertUser(testUserModel); } } }
测试类:
public static void main(String args []){ ClassPathXmlApplicationContext classPathXmlApplicationContextTransaction = new ClassPathXmlApplicationContext("classpath*:/test.xml"); TestUserService testUserService = (TestUserService) classPathXmlApplicationContextTransaction.getBean("TestUserService"); testUserService.insertUser(); }