环境:jdk1.7 spring3.2.2 struts2.3.15 mybatis3.2.2 druid1.0.9.jar
MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子包),如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean,这样,我们在Service中就可以直接注入映射接口的Bean。
==============其中一个业务类StudentBiz.xml========================
上一篇文章我们讲解了ssm整合之面向接口编程,这一篇将在上一篇的基础上用注解进一步改造。
上一篇文章中,如果数据映射接口很多的话,需要在Spring的配置文件中对数据映射接口做配置,相应的配置项也会很多。为了简化配置,在MyBatis-Spring中提供了一个转换器MapperScannerConfigurer,它可以将接口直接转换为Spring容器中的Bean,在Senvce中@Autowired的方法直接注入接口实例。在Spring的配置文件中可以采用如下所示的配置将接口转化为Bean。
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryName" value="sqlSessionFactory"/>
<propertyname="basePackage" value=" com.cshope.jboa.dao"/>
</bean>
MapperScannerConfigurer将扫描basePackage所指定的包下的所有接口类(包括子包),如果它们在SQL映射文件中定义过,则将它们动态定义为一个Spring Bean,这样,我们在Service中就可以直接注入映射接口的Bean。
目录结构
相当在上一篇的基础上省了spring-dao.xml,spring-biz.xml
关键代码
==================spring核心配置文件context.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:aop="http://www.springframework.org/schema/aop"
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.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- durid连接池配置start -->
<bean id="duridConfig" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:configs/druid.properties</value>
</list>
</property>
</bean>
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<property name="filters" value="${filters}" />
<property name="initialSize" value="${initialSize}" />
<property name="maxActive" value="${maxActive}" />
<property name="minIdle" value="${minIdle}" />
<property name="maxWait" value="${maxWait}" />
<property name="validationQuery" value="${validationQuery}" />
<property name="testWhileIdle" value="${testWhileIdle}" />
<property name="testOnBorrow" value="${testOnBorrow}" />
<property name="testOnReturn" value="${testOnReturn}" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="${maxPoolPreparedStatementPerConnectionSize}" />
<property name="removeAbandoned" value="${removeAbandoned}" />
<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />
<property name="timeBetweenEvictionRunsMillis" value="${timeBetweenEvictionRunsMillis}" />
<property name="minEvictableIdleTimeMillis" value="${minEvictableIdleTimeMillis}" />
</bean>
<!-- durid连接池配置end -->
<!-- spring整合mybatis -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="druidDataSource"></property>
<property name="configLocation">
<value>classpath:configs/mybatis-config.xml</value>
</property>
<property name="mapperLocations">
<list>
<value>classpath:configs/mappers/*.xml</value>
</list>
</property>
</bean>
<bean id="sqlTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"></constructor-arg>
</bean>
<!-- 启动注解的扫描支持 -->
<context:component-scan base-package="com.obtk.biz">
</context:component-scan>
<!-- 自动扫描接口并转换为dao实例 -->
<bean id="daoScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
<property name="basePackage" value="com.obtk.dao"></property>
</bean>
<!-- 事务配置start -->
<bean id="txManger" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="druidDataSource"></property>
</bean>
<tx:advice id="txAdvise" transaction-manager="txManger">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="search*" read-only="true"/>
<tx:method name="load*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="get*" read-only="true"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="save*" propagation="REQUIRED"/>
<tx:method name="modify*" propagation="REQUIRED"/>
<tx:method name="update*" propagation="REQUIRED"/>
<tx:method name="del*" propagation="REQUIRED"/>
<tx:method name="do*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="mycut" expression="execution(* com.obtk.biz.*.*(..))"/>
<aop:advisor advice-ref="txAdvise" pointcut-ref="mycut"/>
</aop:config>
<!-- 事务配置end -->
</beans>
==============其中一个业务类StudentBiz.xml========================
package com.obtk.biz;
import java.util.List;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.obtk.dao.IStudentDao;
import com.obtk.entitys.PageEntity;
import com.obtk.entitys.StudentEntity;
@Service(value="stuBiz")
public class StudentBiz {
@Autowired
private IStudentDao stuDao;
public StudentEntity loadOne(Integer stuId) {
return stuDao.loadOne(stuId);
}
public List<StudentEntity> queryByPage(final PageEntity thePage) {
return stuDao.queryByPage(thePage);
}
public Integer countCnt(PageEntity thePage) {
return stuDao.countCnt(thePage);
}
}
==============action代码StudentPageAction========================
package com.obtk.actions.stu;
import java.util.List;
import java.util.Map;
import com.obtk.biz.StudentBiz;
import com.obtk.entitys.PageEntity;
import com.obtk.entitys.StudentEntity;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class StudentPageAction extends ActionSupport{
private String stuName;
private String gender;
private String deptName;
private String pageNo;
private String pageSize;
private StudentBiz stuBiz;
public void setStuBiz(StudentBiz stuBiz) {
this.stuBiz = stuBiz;
}
public String execute() throws Exception {
PageEntity thePage=new PageEntity();
if(pageNo!=null){
int mYPageNo=Integer.parseInt(pageNo);
thePage.setPageNo(mYPageNo);
}
if(pageSize!=null){
int myPageSize=Integer.parseInt(pageSize);
thePage.setPageSize(myPageSize);
}
if(stuName!=null){
stuName=new String(stuName.getBytes("iso-8859-1"),"utf-8");
thePage.setStuName(stuName);
}
if(gender!=null){
gender=new String(gender.getBytes("iso-8859-1"),"utf-8");
thePage.setGender(gender);
}
if(deptName!=null){
deptName=new String(deptName.getBytes("iso-8859-1"),"utf-8");
thePage.setDeptName(deptName);
}
//第一次进来就是第一页数据
List<StudentEntity> stuList=stuBiz.queryByPage(thePage);
//总条数
int rows=stuBiz.countCnt(thePage);
int totalPages=0;
if(rows%thePage.getPageSize()==0){
totalPages=rows/thePage.getPageSize();
}else{
totalPages=rows/thePage.getPageSize()+1;
}
//因为要在页面上展示
thePage.setTotalPages(totalPages);
ActionContext ac=ActionContext.getContext();
Map request=(Map)ac.get("request");
if(stuList!=null){
//分页实体对象
request.put("thePage",thePage);
//尽量用request对象保存数据
request.put("stuList",stuList);
return this.SUCCESS;
}else{
return this.ERROR;
}
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public String getPageNo() {
return pageNo;
}
public void setPageNo(String pageNo) {
this.pageNo = pageNo;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
}
其它代码请参考上一篇博文