[ibatis]ibatis的配置和测试,ibatis与springMVC集成后的配置与测试(二)

11 篇文章 0 订阅
4 篇文章 0 订阅

二、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

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值