第一步 搭建环境spring的环境,项目的文档结构如下图:
所需的相关jar包如下图
第二步 环境搭建好以后,就可以进行代码的编写了,在开始前先概述一下大体的流程;在业务层PersonServiceImpl中注入PersonDao。
1.personServiceImpl.java如下
<span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-30
*@version 1.0
*@PersonServiceImpl.java
*/
package com.test.services.impl;
import java.util.List;
import com.test.bean.Person;
import com.test.dao.PersonDao;
import com.test.services.PersonService;
public class PersonServiceImpl implements PersonService {
//定义要注入的组件并且实现它的setter方法
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
//业务方法
public List<Person> findPerson(){
personDao.fingPersonList();
return null;
}
}</span></span><span style="font-size:18px;"><span style="font-size:18px;">package com.test.services;
import java.util.List;
import com.test.bean.Person;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:PersonService.java
*/
public interface PersonService {
public List<Person> findPerson();
}</span></span>PersonDaoImpl.java
<span style="font-size:18px;"><span style="font-size:18px;">package com.test.dao.impl;
import java.util.List;
import com.test.bean.Person;
import com.test.dao.PersonDao;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:PersonDaoImpl.java
*/
public class PersonDaoImpl implements PersonDao {
/** 在这里的业务只是向控制台输出一句话,如果能输出证明注入成功*/
public List<Person> fingPersonList(){
System.out.println("我是dao层的findPersonList");
return null;
}
}</span></span>PersonDaoImpl实现的接口
PersonDao.java
<span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-30
*@version 1.0
*@PersonDao.java
*/
package com.test.dao;
import java.util.List;
import com.test.bean.Person;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:PersonDao.java
*/
public interface PersonDao {
public abstract List<Person> fingPersonList();
}</span></span>applicationContext.xml
<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!--通过配置文件注入-->
<!-- 将personDao交给spring容器管理 -->
<bean id="personDao" class="com.test.dao.impl.PersonDaoImpl">
</bean>
<!-- 将personService交给spring容器管理,并且通过setter方式注入personDao -->
<bean id="personService" class="com.test.services.impl.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
</beans></span></span>这样我们的spring项目已经完成,接下来就可以进行测试了
4.测试类如下
PersonTest.java
<span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-29
*@version 1.0
*@PersonTest.java
*/
package com.test;
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.services.PersonService;
/**
*@author wenxue.nong
*@创建时间:2014-12-29
*@version 1.0
*@文件名称:PersonTest.java
*/
public class PersonTest {
@Test
public void test() {
//启动spring容器
ApplicationContext t = new ClassPathXmlApplicationContext("config/applicationContext.xml");
//从容器中通过id获取被管理的bean
PersonService personService = (PersonService) t.getBean("personService");
//得到该bean后调用里面的业务方法
personService.findPerson();
}
}
</span></span><span style="font-size:18px;"><span style="font-size:18px;">十二月 30, 2014 2:05:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@36c14a61: startup date [Tue Dec 30 14:05:42 CST 2014]; root of context hierarchy 十二月 30, 2014 2:05:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config/applicationContext.xml] 十二月 30, 2014 2:05:42 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1efb880e: defining beans [personDao,personService]; root of factory hierarchy 我是dao层的personDaoImpl</span></span>看到
<span style="font-size:18px;"><span style="font-size:18px;">我是dao层的personDaoImpl</span></span>证明 personDaoImpl已经注入到PersonServiceImpl中了。
第三步 当spring已经能注入成功后,接下来集成ibatis
如何集 首先把他相关的包导入到项目中如下 连接数据库肯定用到驱动这里是MySQL的驱动,通过连接池来连接肯定用到连接池commons-pool.jar,数据源用到commons-dbcp.jar,ibatis的jar包,ibatis-sqlMap.jar
ibatis有两个配置文件,总的配置文件sqlMapConfig.xml和xxx.xml对应一个domain,这里是Person.xml
我们这边的domain 如下
Person.java
<span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-30
*@version 1.0
*@Person.java
*/
package com.test.bean;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:Person.java
*/
public class Person {
private String id;
private String name;
}
</span></span>Person.xml对应的配置如下,并且我们给他一个查询SQL语句<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<!-- person的配置 -->
<sqlMap >
<typeAlias type="com.test.bean.Person" alias="person"/>
<select id="ibateisSelectPerson" resultClass="person">
select * from person
</select>
</sqlMap></span></span>sqlMapConfig.xml的配置如下
<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <sqlMap resource="mapper/Person.xml" /> </sqlMapConfig></span></span>配置完ibatis的配置文件就可以用spring集成ibatis了(集成 听起来高大上的样子 说白了 就是在applicationContext.xml中配置ibatis)
集成后的applicationContext.xml的配置文件如下
<span style="font-size:18px;"><span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
">
<!--通过配置文件注入-->
<!-- 将personDao交给spring容器管理 -->
<bean id="personDao" class="com.test.dao.impl.PersonDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"/>
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 将personService交给spring容器管理,并且通过setter方式注入personDao -->
<bean id="personService" class="com.test.services.impl.PersonServiceImpl">
<property name="personDao" ref="personDao"></property>
</bean>
<!-- property-placeholder是一个属性遍历器,定位一个属性文件 -->
<context:property-placeholder location="classpath:config/application.properties" />
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 注入ibatis -->
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation">
<value>classpath:config/sqlMapConfig.xml</value>
</property>
</bean>
</beans></span></span><span style="font-size:18px;"><span style="font-size:18px;">SqlMapClientDaoSupport 类,它里面有sqlMapClient和dataSource属性,并且实现了setter方法,所以可以在<span style="font-size:14px;">applicationContext.xml中注入</span></span></span>
<span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-30
*@version 1.0
*@PersonDaoImpl.java
*/
package com.test.dao.impl;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.test.bean.Person;
import com.test.dao.PersonDao;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:PersonDaoImpl.java
*/
public class PersonDaoImpl extends SqlMapClientDaoSupport implements PersonDao {
/**
*@author wenxue.nong
*@return
*@time 2014-12-30上午10:56:23
*/
public List<Person> fingPersonList(){
System.out.println("我是dao层的personDaoImpl");
List<Person> l = this.getSqlMapClientTemplate().queryForList("ibateisSelectPerson");
return l;
}
}
</span></span><span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-30
*@version 1.0
*@PersonServiceImpl.java
*/
package com.test.services.impl;
import java.util.List;
import com.test.bean.Person;
import com.test.dao.PersonDao;
import com.test.services.PersonService;
/**
*@author wenxue.nong
*@创建时间:2014-12-30
*@version 1.0
*@文件名称:PersonServiceImpl.java
*/
public class PersonServiceImpl implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;
}
/**
* 只做一个方法
*@author wenxue.nong
*@return
*@time 2014-12-30上午10:52:05
*/
public List<Person> findPerson(){
List<Person> l = personDao.fingPersonList();
return l;
}
}
</span></span><span style="font-size:18px;"><span style="font-size:18px;">/**
*@author wenxue.nong
*@2014-12-29
*@version 1.0
*@PersonTest.java
*/
package com.test;
import static org.junit.Assert.*;
import java.util.List;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.test.bean.Person;
import com.test.services.PersonService;
/**
*@author wenxue.nong
*@创建时间:2014-12-29
*@version 1.0
*@文件名称:PersonTest.java
*/
public class PersonTest {
@Test
public void test() {
//启动spring容器
ApplicationContext t = new ClassPathXmlApplicationContext("config/applicationContext.xml");
//从容器中通过id获取被管理的bean
PersonService personService = (PersonService) t.getBean("personService");
//得到该bean后调用里面的业务方法
List<Person> l = personService.findPerson();
System.out.println(l.get(0));
}
}
</span></span>
<span style="font-size:18px;"><span style="font-size:18px;">十二月 30, 2014 3:24:43 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@26d0fffc: startup date [Tue Dec 30 15:24:43 CST 2014]; root of context hierarchy 十二月 30, 2014 3:24:43 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from class path resource [config/applicationContext.xml] 十二月 30, 2014 3:24:43 下午 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties 信息: Loading properties file from class path resource [config/application.properties] 十二月 30, 2014 3:24:43 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons 信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@45890909: defining beans [personDao,personService,org.springframework.context.support.PropertySourcesPlaceholderConfigurer#0,dataSource,sqlMapClient]; root of factory hierarchy 我是dao层的personDaoImpl 证明注入成功 com.test.bean.Person@7791d63a 证明可以读取数据库的数据 </span></span>
注意的地方:在连接数据库的时候要改成自己的数据库可以在application.properties中灵活的修改
application.properties
<span style="font-size:18px;"><span style="font-size:18px;">#develop db jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc\:mysql\://localhost\:3306/person?useUnicode\=true&characterEncoding\=utf-8 jdbc.username=root jdbc.password=root </span></span>
person.sql用navicat导入自己的数据库
下载链接点击打开链接
原文地址:http://blog.csdn.net/nwx546250/article/details/42262411