Myibatis和spring的集成(与事务配置) 一

一些介绍就不用说了,直接进入正题
1.所需要的jar包:
aopalliance-1.0.jar
asm-3.1.jar
aspectjweaver-1.6.2.jar
c3p0-0.9.1.2.jar
cglib-2.2.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
junit-4.7.jar
log4j-1.2.13.jar
mybatis-3.0.4-javadoc.jar
mybatis-3.0.4-sources.jar
mybatis-3.0.6-SNAPSHOT.jar
mybatis-spring-1.0.1-SNAPSHOT.jar
mysql-connector-java-5.1.10-bin.jar
org.springframework.aop-3.0.2.RELEASE.jar
org.springframework.asm-3.0.2.RELEASE.jar
org.springframework.aspects-3.0.2.RELEASE.jar
org.springframework.beans-3.0.2.RELEASE.jar
org.springframework.context-3.0.2.RELEASE.jar
org.springframework.context.support-3.0.2.RELEASE.jar
org.springframework.core-3.0.2.RELEASE.jar
org.springframework.expression-3.0.2.RELEASE.jar
org.springframework.jdbc-3.0.2.RELEASE.jar
org.springframework.orm-3.0.2.RELEASE.jar
org.springframework.test-3.0.2.RELEASE.jar
org.springframework.transaction-3.0.2.RELEASE.jar
org.springframework.web-3.0.2.RELEASE.jar
org.springframework.web.servlet-3.0.2.RELEASE.jar
slf4j-api-1.5.8.jar
slf4j-log4j12-1.5.8.jar
2.创建基础类
Person类:
package com.ibatis.model;

import java.io.Serializable;

public class Person implements Serializable {
	
	private static final long serialVersionUID = 5755798096473771101L;
	private Groups groups;
	private int id;
	private String name;
	private String password;

	public Person() {
	}

	public Groups getGroups() {
		return groups;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getPassword() {
		return password;
	}

	public void setGroups(Groups groups) {
		this.groups = groups;
	}

	public void setId(int id) {
		this.id = id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}



Groups类:
package com.ibatis.model;

import java.io.Serializable;

public class Groups implements Serializable {

	private static final long serialVersionUID = 1L;

	private int gid;

	private String gname;

	public int getGid() {
		return gid;
	}

	public String getGname() {
		return gname;
	}

	public void setGid(int gid) {
		this.gid = gid;
	}

	public void setGname(String gname) {
		this.gname = gname;
	}

}


PersonDao类:
package com.ibatis.dao;

import java.util.List;
import java.util.Map;

import com.ibatis.model.Person;


@SuppressWarnings("rawtypes")
public interface PersonDao {
	
	Person getPersonById(int id);

	List<Map> getAllPerson();
	
	int updatePersonBuId(Map map);

}

PersonDaoImpl类:
package com.ibatis.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.springframework.stereotype.Repository;

import com.ibatis.dao.PersonDao;
import com.ibatis.model.Person;


@SuppressWarnings({"rawtypes","unchecked"})
@Repository
public class PersonDaoImpl extends SqlSessionDaoSupport implements PersonDao{

	@Override
	public Person getPersonById(int id) {
		SqlSession sqlSession= getSqlSession();
		return (Person) sqlSession.selectOne("getPersonById",id);
	}

	
	@Override
	public List<Map> getAllPerson() {
		return getSqlSession().selectList("getAllPerson");
	}

	@Override
	public int updatePersonBuId(Map map) {
		int result =getSqlSession().update("updatePersonBuId",map);
		//result = result/0; 测试事务是否回滚
		return result;
	}
}


applicationContext.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:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util" xmlns:jee="http://www.springframework.org/schema/jee"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd"
	default-autowire="byName" default-lazy-init="false">

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

	<context:component-scan base-package="com.ibatis.dao" />

	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
		p:driverClass="${jdbc.driverClass}" p:jdbcUrl="${jdbc.jdbcUrl}"
		p:user="${jdbc.user}" p:password="${jdbc.password}" p:initialPoolSize="${c3p0.initialPoolSize}"
		p:minPoolSize="${c3p0.minPoolSize}" p:maxPoolSize="${c3p0.maxPoolSize}"
		p:acquireIncrement="${c3p0.acquireIncrement}" p:maxIdleTime="${c3p0.maxIdleTime}"
		p:maxStatements="${c3p0.maxStatements}" lazy-init="true" />
		
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<tx:annotation-driven transaction-manager="transactionManager"/>
	
	<aop:aspectj-autoproxy proxy-target-class="true" />
	
	<tx:advice id="txAdvice">
		<tx:attributes>
			<tx:method name="get*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="query*" propagation="SUPPORTS" read-only="true" />
			<tx:method name="*" propagation="REQUIRED" />
		</tx:attributes>
	</tx:advice>
	
	
	<aop:config>
		<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.ibatis.dao.*.*(..))"/>
	</aop:config>

	<context:annotation-config />

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:/config/ibatisConfig.xml" />
		<property name="typeAliasesPackage" value="com.ibatis.model" />
		<property name="mapperLocations" value="classpath*:com/ibatis/mapper/*.xml" />
	</bean>
	
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.ibatis.dao" />
	</bean>
</beans>

今天先把配置上上,明天来分析一下

你可能感兴趣的:(spring,集成,myibatis)