Spring通過DAO模式,提供了對iBATIS的良好支持。SqlMapClient對象是iBATIS中的主要對象,我們可以通過配置讓spring來管理SqlMapClient對象的創建。
與hibernate類似,Spring提供了SqlMapClientDaoSupport對象,我們的DAO可以繼承這個類,通過它所提供的SqlMapClientTemplate對象來操縱數據庫。看起來這些概念都與hibernate類似。
通過SqlMapClientTemplate來操縱數據庫的CRUD是沒有問題的。此篇文章沒有進行事務處理。
本文采用ibatis+spring+mysql 進行編寫
數據庫腳本如下
1 |
create database ibatis; |
2 |
|
3 |
create table person( |
4 |
id int primary key , |
5 |
name varchar (10), |
6 |
sex int |
7 |
); |
一:要有一個PO類
Person.java
01 |
package po; |
02 |
|
03 |
import java.io.Serializable; |
04 |
|
05 |
public class Person implements Serializable{ |
06 |
/** |
07 |
* |
08 |
*/ |
09 |
private static final long serialVersionUID
= -517413165963030507L; |
10 |
/** |
11 |
* |
12 |
*/ |
13 |
private int id; |
14 |
private String
name; |
15 |
private int sex; |
16 |
|
17 |
public Person(){ |
18 |
|
19 |
} |
20 |
public Person( int id,String
name, int sex){ |
21 |
this .id
= id; |
22 |
this .name
= name; |
23 |
this .sex
= sex; |
24 |
} |
25 |
public int getId()
{ |
26 |
return id; |
27 |
} |
28 |
public void setId( int id)
{ |
29 |
this .id
= id; |
30 |
} |
31 |
public String
getName() { |
32 |
return name; |
33 |
} |
34 |
public void setName(String
name) { |
35 |
this .name
= name; |
36 |
} |
37 |
public int getSex()
{ |
38 |
return sex; |
39 |
} |
40 |
public void setSex( int sex)
{ |
41 |
this .sex
= sex; |
42 |
} |
43 |
|
44 |
} |
二:DAO接口類
IAction.java
01 |
package dao; |
02 |
|
03 |
import java.util.List; |
04 |
|
05 |
import po.Person; |
06 |
|
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); //根據ID查詢 |
12 |
public List<Person>
queryAllPerson(); //查詢全部 |
13 |
} |
三:DAO實現類
ActionImpl.java 此類繼承SqlMapClientSupport 實現IAction接口
01 |
package dao.impl; |
02 |
|
03 |
import java.io.IOException; |
04 |
import java.io.Reader; |
05 |
import java.sql.SQLException; |
06 |
import java.util.List; |
07 |
|
08 |
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; |
09 |
|
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; |
14 |
|
15 |
import po.Person; |
16 |
import dao.IAction; |
17 |
|
18 |
public class ActionImpl extends SqlMapClientDaoSupport implements IAction
{ |
19 |
|
20 |
//添加操作 |
21 |
@Override |
22 |
public boolean insertPerson(Person
person) { |
23 |
//
TODO Auto-generated method stub |
24 |
getSqlMapClientTemplate().insert( "insertPerson" ,person); |
25 |
return false ; |
26 |
} |
27 |
|
28 |
//刪除操作 |
29 |
@Override |
30 |
public boolean deleteById( int id)
{ |
31 |
//
TODO Auto-generated method stub |
32 |
getSqlMapClientTemplate().delete( "deleteById" ,
id); |
33 |
return false ; |
34 |
} |
35 |
|
36 |
|
37 |
|
38 |
//查詢全部 |
39 |
@Override |
40 |
public List<Person>
queryAllPerson() { |
41 |
//
TODO Auto-generated method stub |
42 |
List<Person>
persons = getSqlMapClientTemplate().queryForList( "queryAllPerson" ); |
43 |
return persons; |
44 |
} |
45 |
|
46 |
@Override |
47 |
public Person
queryById( int id)
{ |
48 |
//
TODO Auto-generated method stub |
49 |
50 |
51 |
//自己添加實現代碼 |
52 |
return null ; |
53 |
} |
54 |
|
55 |
@Override |
56 |
public boolean updatePerson(Person
person) { |
57 |
//
TODO Auto-generated method stub |
58 |
59 |
60 |
//自己添加實現代碼 |
61 |
return false ; |
62 |
} |
四:既然是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" |
05 |
< sqlMapConfig > |
06 |
<!--此處一定不能有<settings/>
標籤--> |
07 |
<!--
<settings cacheModelsEnabled="true" |
08 |
enhancementEnabled="true" |
09 |
lazyLoadingEnabled="true" |
10 |
errorTracingEnabled="true" |
11 |
maxRequests="32" |
12 |
maxSessions="10" |
13 |
maxTransactions="5" |
14 |
useStatementNamespaces="false"
/> --> |
15 |
|
16 |
< sqlMap resource = "po/Person.xml" /> |
17 |
|
18 |
</ sqlMapConfig > |
SqlMapClient.xml裏本應該有數據源的配置的 使用spring之後數據源的配置移植到了spring上
五:Person.xml
裏面配置了一下對數據的增刪改查操作
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
<!DOCTYPE
sqlMap |
03 |
PUBLIC
"-//iBATIS.com//DTD SQL Map 2.0//EN" |
05 |
< sqlMap > |
06 |
< typeAlias alias = "person" type = "po.Person" /> |
07 |
|
08 |
< insert id = "insertPerson" parameterClass = "po.Person" > |
09 |
<![CDATA[ |
10 |
insert
into person values (#id#,#name#,#sex#) |
11 |
]]> |
12 |
</ insert > |
13 |
|
14 |
< delete id = "deleteById" parameterClass = "int" > |
15 |
<![CDATA[ |
16 |
delete
from person where id=#id# |
17 |
]]> |
18 |
</ delete > |
19 |
|
20 |
< update id = "updatePerson" parameterClass = "po.Person" > |
21 |
<![CDATA[ |
22 |
update
person set name=#name#,sex=#sex# where id=#id# |
23 |
]]> |
24 |
</ update > |
25 |
|
26 |
< select id = "queryById" parameterClass = "int" resultClass = "po.Person" > |
27 |
<![CDATA[ |
28 |
select
* from person where id=#id# |
29 |
]]> |
30 |
</ select > |
31 |
|
32 |
< select id = "queryAllPerson" cacheModel = "personCache" resultClass = "po.Person" > |
33 |
<![CDATA[ |
34 |
select
* from person |
35 |
]]> |
36 |
</ select > |
37 |
</ sqlMap > |
六:下面最重要的也就是配置applicationContext.xml了
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
03 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" |
04 |
xsi:schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" > |
05 |
|
06 |
< bean id = "dataSource" class = "org.apache.commons.dbcp.BasicDataSource" > |
07 |
< property name = "driverClassName" value = "com.mysql.jdbc.Driver" /> |
08 |
< property name = "url" value = "jdbc:mysql://localhost:3306/ibatis" /> |
09 |
< property name = "username" value = "root" /> |
10 |
< property name = "password" value = "1" /> |
11 |
</ bean > |
12 |
|
13 |
< bean id = "sqlMapClient" class = "org.springframework.orm.ibatis.SqlMapClientFactoryBean" > |
14 |
< property name = "configLocation" > <!--
name 爲configLocation或s 不可爲其他 --> |
15 |
< value >SqlMapConfig.xml</ value > <!--
不區分大小寫,路徑前可加'/' --> |
16 |
</ property > |
17 |
|
18 |
<!--
dataSource不是必需 --> |
19 |
< property name = "dataSource" > |
20 |
< ref local = "dataSource" /> |
21 |
</ property > |
22 |
</ bean > |
23 |
|
24 |
< bean id = "personDAO" class = "dao.impl.ActionImpl" > |
25 |
<!--
dataSource不是必需 --> |
26 |
< property name = "dataSource" > |
27 |
< ref local = "dataSource" /> |
28 |
</ property > |
29 |
|
30 |
<!--
sqlMapClient必需 --> |
31 |
< property name = "sqlMapClient" > |
32 |
< ref local = "sqlMapClient" /> |
33 |
</ property > |
34 |
</ bean > |
35 |
|
36 |
<!--
transactionManager不是必需 --> |
37 |
< bean id = "transactionManager" class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" > |
38 |
< property name = "dataSource" > |
39 |
< ref local = "dataSource" /> |
40 |
</ property > |
41 |
</ bean > |
42 |
</ beans > |
註釋裏面的必需或不是必需都是本人多次試驗的,至於爲什麼是必需不必需 其中的原理我也不是能太講清楚,在此先是這些寫罷了。
裏面的每一個節點,屬性,如果不太理解,可以上網查一些其他資料。
七:編寫測試類
此類利用junit進行測試。只測試了部分功能。
01 |
package dao.impl; |
02 |
|
03 |
import java.util.Iterator; |
04 |
import java.util.List; |
05 |
|
06 |
import org.junit.Test; |
07 |
import org.springframework.context.ApplicationContext; |
08 |
import org.springframework.context.support.ClassPathXmlApplicationContext; |
09 |
|
10 |
import po.Person; |
11 |
|
12 |
|
13 |
public class ActionImplTest
{ |
14 |
private static ApplicationContext
applicationContext = null ; //提供靜態ApplicationContext |
15 |
static { |
16 |
applicationContext
= new ClassPathXmlApplicationContext( "applicationContext.xml" ); //實例化 |
17 |
} |
18 |
//添加操作 |
19 |
@Test |
20 |
public void testInsertPerson(){ |
21 |
ActionImpl
s = (ActionImpl)applicationContext.getBean( "personDAO" ); |
22 |
s.insertPerson( new Person( 1 , "zhangsan" , 2 )); |
23 |
} |
24 |
|
25 |
//刪除操作 |
26 |
@Test |
27 |
public void testDeletePerson(){ |
28 |
ActionImpl
s = (ActionImpl)applicationContext.getBean( "personDAO" ); |
29 |
s.deleteById( 1 ); |
30 |
} |
31 |
|
32 |
//查詢全部 |
33 |
@Test |
34 |
public void testQueryAllPerson(){ |
35 |
ActionImpl
s = (ActionImpl)applicationContext.getBean( "personDAO" ); |
36 |
List<Person>
persons = s.queryAllPerson(); |
37 |
//System.out.println(persons.size()); |
38 |
Iterator<Person>
ite = persons.iterator(); |
39 |
while (ite.hasNext()){ |
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()); |
44 |
System.out.println(); |
45 |
} |
46 |
} |
47 |
} |
八:如需記錄日誌 則要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 |
11 |
|
12 |
log4j.logger.com.fiscal
= DEBUG |
13 |
log4j.logger.com.system
= DEBUG |
14 |
|
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 |
23 |
|
24 |
log4j.appender.stdout=org.apache.log4j.ConsoleAppender |
25 |
|
26 |
log4j.appender.fileout=org.apache.log4j.RollingFileAppender |
27 |
log4j.appender.fileout.File=C\:\\ibatis.log |
28 |
log4j.appender.fileout.MaxFileSize=10000KB |
29 |
|
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 |
34 |
|
35 |
#log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout |
36 |
|
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
下面是本人的目錄結構圖