二、ibatis与springMVC集成后的配置和测试
1. springMVC需要的jar包(3.1)
再导入commons-pool.jar,commons-dbcp.jar,commons-logging-1.1.1.jar三个与连接池相关的包
2. 修改对应的数据访问实现类StudentDAOImpl.java,并创建业务层接口和实现类(代码省略)
package com.wendellup.dao.impl;
import java.util.List;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Repository;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.wendellup.dao.IStudentDAO;
import com.wendellup.entity.Student;
@Repository
public class StudentDAOImpl extends SqlMapClientDaoSupport implements IStudentDAO{
@Autowired
public void setSqlMapClientForAutowire(SqlMapClient sqlMapClient) {
super.setSqlMapClient(sqlMapClient);
}
@Autowired
public void setDataSourceForAutowire(DataSource dataSource) {
super.setDataSource(dataSource);
}
public List<Student> queryAllStudent() throws Exception {
List<Student> studentList = null;
try {
//studentList = IbatisUtil.getClient().queryForList("selectAllStudent");
SqlMapClient client = getSqlMapClientTemplate().getSqlMapClient();
studentList = client.queryForList("selectAllStudent");
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return studentList;
}
public int addStudent(Student student) throws Exception {
int key;
try {
//key = (Integer) IbatisUtil.getClient().insert("insertStudent",student);
SqlMapClient client = getSqlMapClientTemplate().getSqlMapClient();
key = (Integer) client.insert("insertStudent",student);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
return key;
}
}
3. 创建spring容器配置文件applicationContext.xml,让其管理ibatis初始化,对应的SqlMapConfig.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"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 激活spring注解 -->
<!-- <context:annotation-config/> -->
<!-- 启动扫描所有的biz层,把作了注解的类转换为bean -->
<context:component-scan base-package="com.wendellup.biz" /> <!-- -->
<!-- 启动扫描所有的dao层,把作了注解的类转换为bean -->
<context:component-scan base-package="com.wendellup.dao" /> <!-- -->
<!--设置配置文件 -->
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath*:ibatis/db.properties</value>
</list>
</property>
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="maxActive" value="20" />
<property name="maxIdle" value="20" />
<property name="maxWait" value="3000" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="3600000" />
<!-- <property name="validationQuery" value="select 1 from student" /> -->
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1" />
<property name="driverClassName"
value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation"
value="classpath:ibatis/SqlMapConfig.xml" />
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="ibatis/Student.xml" />
</sqlMapConfig>
4. 创建集成后的测试类TestSpring.java
package test.wendellup;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.wendellup.biz.IStudentService;
public class TestSpring {
public static void main(String[] args) throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("spring/applicationContext.xml");
// System.out.println(ac);
// IStudentDAO iStudentDAO = (IStudentDAO) ac.getBean("StudentServiceImpl");
IStudentService iStudentService = (IStudentService) ac.getBean("studentServiceImpl");
System.out.println(iStudentService);
// System.out.println(iStudentService.queryAllStudent().size());
}
}
5. 搭建好springMVC的环境就可在jsp中获取到后台的值,此处省略。。。
项目的代码地址:
http://pan.baidu.com/share/link?shareid=141111&uk=3475027816