Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【一】
2013-06-13 13:34
501 查看
开发环境:
System:Windows
WebBrowser:IE6+、Firefox3+
JavaEEServer:tomcat5.0.2.8、tomcat6
IDE:eclipse、MyEclipse8
Database:MySQL
开发依赖库:
JavaEE5、Spring3.0.5、Mybatis3.0.4、myBatis-spring-1.0、Struts2.2.3、junit4.8.2、ext2.2.2
Email:hoojo_@126.com
Blog:http://blog.csdn.net/IBM_hoojo
http://hoojo.cnblogs.com/
上次介绍过Spring3、SpringMVC、MyBatis3整合,在线博文:/article/4791843.html
1、下载jar包
Struts2jar下载:
http://labs.renren.com/apache-mirror//struts/library/struts-2.2.3-lib.zip
Spring3jar下载:
http://ebr.springsource.com/repository/app/library/version/detail?name=org.springframework.spring&version=3.0.5.RELEASE
MyBatis3jar下载:http://www.mybatis.org/java.html
2、添加的jar包如下:
1、需要的jar文件如下:
2、在src目录中加入mybatis.xml,内容如下:
上面的配置文件中,可以加入一些公共、常用的MyBatis方面的全局配置。如handler、objectFactory、plugin、以及mappers的映射路径(由于在applicationContext-common中的SqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等。这个类的文件名称和下面的applicationContext-common.xml中的configLocation中的值对应,不是随便写的。
3、在src目录中添加applicationContext-common.xml中加入内容如下:
DataSource,这里的是采用jdbc的dataSource;
SqlSessionFactory,是MyBatis团队提供整合Spring的SqlSession工厂Bean用它可以完成Spring和MyBatis的整合。SqlSessionFactoryBean需要注入DataSource,配置myBatis配置文件的location,以及配置mapper.xml和resultMap.xml文件的路径,可以用通配符模式配置。其实mapper里面是可以存放resultMap的内容的。由于resultMap文件的内容是和JavaBean及数据库表对象进行映射的。一般一张表、一个JavaBean(Model)对应一个resultMap文件。将resultMap独立出来提供可读性、维护性。
TransactionManager,事务管理器是采用jdbc的管理器。需要注入DataSource数据源,这里注入的数据源和SqlSessionFactory是同一个数据源。如果不同的数据源,事务将无法起到作用。
baseTransactionProxy,事务的传播特性才有spring提供的TransactionProxyFactoryBean这个事务代理工厂的拦截器类来完成。其他的暂时还没有可以支持事务配置的传播特性和隔离级别的方法,关于这里你可以参考:/article/4791844.html
MapperScannerConfigurer是MyBatis的mapper接口的扫描器,通过这个配置可以完成对指定basePackage报下的类进行扫描,如果这些类有继承SqlMapper这个类的,将会是MyBatis的接口。不需要单独配置mapper,而完成注入。
4、在src目录添加applicationContext-beans.xml这个文件,文件内容如下:
System:Windows
WebBrowser:IE6+、Firefox3+
JavaEEServer:tomcat5.0.2.8、tomcat6
IDE:eclipse、MyEclipse8
Database:MySQL
开发依赖库:
JavaEE5、Spring3.0.5、Mybatis3.0.4、myBatis-spring-1.0、Struts2.2.3、junit4.8.2、ext2.2.2
Email:hoojo_@126.com
Blog:
上次介绍过Spring3、SpringMVC、MyBatis3整合,在线博文:
一、准备工作
1、下载jar包Struts2jar下载:
Spring3jar下载:
MyBatis3jar下载:
2、添加的jar包如下:
二、Spring、MyBatis整合
1、需要的jar文件如下:2、在src目录中加入mybatis.xml,内容如下:
xmlversion="1.0"encoding="UTF-8"?>
DOCTYPEconfigurationPUBLIC"-//mybatis.org//DTDConfig3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<--别名-->
<typeAliases>
<typeAliastype="com.hoo.entity.Account"alias="account"/>
typeAliases>
configuration>
上面的配置文件中,可以加入一些公共、常用的MyBatis方面的全局配置。如handler、objectFactory、plugin、以及mappers的映射路径(由于在applicationContext-common中的SqlSessionFactoryBean有配置mapper的location,这里就不需要配置)等。这个类的文件名称和下面的applicationContext-common.xml中的configLocation中的值对应,不是随便写的。
3、在src目录中添加applicationContext-common.xml中加入内容如下:
xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<--配置DataSource数据源-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
<propertyname="url"value="jdbc:mysql://10.0.0.131:3306/ash2"/>
<propertyname="username"value="dev"/>
<propertyname="password"value="dev"/>
bean>
<--配置SqlSessionFactoryBean-->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<propertyname="dataSource"ref="dataSource"/>
<propertyname="configLocation"value="classpath:mybatis.xml"/>
<--mapper和resultmap配置路径-->
<propertyname="mapperLocations">
<list>
<--表示在com.hoo.resultmap包或以下所有目录中,以-resultmap.xml结尾所有文件-->
<value>classpath:com/hoo/resultmap/**/*-resultmap.xmlvalue>
<value>classpath:com/hoo/entity/*-resultmap.xmlvalue>
<value>classpath:com/hoo/mapper/**/*-mapper.xmlvalue>
list>
property>
bean>
<--配置事务管理器,注意这里的dataSource和SqlSessionFactoryBean的dataSource要一致,不然事务就没有作用了-->
<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<propertyname="dataSource"ref="dataSource"/>
bean>
<--配置事务的传播特性-->
<beanid="baseTransactionProxy"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"abstract="true">
<propertyname="transactionManager"ref="transactionManager"/>
<propertyname="transactionAttributes">
<props>
<propkey="add*">PROPAGATION_REQUIREDprop>
<propkey="edit*">PROPAGATION_REQUIREDprop>
<propkey="remove*">PROPAGATION_REQUIREDprop>
<propkey="insert*">PROPAGATION_REQUIREDprop>
<propkey="update*">PROPAGATION_REQUIREDprop>
<propkey="del*">PROPAGATION_REQUIREDprop>
<propkey="*">readOnlyprop>
props>
property>
bean>
<--通过扫描的模式,扫描目录在com/hoo/mapper目录下,所有的mapper都继承SqlMapper接口的接口,这样一个bean就可以了-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.hoo.mapper"/>
<propertyname="markerInterface"value="com.hoo.mapper.SqlMapper"/>
bean>
beans>
DataSource,这里的是采用jdbc的dataSource;
SqlSessionFactory,是MyBatis团队提供整合Spring的SqlSession工厂Bean用它可以完成Spring和MyBatis的整合。SqlSessionFactoryBean需要注入DataSource,配置myBatis配置文件的location,以及配置mapper.xml和resultMap.xml文件的路径,可以用通配符模式配置。其实mapper里面是可以存放resultMap的内容的。由于resultMap文件的内容是和JavaBean及数据库表对象进行映射的。一般一张表、一个JavaBean(Model)对应一个resultMap文件。将resultMap独立出来提供可读性、维护性。
TransactionManager,事务管理器是采用jdbc的管理器。需要注入DataSource数据源,这里注入的数据源和SqlSessionFactory是同一个数据源。如果不同的数据源,事务将无法起到作用。
baseTransactionProxy,事务的传播特性才有spring提供的TransactionProxyFactoryBean这个事务代理工厂的拦截器类来完成。其他的暂时还没有可以支持事务配置的传播特性和隔离级别的方法,关于这里你可以参考:
MapperScannerConfigurer是MyBatis的mapper接口的扫描器,通过这个配置可以完成对指定basePackage报下的类进行扫描,如果这些类有继承SqlMapper这个类的,将会是MyBatis的接口。不需要单独配置mapper,而完成注入。
4、在src目录添加applicationContext-beans.xml这个文件,文件内容如下:
xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
'target='_blank'>http://www.springframework.org/schema/beans/spring-beans-3.0.xsd[/code] 'target='_blank'>http://www.springframework.org/schema/context[/code] 'target='_blank'>http://www.springframework.org/schema/context/spring-context-3.0.xsd">[/code] <--注解探测器--><context:component-scanbase-package="com.hoo"/>beans>
这里面可以完成一下Action、Bean的注入配置
5、JavaBean(Model、Entity)相关类、及resultMap.xml文件内容
Beanpackagecom.hoo.entity;importjava.io.Serializable;importjava.util.Date;publicclassAccountimplementsSerializable{privatestaticfinallongserialVersionUID=-7970848646314840509L;privateIntegeraccountId;privateStringusername;privateStringpassword;privateDatecreateTime;publicAccount(){super();}//getter、setter@OverridepublicStringtoString(){returnthis.accountId+"#"+this.username+"#"+this.password+"#"+this.createTime;}}
account-resultMap.xmlxmlversion="1.0"encoding="UTF-8"?>DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mappernamespace="accountMap"><resultMaptype="com.hoo.entity.Account"id="accountResultMap"><idproperty="accountId"column="account_id"/><resultproperty="username"column="username"/><resultproperty="password"column="password"/><resultproperty="createTime"column="create_time"/>resultMap>mapper>
resultMap的type属性和对应的classpath对应,id会在mapper.xml文件中用到。下面的属性property是JavaBean的属性,column和数据库的字段对应。
6、上面的applicationContext-common.xml中配置了SqlMapper,下面是SqlMapper代码,就一个空接口packagecom.hoo.mapper;/***function:所有的Mapper继承这个接口*@authorhoojo*@createDate2011-4-12下午04:00:31*@fileSqlMapper.java*@packagecom.hoo.mapper*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceSqlMapper{}
7、定制我们自己的增删改查(CRUD)组件,接口如下packagecom.hoo.mapper;importjava.util.List;importorg.springframework.dao.DataAccessException;/***function:BaseSqlMapper继承SqlMapper,对Mapper进行接口封装,提供常用的增删改查组件;*也可以对该接口进行扩展和封装*@authorhoojo*@createDate2011-4-14上午11:36:41*@fileBaseSqlMapper.java*@packagecom.hoo.mapper*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceBaseSqlMapper<T>extendsSqlMapper{publicvoidadd(Tentity)throwsDataAccessException;publicvoidedit(Tentity)throwsDataAccessException;publicvoidremvoe(Tentity)throwsDataAccessException;publicTget(Tentity)throwsDataAccessException;publicList<T>getList(Tentity)throwsDataAccessException;}
当然实际开发中,增删改查组件一定不止这几个方法。这里只是随便挪列几个方法做一个示例。实际中可以根据需求进行添加方法,这里添加的方法可以用一个mapper.xml进行实现,然后注入这个mapper即可完成操作。也可以定义其他的mapper,如AccountMapper继承这个接口。然后为AccountMapper提供实现也是可以的。
8、下面看看AccountMapper接口packagecom.hoo.mapper;importjava.util.List;importcom.hoo.entity.Account;/***function:继承SqlMapper,MyBatis数据操作接口;此接口无需实现类*@authorhoojo*@createDate2010-12-21下午05:21:20*@fileAccountMapper.java*@packagecom.hoo.mapper*@projectMyBatis*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceAccountMapper<TextendsAccount>extendsBaseSqlMapper<T>{publicList<T>getAllAccount();}
上面的AccountMapper继承了BaseSqlMapper,并且提供了自己所需要的方法。下面实现这个Mapper中和父接口的方法。
account-mapper.xmlxmlversion="1.0"encoding="UTF-8"?>DOCTYPEmapperPUBLIC"-//mybatis.org//DTDMapper3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><--namespace和定义的Mapper接口对应,并实现其中的方法--><mappernamespace="com.hoo.mapper.AccountMapper"><selectid="getList"parameterType="com.hoo.entity.Account"resultType="list"resultMap="accountResultMap">select*fromaccountwhereusernamelike'%'#{username}'%'select><selectid="getAllAccount"resultType="list"resultMap="accountResultMap">select*fromaccountselect><--accountResultMap是account-resultmap.xml中定义的resultmap--><selectid="get"parameterType="account"resultType="com.hoo.entity.Account"resultMap="accountResultMap">[CDATA[select*fromaccountwhereaccount_id=#{accountId}]]>select><--自动生成id策略--><insertid="add"useGeneratedKeys="true"keyProperty="account_id"parameterType="account">insertintoaccount(account_id,username,password)values(#{accountId},#{username},#{password})insert><updateid="edit"parameterType="account">updateaccountsetusername=#{username},password=#{password}whereaccount_id=#{accountId}update><deleteid="remove"parameterType="account">deletefromaccountwhereaccount_id=#{accountId}delete>mapper>
mapper的namespace和接口的classpath对应,里面的sql语句的id和方法名称对应。这样就完成了AccountMapper的实现。
9、下面来测试下AccountMapper的功能,代码如下:packagecom.hoo.mapper;importjava.util.List;importjavax.inject.Inject;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;importcom.hoo.entity.Account;/***function:AccountMapperJUnit测试类*@authorhoojo*@createDate2011-4-12下午04:21:50*@fileAccountMapperTest.java*@packagecom.hoo.mapper*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/@ContextConfiguration("classpath:applicationContext-*.xml")publicclassAccountMapperTestextendsAbstractJUnit38SpringContextTests{@InjectprivateAccountMapper<Account>mapper;publicvoidtestGetAccount(){Accountacc=newAccount();acc.setAccountId(28);System.out.println(mapper.get(acc));}publicvoidtestAdd(){Accountaccount=newAccount();account.setUsername("lisi@155.com");account.setPassword("abc");mapper.add(account);}publicvoidtestEditAccount(){Accountacc=newAccount();acc.setAccountId(28);acc=mapper.get(acc);System.out.println(acc);acc.setUsername("Zhangsan22");acc.setPassword("123123");mapper.edit(acc);System.out.println(mapper.get(acc));}publicvoidtestRemoveAccount(){Accountacc=newAccount();acc.setAccountId(28);mapper.remvoe(acc);System.out.println(mapper.get(acc));}publicvoidtestAccountList(){List<Account>acc=mapper.getAllAccount();System.out.println(acc.size());System.out.println(acc);}publicvoidtestList(){Accountacc=newAccount();acc.setUsername("@qq.com");List<Account>list=mapper.getList(acc);System.out.println(list.size());System.out.println(list);}}
运行上面的方法,没有错误基本上就算完成了Mapper的功能了。
10、下面来写一个数据库操作的基类Dao,代码如下:packagecom.hoo.dao;importjava.util.List;importcom.hoo.mapper.BaseSqlMapper;/***function:*@authorhoojo*@createDate2011-4-14上午11:30:09*@fileBaseMapperDao.java*@packagecom.hoo.dao*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceBaseMapperDao<T>{@SuppressWarnings("unchecked")publicvoidsetMapperClass(Class<?extendsBaseSqlMapper>mapperClass);publicBaseSqlMapper<T>getMapper();publicbooleanadd(Tentity)throwsException;publicbooleanedit(Tentity)throwsException;publicbooleanremove(Tentity)throwsException;publicTget(Tentity)throwsException;publicList<T>getAll(Tentity)throwsException;}
这个Dao定义了BaseSqlMapper中的方法,还提供了一个setMapperClass的方法。目的是在使用这个dao的时候,需要设置一个Mapper的class。且这个Mapper必须要是BaseSqlMapper或它的子类。所有的dao都可以用BaseMapperDao来完成CRUD操作即可,当BaseMapperDao的方法不够用的情况下,可以在接口中提供SqlSession、SqlSessionTemplate方法,让底层人员自己扩展所需要的方法。这里的dao和mapper好像有点冗余,因为dao和mapper完成的功能都是类似或是相同的。但是你可以在dao中,同时调用不同的mapper,来完成当前模块的dao所需要的功能。
11、看看BaseMapperDao的实现代码packagecom.hoo.dao.impl;importjava.util.List;importjavax.inject.Inject;importorg.apache.ibatis.session.SqlSessionFactory;importorg.mybatis.spring.SqlSessionTemplate;importorg.springframework.stereotype.Repository;importcom.hoo.dao.BaseMapperDao;importcom.hoo.mapper.BaseSqlMapper;/***function:运用SqlSessionTemplate封装Dao常用增删改方法,可以进行扩展*@authorhoojo*@createDate2011-4-14下午12:22:07*@fileBaseMapperDaoImpl.java*@packagecom.hoo.dao.impl*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/@SuppressWarnings("unchecked")@RepositorypublicclassBaseMapperDaoImpl<T>extendsSqlSessionTemplateimplementsBaseMapperDao<T>{@InjectpublicBaseMapperDaoImpl(SqlSessionFactorysqlSessionFactory){super(sqlSessionFactory);}privateClass<?extendsBaseSqlMapper>mapperClass;publicvoidsetMapperClass(Class<?extendsBaseSqlMapper>mapperClass){this.mapperClass=mapperClass;}publicBaseSqlMapper<T>getMapper(){returnthis.getMapper(mapperClass);}publicbooleanadd(Tentity)throwsException{booleanflag=false;try{this.getMapper().add(entity);flag=true;}catch(Exceptione){flag=false;throwe;}returnflag;}publicbooleanedit(Tentity)throwsException{booleanflag=false;try{this.getMapper().edit(entity);flag=true;}catch(Exceptione){flag=false;throwe;}returnflag;}publicTget(Tentity)throwsException{returnthis.getMapper().get(entity);}publicList<T>getAll()throwsException{returnthis.getMapper().getList(null);}publicbooleanremove(Tentity)throwsException{booleanflag=false;try{this.getMapper().remvoe(entity);flag=true;}catch(Exceptione){flag=false;throwe;}returnflag;}}
这个实现类继承了SqlSessionTemplate,并且要注入SqlSessionFactory。提供的setMapperClass方法需要设置其BaseSqlMapper或它的子类。
好了,至此基本的CRUD的mapper和dao就算完成了。现在我们可以定义我们自己的业务模块,调用公共组件来完成增删改查操作。
12、下面测试下这个BaseMapperDao功能packagecom.hoo.dao;importjavax.inject.Inject;importorg.junit.Before;importorg.springframework.test.context.ContextConfiguration;importorg.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;importcom.hoo.entity.Account;importcom.hoo.mapper.AccountMapper;/***function:*@authorhoojo*@createDate2011-4-14下午01:08:49*@fileBaseMapperDaoImplTest.java*@packagecom.hoo.dao*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/@ContextConfiguration("classpath:applicationContext-*.xml")publicclassBaseMapperDaoImplTestextendsAbstractJUnit38SpringContextTests{@InjectprivateBaseMapperDao<Account>dao;@Beforepublicvoidinit(){System.out.println(dao);dao.setMapperClass(AccountMapper.class);}publicvoidtestGet()throwsException{init();Accountacc=newAccount();acc.setAccountId(28);System.out.println(dao.get(acc));}publicvoidtestAdd()throwsException{init();Accountaccount=newAccount();account.setUsername("temp@156.com");account.setPassword("abc");System.out.println(dao.add(account));}}
13、下面定义AccountDao接口packagecom.hoo.dao;importjava.util.List;importorg.springframework.dao.DataAccessException;/***function:Account数据库操作dao接口*@authorhoojo*@createDate2011-4-13上午10:21:38*@fileAccountDao.java*@packagecom.hoo.dao*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceAccountDao<T>{/***function:添加Account对象信息*@authorhoojo*@createDate2011-4-13上午11:50:05*@paramentityAccount*@returnboolean是否成功*@throwsDataAccessException*/publicbooleanaddAccount(Tentity)throwsDataAccessException;/***function:根据id对到Account信息*@authorhoojo*@createDate2011-4-13上午11:50:45*@paramid编号id*@returnAccount*@throwsDataAccessException*/publicTgetAccount(Integerid)throwsDataAccessException;/***function:查询所有Account信息*@authorhoojo*@createDate2011-4-13上午11:51:45*@paramid编号id*@returnAccount*@throwsDataAccessException*/publicList<T>getList()throwsDataAccessException;}
14、AccountDao实现类packagecom.hoo.dao.impl;importjava.util.List;importjavax.inject.Inject;importorg.springframework.dao.DataAccessException;importorg.springframework.stereotype.Repository;importcom.hoo.dao.AccountDao;importcom.hoo.dao.BaseMapperDao;importcom.hoo.entity.Account;importcom.hoo.mapper.AccountMapper;/***function:Account数据库操作dao*@authorhoojo*@createDate2011-4-13上午10:25:02*@fileAccountDaoImpl.java*@packagecom.hoo.dao.impl*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/@SuppressWarnings("unchecked")@RepositorypublicclassAccountDaoImpl<TextendsAccount>implementsAccountDao<T>{@InjectprivateBaseMapperDao<Account>dao;publicbooleanaddAccount(Tentity)throwsDataAccessException{dao.setMapperClass(AccountMapper.class);booleanflag=false;try{dao.add(entity);flag=true;}catch(DataAccessExceptione){flag=false;throwe;}catch(Exceptione){thrownewRuntimeException(e);}returnflag;}publicTgetAccount(Integerid)throwsDataAccessException{dao.setMapperClass(AccountMapper.class);Accountacc=newAccount();Tentity=null;try{acc.setAccountId(id);entity=(T)dao.get(acc);}catch(DataAccessExceptione){throwe;}catch(Exceptione){thrownewRuntimeException(e);}returnentity;}publicList<T>getList()throwsDataAccessException{dao.setMapperClass(AccountMapper.class);List<T>list=null;try{list=(List<T>)((AccountMapper)dao.getMapper()).getAllAcount();}catch(DataAccessExceptione){throwe;}catch(Exceptione){thrownewRuntimeException(e);}returnlist;}}
注意,上面的方法都设置了MapperClass,表示当前dao的Mapper是AccountMapper对象。所有的mapper方法都是调用AccountMapper这个对象中的方法。
15、下面定义服务器层接口packagecom.hoo.biz;importjava.util.List;importorg.springframework.dao.DataAccessException;/***function:biz层Account接口*@authorhoojo*@createDate2011-4-13上午11:33:04*@fileAccountBiz.java*@packagecom.hoo.biz*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*/publicinterfaceAccountBiz<T>{/***function:添加Account对象信息*@authorhoojo*@createDate2011-4-13上午11:50:05*@paramentityAccount*@returnboolean是否成功*@throwsDataAccessException*/publicbooleanaddAccount(Tentity)throwsDataAccessException;publicbooleanexecute(Tentity)throwsDataAccessException;/***function:根据id对到Account信息*@authorhoojo*@createDate2011-4-13上午11:50:45*@paramid编号id*@returnAccount*@throwsDataAccessException*/publicTgetAccount(Integerid)throwsDataAccessException;/***function:查询所有Account信息*@authorhoojo*@createDate2011-4-13上午11:51:45*@paramid编号id*@returnAccount*@throwsDataAccessException*/publicList<T>getList()throwsDataAccessException;}
16、AccountBiz的实现代码packagecom.hoo.biz.impl;importjava.util.List;importjavax.inject.Inject;importorg.springframework.dao.DataAccessException;importorg.springframework.stereotype.Component;importcom.hoo.biz.AccountBiz;importcom.hoo.dao.AccountDao;importcom.hoo.entity.Account;importcom.hoo.exception.BizException;/***function:AccountBiz接口实现*@authorhoojo*@createDate2011-4-13上午11:34:39*@fileAccountBizImpl.java*@packagecom.hoo.biz.impl*@projectMyBatisForSpring*@blog'target='_blank'>http://blog.csdn.net/IBM_hoojo[/code] *@emailhoojo_@126.com*@version1.0*///@Component@ServicepublicclassAccountBizImpl<TextendsAccount>implementsAccountBiz<T>{@InjectprivateAccountDao<T>dao;publicbooleanaddAccount(Tentity)throwsDataAccessException{if(entity==null){thrownewBizException(Account.class.getName()+"对象参数信息为Empty!");}returndao.addAccount(entity);}publicTgetAccount(Integerid)throwsDataAccessException{returndao.getAccount(id);}publicList<T>getList()throwsDataAccessException{returndao.getList();}publicbooleanexecute(Tentity)throwsDataAccessException{if(entity==null){thrownewBizException(Account.class.getName()+"对象参数信息为Empty!");}returndao.addAccount(entity);}}
直接注入AccountDao完成相关操作即可
17、如果你需要在业务层设置事务传播特性,需要在applicationContext-bean.xml中加入配置如下:<--为AccountBiz接口配置事务拦截器,baseTransactionProxy是事务拦截器,在Action中获取这个对象--><beanid="accountBiz"parent="baseTransactionProxy"><--设置target,也就是AccountBiz的实现类--><propertyname="target"ref="accountBizImpl"/>bean>
这样在Action中注入accountBiz这个对象后,那么当这个对象出现DataAccessException异常的时候,就会自动回滚事务。
至此,Spring和MyBatis的整合就完成了。
相关文章推荐
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【一】
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【二】
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【二】
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【二】
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【一】
- Struts2、Spring3、MyBatis3整合ExtJS,完成ColumnTree 【二】
- Struts2、Spring3、MyBatis3整合ExtJS,完成CheckNodeColumnTree
- Struts2、Spring3、MyBatis3整合ExtJS,完成CheckNodeColumnTree
- Struts2、Spring3、MyBatis3整合ExtJS,完成CheckNodeColumnTree
- Struts2、Spring3、MyBatis3整合ExtJS,完成CheckNodeColumnTree
- 整合Spring Struts2 mybatis 所需要的架包
- 一步一步完成SSM框架整合(Spring+Spring MVC + Mybatis)
- 使用Spring插件完成Spring和Struts2的整合
- 10-MyBatis_spring与struts2的整合
- SpringBoot整合Mybatis完成增删改查功能
- Struts2 Spring3 Mybatis 3 整合 相关配置文件
- Struts2、Spring3.2.4、Mybatis和Mysql数据库整合开发
- 利用Intellij+MAVEN完成Spring整合Mybatis项目详解
- struts2、spring和mybatis整合理解
- MyBatis_spring与struts2的整合