首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

spring与ibatis调整

2012-09-04 
spring与ibatis整合1:导入spring与ibatis需要用的包。2:新建一个POJO类。例如:1.?package cn.itcast??? 2.?

spring与ibatis整合

1:导入spring与ibatis需要用的包。
2:新建一个POJO类。例如:
1.?package cn.itcast;???
2.?????
3.? public class Student {???
4.?? private Integer id;???
5.?????
6.?? private String firstname;???
7.?????
8.?? private String lastname;???
9.?????
10.?? public String getFirstname() {???
11.??? return firstname;???
12.?? }???
13.?????
14.?? public void setFirstname(String firstname) {???
15.??? this.firstname = firstname;???
16.?? }???
17.?????
18.?? public Integer getId() {???
19.??? return id;???
20.?? }???
21.?????
22.?? public void setId(Integer id) {???
23.??? this.id = id;???
24.?? }???
25.?????
26.?? public String getLastname() {???
27.??? return lastname;???
28.?? }???
29.?????
30.?? public void setLastname(String lastname) {???
31.??? this.lastname = lastname;???
32.?? }???
33.? }??

3:根据这个POJO类写一个sqlmap_student.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="studetnt">
? <select id="select.student" resultparameterparameterencoding="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>
?<settings????
???? cacheModelsEnabled="true"???????
???? enhancementEnabled="true"???????
???? lazyLoadingEnabled="true"???????
???? errorTracingEnabled="true"???????
???? maxRequests="128"???????????
???? maxSessions="64"???????????
???? maxTransactions="32"???????????
???? useStatementNamespaces="false"/>
?????
? ?<!-- ================================================================ -->
?<!-- 导入配置资源 ( 数据库模块配置文件) -->
?<!-- ================================================================ -->
?<sqlMap resource="sqlmap-student.xml" />
</sqlMapConfig>

?

5:创建一个有关数据源的properties文件取名为:jdbc.properties
# Properties file

datasource.driverClassName=oracle.jdbc.driver.OracleDriver
datasource.url=jdbc:oracle:thin:@localhost:1521:orcl
datasource.username=omsdata
datasource.password=omsdata_2010

datasource.maxActive=100
datasource.maxIdle=10
datasource.maxWait=120000
datasource.whenExhaustedAction=1
datasource.validationQuery=select 1 from dual
datasource.testOnBorrow=true
datasource.testOnReturn=false


6:配置spring的配置文件applicationContext.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
?xmlns:aop="http://www.springframework.org/schema/aop"
?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.0.xsd
????? ?http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
?????????? ?http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd?????????
???http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


?<!-- ==================================================== -->
?<!-- 配置多资源文件 -->
?<!-- ==================================================== -->
?<bean id="propertyConfigurer"
??/>
?</aop:config>
?<tx:advice id="txAdvice">
??<tx:attributes>
???<tx:method name="insert*" />
???<tx:method name="save*" />
???<tx:method name="create*" />
???<tx:method name="add*" />
???<tx:method name="update*" />
???<tx:method name="modify*" />
???<tx:method name="delete*" />
???<tx:method name="remove*" />
???<tx:method name="import*" />
???<tx:method name="export*" />
???<tx:method name="execute*" />
???<tx:method name="*" read-only="true" />
??</tx:attributes>
?</tx:advice>


?<!-- ==================================================== -->
?<!-- 系统数据访问层公共模块配置文件 (包括:增、删、改、查) -->
?<!-- ==================================================== -->
?<bean id="dao" ref="sqlMapClient" />
?</bean>

?<!-- ==================================================== -->
?<!-- 系统服务层公共模块配置文件 (包括:增、删、改、查) -->
?<!-- ==================================================== -->
?<bean id="service"
??ref="dao" />
?</bean>
?
?
?<!-- ==================================================== -->
?<!-- 导入系统配置文件模块配置文件 (包括:增、删、改、查) -->
?<!-- ==================================================== -->
?<import resource="classpath:application-core.xml" />
?<!--这样写的好处是不会将所有的文件统一的放到applicationContext中,而是分开放。增强代码的可读-->
</beans>

第二个spring配置文件为:
application-core.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:dwr="http://www.directwebremoting.org/schema/spring-dwr"
?xmlns:aop="http://www.springframework.org/schema/aop"
?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.0.xsd
????? ?http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd
?????????? ?http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd?????????
???http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">


?<!-- ==================================================== -->
?<!-- 配置多资源文件 -->
?<!-- ==================================================== -->
?<bean id="propertyConfigurer"
??destroy-method="close">
??<property name="driverClassName" value="${datasource.driverClassName}"/>
??<property name="url" value="${datasource.url}"/>
??<property name="username" value="${datasource.username}"/>
??<property name="password" value="${datasource.password}"/>
??<property name="maxActive" value="${datasource.maxActive}" />
?</bean>
?<bean id="sqlMapClient" ref="dataSource"/>
??<property name="lobHandler" ref="lobHandler" />??
?</bean>
?<bean id="transactionManager" ref="dataSource" />
?</bean>
?<bean id="lobHandler" lazy-init="true"
??/>
?
?
?<!-- ==================================================== -->
?<!-- 系统审查日志公共模块配置文件 (包括:增、删、改、查) -->
?<!-- ==================================================== -->
?<bean id="auditLogger" value="auditLogger" />
?</bean>

?
?<!-- ==================================================== -->
?<!-- 系统日志输出公共模块配置文件 (包括:增、删、改、查) -->
?<!-- ==================================================== -->
?<bean id="errorLogger" value="errorLogger" />
?</bean>
?
?
</beans>


7然后写基层的接口,和service,最后调用,基层接口和service我已经放到了spring的配置文件中。可以不用配置了。

热点排行