Spring通过DAO模式,提供了对iBATIS的良好支持。SqlMapClient对象是iBATIS中的主要对象,我们可以通过配置让spring来管理SqlMapClient对象的创建。
与hibernate类似,Spring提供了SqlMapClientDaoSupport对象,我们的DAO可以继承这个类,通过它所提供的SqlMapClientTemplate对象来操纵数据库。看起来这些概念都与hibernate类似。
通过SqlMapClientTemplate来操纵数据库的CRUD是没有问题的。此篇文章没有进行事务处理。
本文采用ibatis+spring+mysql 进行编写
数据库脚本如下
1 | create database ibatis; |
一:要有一个PO类
Person.java
03 | import java.io.Serializable; |
05 | public class Person implements Serializable{ |
09 | private static final long serialVersionUID = -517413165963030507L; |
20 | public Person(int id,String name,int sex){ |
28 | public void setId(int id) { |
31 | public String getName() { |
34 | public void setName(String name) { |
40 | public void setSex(int sex) { |
二:DAO接口类
IAction.java
03 | import java.util.List; |
07 | public interface IAction { |
08 | public boolean insertPerson(Person person); |
09 | public boolean deleteById(int id); |
10 | public boolean updatePerson(Person person); |
11 | public Person queryById(int id); |
12 | public List<Person> queryAllPerson(); |
三:DAO实现类
ActionImpl.java 此类继承SqlMapClientSupport 实现IAction接口
03 | import java.io.IOException; |
04 | import java.io.Reader; |
05 | import java.sql.SQLException; |
06 | import java.util.List; |
08 | import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; |
10 | import com.ibatis.common.resources.Resources; |
11 | import com.ibatis.sqlmap.client.SqlMapClient; |
12 | import com.ibatis.sqlmap.client.SqlMapClientBuilder; |
13 | import com.ibatis.sqlmap.client.SqlMapSession; |
18 | public class ActionImpl extends SqlMapClientDaoSupport implements IAction { |
22 | public boolean insertPerson(Person person) { |
24 | getSqlMapClientTemplate().insert("insertPerson",person); |
30 | public boolean deleteById(int id) { |
32 | getSqlMapClientTemplate().delete("deleteById", id); |
40 | public List<Person> queryAllPerson() { |
42 | List<Person> persons = getSqlMapClientTemplate().queryForList("queryAllPerson"); |
47 | public Person queryById(int id) { |
56 | public boolean updatePerson(Person person) { |
四:既然是ibatis spring整合 那就必须要有ibatis的配置文件
SqlMapConfig.xml
01 | <?xml version="1.0" encoding="UTF-8" ?> |
02 | <!DOCTYPE sqlMapConfig |
03 | PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" |
16 | <sqlMap resource="po/Person.xml" /> |
SqlMapClient.xml里本应该有数据源的配置的 使用spring之后数据源的配置移植到了spring上
五:Person.xml
里面配置了一下对数据的增删改查操作
01 | <?xml version="1.0" encoding="UTF-8"?> |
03 | PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" |
06 | <typeAlias alias="person" type="po.Person" /> |
08 | <insert id="insertPerson" parameterClass="po.Person"> |
10 | insert into person values (#id#,#name#,#sex#) |
14 | <delete id="deleteById" parameterClass="int"> |
16 | delete from person where id=#id# |
20 | <update id="updatePerson" parameterClass="po.Person"> |
22 | update person set name=#name#,sex=#sex# where id=#id# |
26 | <select id="queryById" parameterClass="int" resultClass="po.Person"> |
28 | select * from person where id=#id# |
32 | <select id="queryAllPerson" cacheModel="personCache" resultClass="po.Person"> |
六:下面最重要的也就是配置applicationContext.xml了
01 | <?xml version="1.0" encoding="UTF-8"?> |
06 | <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> |
07 | <property name="driverClassName" value="com.mysql.jdbc.Driver" /> |
09 | <property name="username" value="root" /> |
10 | <property name="password" value="1" /> |
13 | <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean"> |
14 | <property name="configLocation"> |
15 | <value>SqlMapConfig.xml</value> |
19 | <property name="dataSource"> |
20 | <ref local="dataSource" /> |
24 | <bean id="personDAO" class="dao.impl.ActionImpl"> |
26 | <property name="dataSource"> |
27 | <ref local="dataSource" /> |
31 | <property name="sqlMapClient"> |
32 | <ref local="sqlMapClient"/> |
37 | <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> |
38 | <property name="dataSource"> |
39 | <ref local="dataSource" /> |
注释里面的必需或不是必需都是本人多次试验的,至于为什么是必需不必需 其中的原理我也不是能太讲清楚,在此先是这些写罢了。
里面的每一个节点,属性,如果不太理解,可以上网查一些其他资料。
七:编写测试类
此类利用junit进行测试。只测试了部分功能。
03 | import java.util.Iterator; |
04 | import java.util.List; |
06 | import org.junit.Test; |
07 | import org.springframework.context.ApplicationContext; |
08 | import org.springframework.context.support.ClassPathXmlApplicationContext; |
13 | public class ActionImplTest { |
14 | private static ApplicationContext applicationContext = null; |
16 | applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); |
20 | public void testInsertPerson(){ |
21 | ActionImpl s = (ActionImpl)applicationContext.getBean("personDAO"); |
22 | s.insertPerson(new Person(1,"zhangsan",2)); |
27 | public void testDeletePerson(){ |
28 | ActionImpl s = (ActionImpl)applicationContext.getBean("personDAO"); |
34 | public void testQueryAllPerson(){ |
35 | ActionImpl s = (ActionImpl)applicationContext.getBean("personDAO"); |
36 | List<Person> persons = s.queryAllPerson(); |
38 | Iterator<Person> ite = persons.iterator(); |
40 | Person person = ite.next(); |
41 | System.out.print("ID: "+person.getId()); |
42 | System.out.print(" Name: "+person.getName()); |
43 | System.out.print(" Sex: "+person.getSex()); |
八:如需记录日志 则要log4j.properties
01 | #log4j.rootLogger=DEBUG, stdout |
02 | #log4j.appender.stdout=org.apache.log4j.ConsoleAppender |
03 | #log4j.appender.stdout.layout=org.apache.log4j.PatternLayout |
04 | #log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n |
05 | #log4j.logger.java.sql.PreparedStatement=DEBUG |
06 | log4j.rootLogger=DEBUG, stdout, fileout |
07 | #log4j.logger.test=info |
08 | #log4j.logger.org.apache.jasper = DEBUG |
09 | #log4j.logger.org.apache.catalina.startup.TldConfig = DEBUG |
10 | #log4j.logger.org.apache.catalina.session.ManagerBase = DEBUG |
12 | log4j.logger.com.fiscal = DEBUG |
13 | log4j.logger.com.system = DEBUG |
15 | log4j.logger.com.ibatis = DEBUG |
16 | log4j.logger.com.ibatis.common.jdbc.SimpleDataSource = DEBUG |
17 | log4j.logger.com.ibatis.common.jdbc.ScriptRunner = DEBUG |
18 | log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate = DEBUG |
19 | log4j.logger.java.sql.Connection = DEBUG |
20 | log4j.logger.java.sql.Statement = DEBUG |
21 | log4j.logger.java.sql.PreparedStatement = DEBUG, fileout |
22 | log4j.logger.java.sql.ResultSet = DEBUG |
24 | log4j.appender.stdout=org.apache.log4j.ConsoleAppender |
26 | log4j.appender.fileout=org.apache.log4j.RollingFileAppender |
27 | log4j.appender.fileout.File=C\:\\ibatis.log |
28 | log4j.appender.fileout.MaxFileSize=10000KB |
30 | log4j.appender.stdout.layout=org.apache.log4j.PatternLayout |
31 | log4j.appender.stdout.layout.ConversionPattern=[%-5p] %d{yyyy-MM-dd HH\:mm\:ss} \:%m%n |
32 | log4j.appender.fileout.layout=org.apache.log4j.PatternLayout |
33 | log4j.appender.fileout.layout.ConversionPattern=[%-5p]_%d{yyyy-MM-dd HH\:mm\:ss} \:%m%n |
35 | #log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout |
37 | # log4j.logger.org=info |
九:已经到最后了,我觉得这最后的才是最最重要的,就是一下jar包问题
我调试了很长时间 一大部分时间是jar问题
在此列出一下我认为能够跑起来这个小程序所需的一下jar包
如没有,可网上下载。
- ibaits-2.3.4.jar
- spring.jar
- mysql-connector-java-bin.jar
- commons-dbcp-1.4.jar
- commons-pool-1.5.6.jar
- spring-orm-2.5.6.jar //已集成到spring.jar里
- //记录日志所需
- log4j-1.2.15.jar
- commons-logging.jar
下面是本人的目录结构图
