OR Mapping的思想相信不用多说大家都明白了,在这里我选择的是ibatis由于手动的控制事务会带来很多额外的工作,同时也没有很好的体现面向对象的思想,因而利用ibatis整合spring ;由于要注意的细节非常多现在整理核心步骤如下:
配置前需把ibatis的jar导入到工程,这里从略
[b]一 web.xml的配置[/b]
[code]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>InserttoDBServlet</servlet-name>
<servlet-class>service.InserttoDBServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InserttoDBServlet</servlet-name>
<url-pattern>/InserttoDBServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>ExtFilter</filter-name>
<filter-class>filter.ExtFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExtFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
[/code]
[b]二 applicationContext.xml文件的配置[/b]
[code]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_oracle" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="1" />
<property name="maxActive" value="4" />
</bean>
<bean id="dataSource_oracle"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.100.235:1521:mpptest" />
<property name="username" value="gmcc" />
<property name="password" value="skywin" />
<property name="initialSize" value="1" />
<property name="maxActive" value="4" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource_oracle" />
</bean>
<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="studentDao" class="dao.StudentDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="studentServiceProxy" parent="baseTxService">
<property name="target" ref="studentService"></property>
</bean>
<bean id="treeService" class="service.TreeServiceImpl_map">
<property name="treeDao" ref="treeDao"></property>
</bean>
<bean id="treeServiceProxy" parent="baseTxService">
<property name="target" ref="treeService"></property>
</bean>
<bean id="treeDao" class="dao.TreeDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="xmlTreeService" class="service.XmlTreeServiceImpl"></bean>
</beans>
[/code]
[b]三 sqlmap-config.xml 文件配置[/b]
[code]
<!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="student_oracle.xml" />
<sqlMap resource="treeNode_oracle.xml" />
</sqlMapConfig>
[/code]
[b]四 ormpping文件的配置(student_oracle.xml等 )[/b]
[code]
<?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">
<sqlMap namespace="Student_oracle">
<resultMap id="result"
class="entity.Student">
<result property="stuId" column="id"
columnIndex="1" />
<result property="name" column="stuname"
columnIndex="2" />
<result property="password" column="stupassword" columnIndex="3" />
</resultMap>
<insert id="insertToDb">
insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)
</insert>
<insert id="insertTest">
<selectKey resultClass="Integer" keyProperty="stuId">
SELECT test_qjk_stu_seq.nextval FROM DUAL
</selectKey>
insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)
</insert>
</sqlMap>
[/code]
[b]五 实体bean代码[/b]
[code]
package entity;
import java.util.Date;
public class Student {
private Integer stuId;
private String name;
private Date birthday;
private String sex;
private String passport;
private byte[] password;
public byte[] getPassword() {
return password;
}
public void setPassword(byte[] password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassport() {
return passport;
}
public void setPassport(String passport) {
this.passport = passport;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String toString(){
return "stuid:"+stuId+"---name:"+name+"---passport:"+passport+"-----sex:"+sex+"--birthday:"+birthday;
}
}
[/code]
[b]六 dao实现类[/b]
[code]
package dao;
import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import util.Print;
import entity.Student;
public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {
public void insertToDb(Student stu) {
getSqlMapClientTemplate().insert("insertToDb", stu);
}
public Student queryStuById(Integer id) {
Student stu=(Student) getSqlMapClientTemplate().queryForObject("queryStuById", id);
Print.contrlPrint("dao查到的对象是"+stu);
return stu;
}
public List getSomeStu(int start, int pageSize) {
System.out.println("dao ---start"+start);
System.out.println("dao ---pageSize"+pageSize);
List list=getSqlMapClientTemplate().queryForList("queryStudent", null, start, pageSize);
System.out.println("dao---list---size"+list.size());
return list;
}
public Long getStudentCount() {
return (Long) getSqlMapClientTemplate().queryForObject("getStudentCount", null);
}
public void delStuById(Long stuId) {
getSqlMapClientTemplate().delete("delStuById", stuId);
}
public void updateStudent(Student stu) {
getSqlMapClientTemplate().update("updateStudent", stu);
}
public List test(){
return getSqlMapClientTemplate().queryForList("test",null);
}
public Integer insertTest(Student stu) {
Print.contrlPrint("调用了insertTest");
return (Integer) getSqlMapClientTemplate().insert("insertTest", stu);
}
}
[/code]
[b]七 业务层实现类[/b]
[code]
package service;
import java.util.List;
import util.Tools;
import dao.StudentDao;
import entity.Student;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void insertToDb(Student stu) {
studentDao.insertToDb(stu);
// Integer id=studentDao.insertTest(stu);
// System.out.println("插入记录的id是"+id);
}
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student queryById(Integer id) {
return studentDao.queryStuById(id);
}
public List getSomeStu(int start, int pageSize) {
return studentDao.getSomeStu(start, pageSize);
}
public String getResponseJson(int start, int pageSize) {
List list=getSomeStu(start, pageSize);
Long count=getStudentCount();
return Tools.getResponseJson(list,count);
}
public Long getStudentCount() {
return studentDao.getStudentCount();
}
public void delStuById(Long stuId) {
studentDao.delStuById(stuId);
}
public void updateStudent(Student stu) {
studentDao.updateStudent(stu);
}
}
[/code]
配置前需把ibatis的jar导入到工程,这里从略
[b]一 web.xml的配置[/b]
[code]
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>InserttoDBServlet</servlet-name>
<servlet-class>service.InserttoDBServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>InserttoDBServlet</servlet-name>
<url-pattern>/InserttoDBServlet</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>TestServlet</servlet-name>
<servlet-class>test.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>TestServlet</servlet-name>
<url-pattern>/servlet/TestServlet</url-pattern>
</servlet-mapping>
<filter>
<filter-name>ExtFilter</filter-name>
<filter-class>filter.ExtFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExtFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
[/code]
[b]二 applicationContext.xml文件的配置[/b]
[code]
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource_oracle" />
</bean>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="root" />
<property name="initialSize" value="1" />
<property name="maxActive" value="4" />
</bean>
<bean id="dataSource_oracle"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="oracle.jdbc.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@192.168.100.235:1521:mpptest" />
<property name="username" value="gmcc" />
<property name="password" value="skywin" />
<property name="initialSize" value="1" />
<property name="maxActive" value="4" />
</bean>
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:sqlmap-config.xml" />
<property name="dataSource" ref="dataSource_oracle" />
</bean>
<bean id="baseTxService"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
abstract="true">
<property name="transactionManager" ref="transactionManager" />
<property name="proxyTargetClass" value="true" />
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="query*">readOnly</prop>
<prop key="get*">readOnly</prop>
<prop key="del*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="studentDao" class="dao.StudentDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studentDao" ref="studentDao"></property>
</bean>
<bean id="studentServiceProxy" parent="baseTxService">
<property name="target" ref="studentService"></property>
</bean>
<bean id="treeService" class="service.TreeServiceImpl_map">
<property name="treeDao" ref="treeDao"></property>
</bean>
<bean id="treeServiceProxy" parent="baseTxService">
<property name="target" ref="treeService"></property>
</bean>
<bean id="treeDao" class="dao.TreeDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient"></property>
</bean>
<bean id="xmlTreeService" class="service.XmlTreeServiceImpl"></bean>
</beans>
[/code]
[b]三 sqlmap-config.xml 文件配置[/b]
[code]
<!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="student_oracle.xml" />
<sqlMap resource="treeNode_oracle.xml" />
</sqlMapConfig>
[/code]
[b]四 ormpping文件的配置(student_oracle.xml等 )[/b]
[code]
<?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">
<sqlMap namespace="Student_oracle">
<resultMap id="result"
class="entity.Student">
<result property="stuId" column="id"
columnIndex="1" />
<result property="name" column="stuname"
columnIndex="2" />
<result property="password" column="stupassword" columnIndex="3" />
</resultMap>
<insert id="insertToDb">
insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)
</insert>
<insert id="insertTest">
<selectKey resultClass="Integer" keyProperty="stuId">
SELECT test_qjk_stu_seq.nextval FROM DUAL
</selectKey>
insert into test_qjk_stu(id,stuname,stupassword)values(#stuId#,#name#,#password#)
</insert>
</sqlMap>
[/code]
[b]五 实体bean代码[/b]
[code]
package entity;
import java.util.Date;
public class Student {
private Integer stuId;
private String name;
private Date birthday;
private String sex;
private String passport;
private byte[] password;
public byte[] getPassword() {
return password;
}
public void setPassword(byte[] password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassport() {
return passport;
}
public void setPassport(String passport) {
this.passport = passport;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getStuId() {
return stuId;
}
public void setStuId(Integer stuId) {
this.stuId = stuId;
}
public String toString(){
return "stuid:"+stuId+"---name:"+name+"---passport:"+passport+"-----sex:"+sex+"--birthday:"+birthday;
}
}
[/code]
[b]六 dao实现类[/b]
[code]
package dao;
import java.sql.SQLException;
import java.util.List;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import util.Print;
import entity.Student;
public class StudentDaoImpl extends SqlMapClientDaoSupport implements StudentDao {
public void insertToDb(Student stu) {
getSqlMapClientTemplate().insert("insertToDb", stu);
}
public Student queryStuById(Integer id) {
Student stu=(Student) getSqlMapClientTemplate().queryForObject("queryStuById", id);
Print.contrlPrint("dao查到的对象是"+stu);
return stu;
}
public List getSomeStu(int start, int pageSize) {
System.out.println("dao ---start"+start);
System.out.println("dao ---pageSize"+pageSize);
List list=getSqlMapClientTemplate().queryForList("queryStudent", null, start, pageSize);
System.out.println("dao---list---size"+list.size());
return list;
}
public Long getStudentCount() {
return (Long) getSqlMapClientTemplate().queryForObject("getStudentCount", null);
}
public void delStuById(Long stuId) {
getSqlMapClientTemplate().delete("delStuById", stuId);
}
public void updateStudent(Student stu) {
getSqlMapClientTemplate().update("updateStudent", stu);
}
public List test(){
return getSqlMapClientTemplate().queryForList("test",null);
}
public Integer insertTest(Student stu) {
Print.contrlPrint("调用了insertTest");
return (Integer) getSqlMapClientTemplate().insert("insertTest", stu);
}
}
[/code]
[b]七 业务层实现类[/b]
[code]
package service;
import java.util.List;
import util.Tools;
import dao.StudentDao;
import entity.Student;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao;
public void insertToDb(Student stu) {
studentDao.insertToDb(stu);
// Integer id=studentDao.insertTest(stu);
// System.out.println("插入记录的id是"+id);
}
public StudentDao getStudentDao() {
return studentDao;
}
public void setStudentDao(StudentDao studentDao) {
this.studentDao = studentDao;
}
public Student queryById(Integer id) {
return studentDao.queryStuById(id);
}
public List getSomeStu(int start, int pageSize) {
return studentDao.getSomeStu(start, pageSize);
}
public String getResponseJson(int start, int pageSize) {
List list=getSomeStu(start, pageSize);
Long count=getStudentCount();
return Tools.getResponseJson(list,count);
}
public Long getStudentCount() {
return studentDao.getStudentCount();
}
public void delStuById(Long stuId) {
studentDao.delStuById(stuId);
}
public void updateStudent(Student stu) {
studentDao.updateStudent(stu);
}
}
[/code]