- 浏览: 170261 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
boz.lee:
不错, 好多地方因为这里而出错
全局变量和局部变量在内存里的区别 -
issllx:
看到此文后对iteye深表遗憾
Yahoo S4 -
wohenshuaiba:
请问你有这个的代码吗?
k均值聚类(K-means) -
yxwang0615:
大神:
apr-1.3.3.tar.bz2
apr-util- ...
log4cxx编译方法 -
yxwang0615:
比csdn上的说的清楚干练,早看到这个就好了,我编了一天
p ...
log4cxx编译方法
文章分为2大部分,第1部分谈Spring对ibatis的整合,第2部分谈谈Spring事务管理,第一部分成功实现了,第二部分在实现的时候出现了问题,希望各位高手能多多指点。
一 spring整合ibatis
1 创建ibatis功能类
由于ibatis是基于半自动ORM框架的,对于每个DAO都需要手动编写功能类和xml配置文件,写起来是很费事的。所以,我们选用ibatis的代码自动生成器去生成具体的功能类。
下面是用代码自动生成器生成的功能类:
abatorConfig.xml文件,这个配置文件是ibatis的代码自动生成器需要配置的文件。
Java代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://localhost:1433;databaseName=mydb" userId="sa" password="841026" >
<classPathEntry location="F:/Program Files/workspace/SpringTranslateDemo/driver/sqljdbc.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.whpu.computershop.ibatis.pojo" targetProject="SpringTranslateDemo" />
<sqlMapGenerator targetPackage="com.whpu.upanddown.ibatis.config" targetProject="SpringTranslateDemo" />
<daoGenerator targetPackage="com.whpu.upanddown.ibatis.dao" targetProject="SpringTranslateDemo" type="GENERIC-CI" />
<table schema="dbo" tableName="student" >
</table>
</abatorContext>
</abatorConfiguration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://localhost:1433;databaseName=mydb" userId="sa" password="841026" >
<classPathEntry location="F:/Program Files/workspace/SpringTranslateDemo/driver/sqljdbc.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.whpu.computershop.ibatis.pojo" targetProject="SpringTranslateDemo" />
<sqlMapGenerator targetPackage="com.whpu.upanddown.ibatis.config" targetProject="SpringTranslateDemo" />
<daoGenerator targetPackage="com.whpu.upanddown.ibatis.dao" targetProject="SpringTranslateDemo" type="GENERIC-CI" />
<table schema="dbo" tableName="student" >
</table>
</abatorContext>
</abatorConfiguration>
自动生成的pojo:
package com.whpu.computershop.ibatis.pojo;
Java代码
public class Student {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuid;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuname;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stusex;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuage;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuclass;
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuid
*
* @return the value of dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuid() {
return stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuid
*
* @param stuid the value for dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuname
*
* @return the value of dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuname() {
return stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuname
*
* @param stuname the value for dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuname(String stuname) {
this.stuname = stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stusex
*
* @return the value of dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStusex() {
return stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stusex
*
* @param stusex the value for dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStusex(String stusex) {
this.stusex = stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuage
*
* @return the value of dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuage() {
return stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuage
*
* @param stuage the value for dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuclass
*
* @return the value of dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuclass() {
return stuclass;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuclass
*
* @param stuclass the value for dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuclass(String stuclass) {
this.stuclass = stuclass;
}
}
public class Student {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuid;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuname;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stusex;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuage;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuclass;
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuid
*
* @return the value of dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuid() {
return stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuid
*
* @param stuid the value for dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuname
*
* @return the value of dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuname() {
return stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuname
*
* @param stuname the value for dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuname(String stuname) {
this.stuname = stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stusex
*
* @return the value of dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStusex() {
return stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stusex
*
* @param stusex the value for dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStusex(String stusex) {
this.stusex = stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuage
*
* @return the value of dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuage() {
return stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuage
*
* @param stuage the value for dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuclass
*
* @return the value of dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuclass() {
return stuclass;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuclass
*
* @param stuclass the value for dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuclass(String stuclass) {
this.stuclass = stuclass;
}
}
自动生成的studao接口和studaoimpl实现类:
Java代码
package com.whpu.upanddown.ibatis.dao;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public interface StudentDAO {
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
void insert(Student record) ;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKey(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKeySelective(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
List selectByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
Student selectByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int countByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExampleSelective(Student record, StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExample(Student record, StudentExample example) throws SQLException;
List selectByname(String name) throws SQLException;
List selectByclassname(String classname) throws SQLException;
}
package com.whpu.upanddown.ibatis.dao;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public interface StudentDAO {
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
void insert(Student record) ;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKey(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKeySelective(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
List selectByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
Student selectByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int countByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExampleSelective(Student record, StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExample(Student record, StudentExample example) throws SQLException;
List selectByname(String name) throws SQLException;
List selectByclassname(String classname) throws SQLException;
}
Java代码
package com.whpu.upanddown.ibatis.dao;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public class StudentDAOImpl implements StudentDAO {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private SqlMapClient sqlMapClient;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public StudentDAOImpl(SqlMapClient sqlMapClient) {
super();
this.sqlMapClient = sqlMapClient;
}
public StudentDAOImpl(){
super();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void insert(Student record) {
try {
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKey(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKey", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKeySelective(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKeySelective", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public List selectByExample(StudentExample example) throws SQLException {
List list = sqlMapClient.queryForList("dbo_student.abatorgenerated_selectByExample", example);
return list;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Student selectByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
Student record = (Student) sqlMapClient.queryForObject("dbo_student.abatorgenerated_selectByPrimaryKey", key);
return record;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByExample(StudentExample example) throws SQLException {
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByExample", example);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByPrimaryKey", key);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int countByExample(StudentExample example) throws SQLException {
Integer count = (Integer) sqlMapClient.queryForObject("dbo_student.abatorgenerated_countByExample", example);
return count.intValue();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExampleSelective(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExampleSelective", parms);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExample(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExample", parms);
return rows;
}
/**
* This class was generated by Abator for iBATIS.
* This class corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private static class UpdateByExampleParms extends StudentExample {
private Object record;
public UpdateByExampleParms(Object record, StudentExample example) {
super(example);
this.record = record;
}
public Object getRecord() {
return record;
}
}
public List selectByname(String stuname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbystuname", stuname);
return list;
}
public List selectByclassname(String classname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbyclassname", classname);
return list;
}
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}
package com.whpu.upanddown.ibatis.dao;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public class StudentDAOImpl implements StudentDAO {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private SqlMapClient sqlMapClient;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public StudentDAOImpl(SqlMapClient sqlMapClient) {
super();
this.sqlMapClient = sqlMapClient;
}
public StudentDAOImpl(){
super();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void insert(Student record) {
try {
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKey(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKey", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKeySelective(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKeySelective", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public List selectByExample(StudentExample example) throws SQLException {
List list = sqlMapClient.queryForList("dbo_student.abatorgenerated_selectByExample", example);
return list;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Student selectByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
Student record = (Student) sqlMapClient.queryForObject("dbo_student.abatorgenerated_selectByPrimaryKey", key);
return record;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByExample(StudentExample example) throws SQLException {
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByExample", example);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByPrimaryKey", key);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int countByExample(StudentExample example) throws SQLException {
Integer count = (Integer) sqlMapClient.queryForObject("dbo_student.abatorgenerated_countByExample", example);
return count.intValue();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExampleSelective(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExampleSelective", parms);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExample(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExample", parms);
return rows;
}
/**
* This class was generated by Abator for iBATIS.
* This class corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private static class UpdateByExampleParms extends StudentExample {
private Object record;
public UpdateByExampleParms(Object record, StudentExample example) {
super(example);
this.record = record;
}
public Object getRecord() {
return record;
}
}
public List selectByname(String stuname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbystuname", stuname);
return list;
}
public List selectByclassname(String classname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbyclassname", classname);
return list;
}
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}
自动生成的studao配置文件,当然SqlMapConfig.xml文件需要手动去编写的。
Java代码
<?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="dbo_student" >
<resultMap id="abatorgenerated_StudentResult" class="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<result column="stuid" property="stuid" jdbcType="INTEGER" />
<result column="stuname" property="stuname" jdbcType="VARCHAR" />
<result column="stusex" property="stusex" jdbcType="VARCHAR" />
<result column="stuage" property="stuage" jdbcType="INTEGER" />
<result column="stuclass" property="stuclass" jdbcType="VARCHAR" />
</resultMap>
<sql id="abatorgenerated_Example_Where_Clause" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true" >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
<select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
where stuid = #stuid:INTEGER#
</select>
<select id="abatorgenerated_selectByExample" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
<isNotNull property="orderByClause" >
order by $orderByClause$
</isNotNull>
</isParameterPresent>
</select>
<delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
where stuid = #stuid:INTEGER#
</delete>
<delete id="abatorgenerated_deleteByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</delete>
<insert id="abatorgenerated_insert" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
insert into dbo.student (stuid, stuname, stusex, stuage, stuclass)
values (#stuid:INTEGER#, #stuname:VARCHAR#, #stusex:VARCHAR#, #stuage:INTEGER#,
#stuclass:VARCHAR#)
</insert>
<update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuname = #stuname:VARCHAR#,
stusex = #stusex:VARCHAR#,
stuage = #stuage:INTEGER#,
stuclass = #stuclass:VARCHAR#
where stuid = #stuid:INTEGER#
</update>
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="stuname" >
stuname = #stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stusex" >
stusex = #stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stuage" >
stuage = #stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="stuclass" >
stuclass = #stuclass:VARCHAR#
</isNotNull>
</dynamic>
where stuid = #stuid:INTEGER#
</update>
<select id="abatorgenerated_countByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" resultClass="java.lang.Integer" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select count(*) from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</select>
<update id="abatorgenerated_updateByExampleSelective" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="record.stuid" >
stuid = #record.stuid:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuname" >
stuname = #record.stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stusex" >
stusex = #record.stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stuage" >
stuage = #record.stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuclass" >
stuclass = #record.stuclass:VARCHAR#
</isNotNull>
</dynamic>
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<update id="abatorgenerated_updateByExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuid = #record.stuid:INTEGER#,
stuname = #record.stuname:VARCHAR#,
stusex = #record.stusex:VARCHAR#,
stuage = #record.stuage:INTEGER#,
stuclass = #record.stuclass:VARCHAR#
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<select id="selectbystuname" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select stuid, stuname, stusex, stuage, stuclass
from dbo.student where stuname=#name#
</select>
<select id="selectbystuclass" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select * from dbo.student as stu where stu.stuclass=#stuclass#
</select>
</sqlMap>
<?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="dbo_student" >
<resultMap id="abatorgenerated_StudentResult" class="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<result column="stuid" property="stuid" jdbcType="INTEGER" />
<result column="stuname" property="stuname" jdbcType="VARCHAR" />
<result column="stusex" property="stusex" jdbcType="VARCHAR" />
<result column="stuage" property="stuage" jdbcType="INTEGER" />
<result column="stuclass" property="stuclass" jdbcType="VARCHAR" />
</resultMap>
<sql id="abatorgenerated_Example_Where_Clause" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true" >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
<select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
where stuid = #stuid:INTEGER#
</select>
<select id="abatorgenerated_selectByExample" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
<isNotNull property="orderByClause" >
order by $orderByClause$
</isNotNull>
</isParameterPresent>
</select>
<delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
where stuid = #stuid:INTEGER#
</delete>
<delete id="abatorgenerated_deleteByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</delete>
<insert id="abatorgenerated_insert" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
insert into dbo.student (stuid, stuname, stusex, stuage, stuclass)
values (#stuid:INTEGER#, #stuname:VARCHAR#, #stusex:VARCHAR#, #stuage:INTEGER#,
#stuclass:VARCHAR#)
</insert>
<update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuname = #stuname:VARCHAR#,
stusex = #stusex:VARCHAR#,
stuage = #stuage:INTEGER#,
stuclass = #stuclass:VARCHAR#
where stuid = #stuid:INTEGER#
</update>
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="stuname" >
stuname = #stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stusex" >
stusex = #stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stuage" >
stuage = #stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="stuclass" >
stuclass = #stuclass:VARCHAR#
</isNotNull>
</dynamic>
where stuid = #stuid:INTEGER#
</update>
<select id="abatorgenerated_countByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" resultClass="java.lang.Integer" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select count(*) from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</select>
<update id="abatorgenerated_updateByExampleSelective" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="record.stuid" >
stuid = #record.stuid:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuname" >
stuname = #record.stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stusex" >
stusex = #record.stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stuage" >
stuage = #record.stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuclass" >
stuclass = #record.stuclass:VARCHAR#
</isNotNull>
</dynamic>
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<update id="abatorgenerated_updateByExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuid = #record.stuid:INTEGER#,
stuname = #record.stuname:VARCHAR#,
stusex = #record.stusex:VARCHAR#,
stuage = #record.stuage:INTEGER#,
stuclass = #record.stuclass:VARCHAR#
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<select id="selectbystuname" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select stuid, stuname, stusex, stuage, stuclass
from dbo.student where stuname=#name#
</select>
<select id="selectbystuclass" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select * from dbo.student as stu where stu.stuclass=#stuclass#
</select>
</sqlMap>
配置spring的配置文件去整合IBatis的功能类
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<!--
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<!--
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
上面注释掉的部分是spring的事务管理,本来想用spring去控制ibatis的事务管理,但没用成功,希望各位高手能看看,问题出在哪里?
测试类:
Java代码
package com.hjy.SpringTranslateDemo.Test;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.upanddown.ibatis.dao.StudentDAO;
import com.whpu.upanddown.ibatis.dao.StudentDAOImpl;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlMapClient sqlmap = (SqlMapClient) ac.getBean("sqlMapClient");
// StudentDAO studao = (StudentDAO) ac.getBean("studao");
// studentdaoimpl
StudentDAO studao = (StudentDAO) ac.getBean("studao");
List list;
try {
list = studao.selectByname("张三");
Student stu = (Student) list.iterator().next();
System.out.println(stu.getStuname());
System.out.println(stu.getStusex());
System.out.println(stu.getStuclass());
System.out.println(stu.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.hjy.SpringTranslateDemo.Test;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.upanddown.ibatis.dao.StudentDAO;
import com.whpu.upanddown.ibatis.dao.StudentDAOImpl;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlMapClient sqlmap = (SqlMapClient) ac.getBean("sqlMapClient");
// StudentDAO studao = (StudentDAO) ac.getBean("studao");
// studentdaoimpl
StudentDAO studao = (StudentDAO) ac.getBean("studao");
List list;
try {
list = studao.selectByname("张三");
Student stu = (Student) list.iterator().next();
System.out.println(stu.getStuname());
System.out.println(stu.getStusex());
System.out.println(stu.getStuclass());
System.out.println(stu.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
下面是测试结果:
张三
男
1班
20
二 Spring事务管理
Spring事务管理可以让我们的事务在失败时能自动回滚,Spring的事务管理主要有两种实现方式,编程式和声明式。对于编程式,可以用统一的模版去处理事务,但个人感觉违背了侵入式的编程思想。声明式是很现在用的最多的事务处理方式,只需要配置spring的配置文件就可以了,很方便。spring2.x和spring1.x的配置方式是不一样的。
我用的是spring1.x的配置方式,但在实现时出了问题,也不知道为什么,希望高手指出。
Spring application.xml文件
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name
一 spring整合ibatis
1 创建ibatis功能类
由于ibatis是基于半自动ORM框架的,对于每个DAO都需要手动编写功能类和xml配置文件,写起来是很费事的。所以,我们选用ibatis的代码自动生成器去生成具体的功能类。
下面是用代码自动生成器生成的功能类:
abatorConfig.xml文件,这个配置文件是ibatis的代码自动生成器需要配置的文件。
Java代码
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://localhost:1433;databaseName=mydb" userId="sa" password="841026" >
<classPathEntry location="F:/Program Files/workspace/SpringTranslateDemo/driver/sqljdbc.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.whpu.computershop.ibatis.pojo" targetProject="SpringTranslateDemo" />
<sqlMapGenerator targetPackage="com.whpu.upanddown.ibatis.config" targetProject="SpringTranslateDemo" />
<daoGenerator targetPackage="com.whpu.upanddown.ibatis.dao" targetProject="SpringTranslateDemo" type="GENERIC-CI" />
<table schema="dbo" tableName="student" >
</table>
</abatorContext>
</abatorConfiguration>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE abatorConfiguration PUBLIC "-//Apache Software Foundation//DTD Abator for iBATIS Configuration 1.0//EN" "http://ibatis.apache.org/dtd/abator-config_1_0.dtd" >
<abatorConfiguration >
<abatorContext >
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver" connectionURL="jdbc:sqlserver://localhost:1433;databaseName=mydb" userId="sa" password="841026" >
<classPathEntry location="F:/Program Files/workspace/SpringTranslateDemo/driver/sqljdbc.jar" />
</jdbcConnection>
<javaModelGenerator targetPackage="com.whpu.computershop.ibatis.pojo" targetProject="SpringTranslateDemo" />
<sqlMapGenerator targetPackage="com.whpu.upanddown.ibatis.config" targetProject="SpringTranslateDemo" />
<daoGenerator targetPackage="com.whpu.upanddown.ibatis.dao" targetProject="SpringTranslateDemo" type="GENERIC-CI" />
<table schema="dbo" tableName="student" >
</table>
</abatorContext>
</abatorConfiguration>
自动生成的pojo:
package com.whpu.computershop.ibatis.pojo;
Java代码
public class Student {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuid;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuname;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stusex;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuage;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuclass;
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuid
*
* @return the value of dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuid() {
return stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuid
*
* @param stuid the value for dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuname
*
* @return the value of dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuname() {
return stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuname
*
* @param stuname the value for dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuname(String stuname) {
this.stuname = stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stusex
*
* @return the value of dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStusex() {
return stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stusex
*
* @param stusex the value for dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStusex(String stusex) {
this.stusex = stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuage
*
* @return the value of dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuage() {
return stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuage
*
* @param stuage the value for dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuclass
*
* @return the value of dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuclass() {
return stuclass;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuclass
*
* @param stuclass the value for dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuclass(String stuclass) {
this.stuclass = stuclass;
}
}
public class Student {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuid;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuname;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stusex;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private Integer stuage;
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database column dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private String stuclass;
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuid
*
* @return the value of dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuid() {
return stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuid
*
* @param stuid the value for dbo.student.stuid
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuid(Integer stuid) {
this.stuid = stuid;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuname
*
* @return the value of dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuname() {
return stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuname
*
* @param stuname the value for dbo.student.stuname
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuname(String stuname) {
this.stuname = stuname;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stusex
*
* @return the value of dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStusex() {
return stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stusex
*
* @param stusex the value for dbo.student.stusex
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStusex(String stusex) {
this.stusex = stusex;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuage
*
* @return the value of dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Integer getStuage() {
return stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuage
*
* @param stuage the value for dbo.student.stuage
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuage(Integer stuage) {
this.stuage = stuage;
}
/**
* This method was generated by Abator for iBATIS.
* This method returns the value of the database column dbo.student.stuclass
*
* @return the value of dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public String getStuclass() {
return stuclass;
}
/**
* This method was generated by Abator for iBATIS.
* This method sets the value of the database column dbo.student.stuclass
*
* @param stuclass the value for dbo.student.stuclass
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void setStuclass(String stuclass) {
this.stuclass = stuclass;
}
}
自动生成的studao接口和studaoimpl实现类:
Java代码
package com.whpu.upanddown.ibatis.dao;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public interface StudentDAO {
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
void insert(Student record) ;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKey(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKeySelective(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
List selectByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
Student selectByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int countByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExampleSelective(Student record, StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExample(Student record, StudentExample example) throws SQLException;
List selectByname(String name) throws SQLException;
List selectByclassname(String classname) throws SQLException;
}
package com.whpu.upanddown.ibatis.dao;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public interface StudentDAO {
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
void insert(Student record) ;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKey(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByPrimaryKeySelective(Student record) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
List selectByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
Student selectByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int deleteByPrimaryKey(Integer stuid) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int countByExample(StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExampleSelective(Student record, StudentExample example) throws SQLException;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
int updateByExample(Student record, StudentExample example) throws SQLException;
List selectByname(String name) throws SQLException;
List selectByclassname(String classname) throws SQLException;
}
Java代码
package com.whpu.upanddown.ibatis.dao;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public class StudentDAOImpl implements StudentDAO {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private SqlMapClient sqlMapClient;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public StudentDAOImpl(SqlMapClient sqlMapClient) {
super();
this.sqlMapClient = sqlMapClient;
}
public StudentDAOImpl(){
super();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void insert(Student record) {
try {
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKey(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKey", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKeySelective(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKeySelective", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public List selectByExample(StudentExample example) throws SQLException {
List list = sqlMapClient.queryForList("dbo_student.abatorgenerated_selectByExample", example);
return list;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Student selectByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
Student record = (Student) sqlMapClient.queryForObject("dbo_student.abatorgenerated_selectByPrimaryKey", key);
return record;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByExample(StudentExample example) throws SQLException {
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByExample", example);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByPrimaryKey", key);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int countByExample(StudentExample example) throws SQLException {
Integer count = (Integer) sqlMapClient.queryForObject("dbo_student.abatorgenerated_countByExample", example);
return count.intValue();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExampleSelective(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExampleSelective", parms);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExample(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExample", parms);
return rows;
}
/**
* This class was generated by Abator for iBATIS.
* This class corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private static class UpdateByExampleParms extends StudentExample {
private Object record;
public UpdateByExampleParms(Object record, StudentExample example) {
super(example);
this.record = record;
}
public Object getRecord() {
return record;
}
}
public List selectByname(String stuname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbystuname", stuname);
return list;
}
public List selectByclassname(String classname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbyclassname", classname);
return list;
}
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}
package com.whpu.upanddown.ibatis.dao;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.computershop.ibatis.pojo.StudentExample;
import java.sql.SQLException;
import java.util.List;
public class StudentDAOImpl implements StudentDAO {
/**
* This field was generated by Abator for iBATIS.
* This field corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private SqlMapClient sqlMapClient;
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public StudentDAOImpl(SqlMapClient sqlMapClient) {
super();
this.sqlMapClient = sqlMapClient;
}
public StudentDAOImpl(){
super();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public void insert(Student record) {
try {
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
sqlMapClient.insert("dbo_student.abatorgenerated_insert", record);
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKey(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKey", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByPrimaryKeySelective(Student record) throws SQLException {
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByPrimaryKeySelective", record);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public List selectByExample(StudentExample example) throws SQLException {
List list = sqlMapClient.queryForList("dbo_student.abatorgenerated_selectByExample", example);
return list;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public Student selectByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
Student record = (Student) sqlMapClient.queryForObject("dbo_student.abatorgenerated_selectByPrimaryKey", key);
return record;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByExample(StudentExample example) throws SQLException {
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByExample", example);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int deleteByPrimaryKey(Integer stuid) throws SQLException {
Student key = new Student();
key.setStuid(stuid);
int rows = sqlMapClient.delete("dbo_student.abatorgenerated_deleteByPrimaryKey", key);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int countByExample(StudentExample example) throws SQLException {
Integer count = (Integer) sqlMapClient.queryForObject("dbo_student.abatorgenerated_countByExample", example);
return count.intValue();
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExampleSelective(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExampleSelective", parms);
return rows;
}
/**
* This method was generated by Abator for iBATIS.
* This method corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
public int updateByExample(Student record, StudentExample example) throws SQLException {
UpdateByExampleParms parms = new UpdateByExampleParms(record, example);
int rows = sqlMapClient.update("dbo_student.abatorgenerated_updateByExample", parms);
return rows;
}
/**
* This class was generated by Abator for iBATIS.
* This class corresponds to the database table dbo.student
*
* @abatorgenerated Thu Feb 11 21:34:33 CST 2010
*/
private static class UpdateByExampleParms extends StudentExample {
private Object record;
public UpdateByExampleParms(Object record, StudentExample example) {
super(example);
this.record = record;
}
public Object getRecord() {
return record;
}
}
public List selectByname(String stuname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbystuname", stuname);
return list;
}
public List selectByclassname(String classname) throws SQLException
{
List list=sqlMapClient.queryForList("dbo_student.selectbyclassname", classname);
return list;
}
public void setSqlMapClient(SqlMapClient sqlMapClient) {
this.sqlMapClient = sqlMapClient;
}
}
自动生成的studao配置文件,当然SqlMapConfig.xml文件需要手动去编写的。
Java代码
<?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="dbo_student" >
<resultMap id="abatorgenerated_StudentResult" class="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<result column="stuid" property="stuid" jdbcType="INTEGER" />
<result column="stuname" property="stuname" jdbcType="VARCHAR" />
<result column="stusex" property="stusex" jdbcType="VARCHAR" />
<result column="stuage" property="stuage" jdbcType="INTEGER" />
<result column="stuclass" property="stuclass" jdbcType="VARCHAR" />
</resultMap>
<sql id="abatorgenerated_Example_Where_Clause" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true" >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
<select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
where stuid = #stuid:INTEGER#
</select>
<select id="abatorgenerated_selectByExample" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
<isNotNull property="orderByClause" >
order by $orderByClause$
</isNotNull>
</isParameterPresent>
</select>
<delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
where stuid = #stuid:INTEGER#
</delete>
<delete id="abatorgenerated_deleteByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</delete>
<insert id="abatorgenerated_insert" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
insert into dbo.student (stuid, stuname, stusex, stuage, stuclass)
values (#stuid:INTEGER#, #stuname:VARCHAR#, #stusex:VARCHAR#, #stuage:INTEGER#,
#stuclass:VARCHAR#)
</insert>
<update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuname = #stuname:VARCHAR#,
stusex = #stusex:VARCHAR#,
stuage = #stuage:INTEGER#,
stuclass = #stuclass:VARCHAR#
where stuid = #stuid:INTEGER#
</update>
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="stuname" >
stuname = #stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stusex" >
stusex = #stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stuage" >
stuage = #stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="stuclass" >
stuclass = #stuclass:VARCHAR#
</isNotNull>
</dynamic>
where stuid = #stuid:INTEGER#
</update>
<select id="abatorgenerated_countByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" resultClass="java.lang.Integer" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select count(*) from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</select>
<update id="abatorgenerated_updateByExampleSelective" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="record.stuid" >
stuid = #record.stuid:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuname" >
stuname = #record.stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stusex" >
stusex = #record.stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stuage" >
stuage = #record.stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuclass" >
stuclass = #record.stuclass:VARCHAR#
</isNotNull>
</dynamic>
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<update id="abatorgenerated_updateByExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuid = #record.stuid:INTEGER#,
stuname = #record.stuname:VARCHAR#,
stusex = #record.stusex:VARCHAR#,
stuage = #record.stuage:INTEGER#,
stuclass = #record.stuclass:VARCHAR#
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<select id="selectbystuname" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select stuid, stuname, stusex, stuage, stuclass
from dbo.student where stuname=#name#
</select>
<select id="selectbystuclass" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select * from dbo.student as stu where stu.stuclass=#stuclass#
</select>
</sqlMap>
<?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="dbo_student" >
<resultMap id="abatorgenerated_StudentResult" class="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<result column="stuid" property="stuid" jdbcType="INTEGER" />
<result column="stuname" property="stuname" jdbcType="VARCHAR" />
<result column="stusex" property="stusex" jdbcType="VARCHAR" />
<result column="stuage" property="stuage" jdbcType="INTEGER" />
<result column="stuclass" property="stuclass" jdbcType="VARCHAR" />
</resultMap>
<sql id="abatorgenerated_Example_Where_Clause" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
<iterate property="oredCriteria" conjunction="or" prepend="where" removeFirstPrepend="iterate" >
<isEqual property="oredCriteria[].valid" compareValue="true" >
(
<iterate prepend="and" property="oredCriteria[].criteriaWithoutValue" conjunction="and" >
$oredCriteria[].criteriaWithoutValue[]$
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithSingleValue" conjunction="and" >
$oredCriteria[].criteriaWithSingleValue[].condition$
#oredCriteria[].criteriaWithSingleValue[].value#
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithListValue" conjunction="and" >
$oredCriteria[].criteriaWithListValue[].condition$
<iterate property="oredCriteria[].criteriaWithListValue[].values" open="(" close=")" conjunction="," >
#oredCriteria[].criteriaWithListValue[].values[]#
</iterate>
</iterate>
<iterate prepend="and" property="oredCriteria[].criteriaWithBetweenValue" conjunction="and" >
$oredCriteria[].criteriaWithBetweenValue[].condition$
#oredCriteria[].criteriaWithBetweenValue[].values[0]# and
#oredCriteria[].criteriaWithBetweenValue[].values[1]#
</iterate>
)
</isEqual>
</iterate>
</sql>
<select id="abatorgenerated_selectByPrimaryKey" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
where stuid = #stuid:INTEGER#
</select>
<select id="abatorgenerated_selectByExample" resultMap="abatorgenerated_StudentResult" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select stuid, stuname, stusex, stuage, stuclass
from dbo.student
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
<isNotNull property="orderByClause" >
order by $orderByClause$
</isNotNull>
</isParameterPresent>
</select>
<delete id="abatorgenerated_deleteByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
where stuid = #stuid:INTEGER#
</delete>
<delete id="abatorgenerated_deleteByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
delete from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</delete>
<insert id="abatorgenerated_insert" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
insert into dbo.student (stuid, stuname, stusex, stuage, stuclass)
values (#stuid:INTEGER#, #stuname:VARCHAR#, #stusex:VARCHAR#, #stuage:INTEGER#,
#stuclass:VARCHAR#)
</insert>
<update id="abatorgenerated_updateByPrimaryKey" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuname = #stuname:VARCHAR#,
stusex = #stusex:VARCHAR#,
stuage = #stuage:INTEGER#,
stuclass = #stuclass:VARCHAR#
where stuid = #stuid:INTEGER#
</update>
<update id="abatorgenerated_updateByPrimaryKeySelective" parameterClass="com.whpu.computershop.ibatis.pojo.Student" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="stuname" >
stuname = #stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stusex" >
stusex = #stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="stuage" >
stuage = #stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="stuclass" >
stuclass = #stuclass:VARCHAR#
</isNotNull>
</dynamic>
where stuid = #stuid:INTEGER#
</update>
<select id="abatorgenerated_countByExample" parameterClass="com.whpu.computershop.ibatis.pojo.StudentExample" resultClass="java.lang.Integer" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
select count(*) from dbo.student
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</select>
<update id="abatorgenerated_updateByExampleSelective" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
<dynamic prepend="set" >
<isNotNull prepend="," property="record.stuid" >
stuid = #record.stuid:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuname" >
stuname = #record.stuname:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stusex" >
stusex = #record.stusex:VARCHAR#
</isNotNull>
<isNotNull prepend="," property="record.stuage" >
stuage = #record.stuage:INTEGER#
</isNotNull>
<isNotNull prepend="," property="record.stuclass" >
stuclass = #record.stuclass:VARCHAR#
</isNotNull>
</dynamic>
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<update id="abatorgenerated_updateByExample" >
<!--
WARNING - This element is automatically generated by Abator for iBATIS, do not modify.
This element was generated on Thu Feb 11 21:34:33 CST 2010.
-->
update dbo.student
set stuid = #record.stuid:INTEGER#,
stuname = #record.stuname:VARCHAR#,
stusex = #record.stusex:VARCHAR#,
stuage = #record.stuage:INTEGER#,
stuclass = #record.stuclass:VARCHAR#
<isParameterPresent >
<include refid="dbo_student.abatorgenerated_Example_Where_Clause" />
</isParameterPresent>
</update>
<select id="selectbystuname" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select stuid, stuname, stusex, stuage, stuclass
from dbo.student where stuname=#name#
</select>
<select id="selectbystuclass" resultMap="abatorgenerated_StudentResult" parameterClass="java.lang.String" >
select * from dbo.student as stu where stu.stuclass=#stuclass#
</select>
</sqlMap>
配置spring的配置文件去整合IBatis的功能类
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<!--
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<!--
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
-->
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
上面注释掉的部分是spring的事务管理,本来想用spring去控制ibatis的事务管理,但没用成功,希望各位高手能看看,问题出在哪里?
测试类:
Java代码
package com.hjy.SpringTranslateDemo.Test;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.upanddown.ibatis.dao.StudentDAO;
import com.whpu.upanddown.ibatis.dao.StudentDAOImpl;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlMapClient sqlmap = (SqlMapClient) ac.getBean("sqlMapClient");
// StudentDAO studao = (StudentDAO) ac.getBean("studao");
// studentdaoimpl
StudentDAO studao = (StudentDAO) ac.getBean("studao");
List list;
try {
list = studao.selectByname("张三");
Student stu = (Student) list.iterator().next();
System.out.println(stu.getStuname());
System.out.println(stu.getStusex());
System.out.println(stu.getStuclass());
System.out.println(stu.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
package com.hjy.SpringTranslateDemo.Test;
import java.sql.SQLException;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.whpu.computershop.ibatis.pojo.Student;
import com.whpu.upanddown.ibatis.dao.StudentDAO;
import com.whpu.upanddown.ibatis.dao.StudentDAOImpl;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
SqlMapClient sqlmap = (SqlMapClient) ac.getBean("sqlMapClient");
// StudentDAO studao = (StudentDAO) ac.getBean("studao");
// studentdaoimpl
StudentDAO studao = (StudentDAO) ac.getBean("studao");
List list;
try {
list = studao.selectByname("张三");
Student stu = (Student) list.iterator().next();
System.out.println(stu.getStuname());
System.out.println(stu.getStusex());
System.out.println(stu.getStuclass());
System.out.println(stu.getStuage());
} catch (SQLException e) {
e.printStackTrace();
}
}
}
下面是测试结果:
张三
男
1班
20
二 Spring事务管理
Spring事务管理可以让我们的事务在失败时能自动回滚,Spring的事务管理主要有两种实现方式,编程式和声明式。对于编程式,可以用统一的模版去处理事务,但个人感觉违背了侵入式的编程思想。声明式是很现在用的最多的事务处理方式,只需要配置spring的配置文件就可以了,很方便。spring2.x和spring1.x的配置方式是不一样的。
我用的是spring1.x的配置方式,但在实现时出了问题,也不知道为什么,希望高手指出。
Spring application.xml文件
Java代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<bean id="stu1" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="23"></property>
<property name="stuage" value="12"></property>
<property name="stuclass" value="1班"></property>
<property name="stusex" value="男"></property>
</bean>
<bean id="stu2" class="com.whpu.computershop.ibatis.pojo.Student">
<property name="stuid" value="1"></property>
<property name="stuage" value="13"></property>
<property name="stuclass" value="2班"></property>
<property name="stusex" value="女"></property>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用spring的DriverManagerDataSource类配置数据源 -->
<bean id="datasource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=mydb">
</property>
<property name="username" value="sa"></property>
<property name="password" value="841026"></property>
</bean>
<!-- 利用spring的SqlMapClientFactoryBean类生成sqlmapclient工厂-->
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref bean="datasource" />
</property>
<property name="configLocation"
value="com/hjy/SpringTranslateDemo/core/SqlMapConfig.xml">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource">
<ref bean="datasource" />
</property>
</bean>
<bean id="studentdaoimpl"
class="com.whpu.upanddown.ibatis.dao.StudentDAOImpl">
<property name="sqlMapClient">
<ref bean="sqlMapClient" />
</property>
</bean>
<!-- 利用spring的TransactionProxyFactoryBean去对事务进行自动管理 -->
<bean id="studao"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref local="transactionManager" />
</property>
<property name="target">
<ref local="studentdaoimpl" />
</property>
<property name
发表评论
-
GC的基本原理
2011-03-21 10:36 887GC的基本原理 Java的内存管理实际上就是对象 ... -
Java日期格式化及其使用例子收集
2011-01-20 22:18 7711 SimpleDateFormat担当重任,怎样格式化都 ... -
在线编辑器大全
2010-11-15 21:14 10421,FCKeditor 编辑器最新版本: 2. ... -
http://localhost:8079/ExtTest/generalmgmt/innermail/main.html
2010-04-29 12:32 1459http://localhost:8079/ExtTest/g ... -
使用JMeter进行性能测试
2010-03-30 13:28 861JMeter 是 Apache 组织的开放源代码项目,它是功能 ... -
Java实现Windows系统服务(转)
2010-03-30 13:18 1024Windows系统服务(NT服务)相对于普通应用程序最直接的一 ... -
struts2与json
2010-03-28 16:16 2996JSON(Java Script Object Notatio ... -
使用Spring注解驱动实现Ioc
2010-03-28 16:13 738基于注释(Annotation)的配置有越来越流行的趋势,Sp ... -
Struts2与spring整合
2010-03-28 16:12 778Struts 2框架整合Spring很简单,下面是整合的步骤。 ... -
Myeclipse有用的设置
2010-03-28 16:11 898Eclipse本身很快的,但是加上了myeclipse后,就狂 ... -
Jprofiler的XX与使用
2010-03-28 16:09 13171.将整个文件解压 2.将c ...
相关推荐
1. 添加依赖:在`spring ibatis整合所需jar包`中,通常包含以下关键jar文件: - spring核心库:如spring-context、spring-beans、spring-aop等,用于Spring框架的基本功能。 - spring-jdbc和spring-tx:支持Spring...
struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码struts1 spring ibatis整合项目源码
亲手做的一个spring ibatis整合,刚刚接触ibatis,二话不说,ibatis环境先搭建好。
【SSI框架搭建实例教程(struts spring ibatis整合 附切面事物处理)】 在软件开发中,集成多种框架可以提高应用程序的灵活性和可维护性。SSI框架是指Struts、Spring和iBatis的集成,这三种框架分别负责MVC模式中的...
当我们把Spring、Struts2和iBatis整合在一起时,可以构建出一个高效、模块化的Web应用。Spring作为整体的框架容器,负责管理所有的Bean,包括Struts2和iBatis的相关组件。Struts2处理HTTP请求,调用Spring管理的业务...
1. 添加依赖:整合SSH,需要在项目中引入相应的jar包,包括Struts2、Spring、iBatis以及它们的依赖库,例如sitemesh用于页面装饰。确保包含所有必要的jar,如描述中提到的,这里有18个jar包,涵盖了基本需求。 2. ...
Struts2、Spring和iBatis是Java Web开发中常用的三个框架,它们分别负责MVC模式中的Action层、业务逻辑层以及数据访问层。将这三个框架整合在一起,可以构建出高效、灵活的企业级应用。 **Struts2** 是一个基于MVC...
在整合 Spring 和 iBATIS 进行项目开发时,主要涉及以下几个关键知识点: 1. **依赖注入 (Dependency Injection, DI)**:Spring 的核心特性之一,通过 DI 可以使代码更松耦合,易于测试和维护。在整合 iBATIS 时,...
在IT行业中,Spring框架与iBatis(现为MyBatis)的整合是常见的数据访问层解决方案,尤其在企业级应用开发中极为普遍。Spring是一个全面的Java应用程序框架,提供了依赖注入、AOP(面向切面编程)、MVC(模型-视图-...
通过整合 Spring MVC、Spring 和 iBatis,开发者可以利用它们的优势,如 Spring 的依赖注入、iBatis 的灵活 SQL 映射以及 Spring MVC 的 MVC 设计模式,构建出高效、可维护的Web应用。在实际项目中,这个整合包可能...
"Struts2+Spring+iBatis整合"是一个典型的MVC(Model-View-Controller)架构实现,适用于构建大型企业级应用。下面将详细介绍这三个框架以及它们整合的关键点。 **Struts2** 是一个基于MVC设计模式的Web应用框架,...
包括commons-dbcp-1.2.2.jar,commons-logging-1.1.jar,commons-pool-1.3.jar,ibatis-2.3.4.jar,log4j-over-slf4j-1.6.1.jar,ojdbc14.jar,spring-2.5.6.jar
STRUTS2+SPRING+IBATIS 下载后用ECLIPSE打开 结构清晰。 去掉了JAR包 需要的请与我联系 QQ81611911
整合iBatis通常涉及到Spring的DataSource配置,以及MyBatis的SqlSessionFactory和Mapper接口的使用。 在用户登录场景下,拦截器起着关键作用。在Struts2中,我们可以自定义拦截器来拦截特定的请求,比如所有需要...
7. **测试**:在Spring与iBatis整合后,可以使用Spring的`@Transactional`注解进行单元测试,确保每个方法都在独立的事务中执行,便于模拟各种数据状态并进行断言。 以上就是关于"Spring 3.2与iBatis整合"及"在...
Spring3 和 iBatis 整合是一个常见的Java企业级应用开发模式,主要用于构建灵活、可扩展的后端数据访问层。这个项目实例提供了一个详细的学习资源,帮助新手掌握这两种技术的融合。以下是对这个整合实例的详细解析:...
struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例 struts2、spring、ibatis整合实例
在与Struts2和iBatis的整合中,Spring主要负责管理Bean的生命周期和依赖关系,以及事务的统一管理。Spring的事务管理可以基于编程式或者声明式,后者通过@Transactional注解实现,更符合开闭原则,降低了代码的耦合...
### Spring与iBatis整合详解 #### 一、前言 在Java企业级应用开发中,Spring框架以其强大的依赖注入(DI)和面向切面编程(AOP)能力深受开发者喜爱,而iBatis(现更名为MyBatis)作为一款优秀的持久层框架,通过...
#### 一、Spring与IBatis整合概述 Spring框架与IBatis(现称为MyBatis)的整合为开发者提供了一种更简洁、更强大的数据库访问方式。Spring通过其内置的支持机制极大地简化了原有的IBatis访问代码,并且提供了与...