설정 트 랜 잭 션 springmv 3.1 my batis 3.1 트 랜 잭 션 이 작 동 하지 않 습 니 다.

10359 단어 springMVC
최근 개발 을 사용 하 는 과정 에서 작은 문제 가 발생 했 습 니 다. 원인 과 방법 인 배치 사 무 를 기록 하 는 김 에.
    Spring. 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:context="http://www.springframework.org/schema/context" xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.0.xsd

">

	<!--        -->

	<context:property-placeholder location="classpath:config.properties" />

	

	<!--     dao service (    ) -->

	<context:component-scan base-package="com.zfy.db.dao,com.zfy.service" />

</beans>

    spring - mybatis. 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: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-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/aop 

http://www.springframework.org/schema/aop/spring-aop-3.1.xsd

">



	<!-- JNDI        --><!--

 <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">

  <property name="jndiName" value="${jndiName}"></property>

  </bean> 



	--><!--       -->

	

	<bean name="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="0" />

		<property name="maxActive" value="20" />

		<property name="maxIdle" value="20" />

		<property name="minIdle" value="0" />

		<property name="maxWait" value="60000" />

		<property name="validationQuery" value="${validationQuery}" />

		<property name="testOnBorrow" value="false" />

		<property name="testOnReturn" value="false" />

		<property name="testWhileIdle" value="true" />

		<property name="timeBetweenEvictionRunsMillis" value="60000" />

		<property name="minEvictableIdleTimeMillis" value="25200000" />

		<property name="removeAbandoned" value="true" />

		<property name="removeAbandonedTimeout" value="1800" />

		<property name="logAbandoned" value="true" />

		<property name="filters" value="mergeStat" />

	</bean>

	

	<!-- myBatis   -->

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

		<property name="dataSource" ref="dataSource" />

		<!--     entity  ,   Configuration.xml       -->

		<property name="mapperLocations">

			<array>

				<value>classpath:com/zfy/mapper/portal/*.xml</value>

				<value>classpath:com/zfy/mapper/permission/*.xml</value>

				<value>classpath:com/zfy/mapper/form/*.xml</value>

			</array>

		</property>

	</bean>



	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

		<property name="basePackage" value="com.zfy.db.dao" />

		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

	</bean>



	<!--        ,     dataSource SqlSessionFactoryBean dataSource   ,           -->

	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

		<property name="dataSource" ref="dataSource" />

	</bean>



	<!--          -->

	<!-- <tx:annotation-driven transaction-manager="transactionManager" /> -->



	<!--           -->

	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">

		<tx:attributes>

			<tx:method name="add*" propagation="REQUIRED" />

			<tx:method name="append*" propagation="REQUIRED" />

			<tx:method name="insert*" propagation="REQUIRED" rollback-for="java.lang.Exception"/>

			<tx:method name="save*" propagation="REQUIRED" />

			<tx:method name="update*" propagation="REQUIRED" />

			<tx:method name="modify*" propagation="REQUIRED" />

			<tx:method name="edit*" propagation="REQUIRED" />

			<tx:method name="delete*" propagation="REQUIRED" />

			<tx:method name="remove*" propagation="REQUIRED" />

			<tx:method name="repair" propagation="REQUIRED" />

			<tx:method name="delAndRepair" propagation="REQUIRED" />



			<tx:method name="get*" propagation="SUPPORTS" />

			<tx:method name="find*" propagation="SUPPORTS" />

			<tx:method name="load*" propagation="SUPPORTS" />

			<tx:method name="all*" propagation="SUPPORTS" />

			<tx:method name="search*" propagation="SUPPORTS" />

			<tx:method name="datagrid*" propagation="SUPPORTS" />



			<tx:method name="*" propagation="SUPPORTS" />

		</tx:attributes>

	</tx:advice>

	<aop:config>

		<aop:pointcut id="transactionPointcut" expression="execution(* com.zfy.service..*.*impl.*(..))" />

		<aop:advisor pointcut-ref="transactionPointcut" advice-ref="transactionAdvice" />

	</aop:config>





	<!--   druid  spring jdbc -->

	<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">

	</bean>

	<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">

		<property name="patterns">

			<list>

				<value>com.zfy.service.*</value>

			</list>

		</property>

	</bean>

	<aop:config>

		<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />

	</aop:config>

	

</beans>

    매일 같은 이치
러시아 작가 체 호 프 는 "큰 개가 있 고 강아지 가 있 으 며 강 아 지 는 큰 개의 존재 로 당황 해 서 는 안 된다" 며 "모든 개 는 짖 어야 한다. 각자 하나님 이 주신 목소리 로 하 라" 고 말 했다.
    
    프로젝트 구조 
    DAO 인터페이스:  com.zfy.db.dao.form.FormSubjectMapper 
    
package com.zfy.db.dao.form;



import com.zfy.db.model.form.FormSubject;



public interface FormSubjectMapper {

  



    int insert(FormSubject record);

 }

    Service :     com.zfy.service.form.FormSubjectService
package com.zfy.service.form;



import com.zfy.db.model.form.FormSubject;



/**

 * @author keke

 * @version     :2013-5-15   11:05:45

 * 

 */

public interface FormSubjectService {

	public    int insert(FormSubject record) throws Exception;

}

 
    ServiceImpl: com.zfy.service.form.impl.FormSubjectServiceImpl
 
package com.zfy.service.form.impl;mport com.zfy.db.model.form.FormSubject;



/**

 * @author keke

 * @version     :2013-5-15   11:06:56

 * 

 */

@Service

@Transactional

public class FormSubjectServiceImpl implements FormSubjectService {



	@Resource

	private FormSubjectMapper mapper;



	public int insert(FormSubject record) throws Exception {

		return mapper.insert(record);

	}





}

 
     mybatis 프로필 FormSubjectMapper. 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.zfy.db.dao.form.FormSubjectMapper" >

  <resultMap id="BaseResultMap" type="com.zfy.db.model.form.FormSubject" >

    <id column="FORM_CODE" property="formCode" jdbcType="INTEGER" />

    <result column="FORM_NAME" property="formNam" jdbcType="VARCHAR" />

  </resultMap>



  <insert id="insert" parameterType="com.zfy.db.model.form.FormSubject" >

    insert into FORM_SUBJECT (FORM_CODE, FORM_NAME)

    values (#{formCode,jdbcType=INTEGER}, #{formName,jdbcType=VARCHAR})

  </insert>

  </mapper>

 

 
    Junit  테스트 클래스:
 
@Test

	public void  test1() throws Exception{

	    

		try {

			FormSubject fs = new FormSubject();

 			  fs.setFormCode(3);

 			  fs.setFormName("  123");

  			 for (int i = 0; i <2; i++) {

  			   mapper.insert( fs);

   		}} catch (RuntimeException e) {

			e.printStackTrace();

		}

	}

      
     개발 라인: Spring 3.1  mybatis 3.1 
    실행 후, "장삼" 은 삽입 되 었 으 나, "이사" 는 유일한 구속 조건 을 위반 하여 스크롤 백 을 하지 않 았 기 때문에 업무 가 역할 을 하지 못 했다.
 
    도와 주세요. 감사합니다! 온라인 등...
글 이 끝나 면 프로그래머 의 농담 어록 을 공유 하 겠 습 니 다. '중국인 이 아니 라' 는 댓 글 을 보 니 댓 글 을 가 라 앉 히 려 는 의도 입 니 다.

좋은 웹페이지 즐겨찾기