简介:apache的一个开源项目,OR Mapping解决方案
官网:http://ibatis.apache.org
环境搭建:
- jar包 jdbc ibatis
- 配置文件 jdbc连接属性文件 总配置文件SqlMapConfig.xml 实体映射文件
SqlMapConfig.xml
student.xml实体类映射文件
<typeAlias alias="Student" type="com.xxx.Student"/>别名
<select id="selectAllStudents" resultClass="Student">select * from Student</select>
<select id="selectStudentById" parameterClass="int" resultClass="Student">select sid,sname,score,major,birth from Student where sid=#sid#</select>
<insert id="insertStudent" parameterClass="Student">insert into Student(sid,sname,major,score,birth)values(#sid#,#sname#,#major#,#score#,#birth#)
<delete id="deleteStudentById" parameterClass="int">delete from Student where sid=#sid#</delete>
<update id="updateStudentById" parameterClass="Student">update Student set major=#major#,sname=#sname#,birth=#birth#,score=#score# where sid=#sid#</update>
<select id="selectStudentByName" parameterClass="String" resultClass="Student">select sid,sname,major,birth,score from Student where sname like '%$sname$%#</select> 模糊查询
<insert id="insertStudentBySquence" parameterClass="Student"> 主键自动生成
<selectKey resultClass="int" keyProperty="sid"> 主键值自动增长
select studentPKSequence.nextVal as sid from dual
</selectKey>
insert into Student(sid,sname,major,score,birth)values(#sid#,#sname#,#major#,#score#,#birth#)
</insert>
//定义一个实体类
public class Student{
private int sid;
private String sname;
private String major;
private Date birth;
get/set方法略
}
//读取配置文件
private static SqlMapClient sqlMapClient = null;
static{
try{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");//字符输入流
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(IOException e){}
}
进行CRUD操作借助SqlMapClient中的方法:
- queryForObject
- queryForList
- insert
- delete
- update
定义Dao层
public Interface IStudentDao{
public void addStudent(Student student);
public void addStudentBySequence(Student student);
public void deleteStudentById();
public void updateStudent(Student student);
public List<Student> queryAllStudent();
public List<Student> queryStudentByName(String name);
public Student queryStudentById(int id);
}
public class IStudentDaoImpl implements IStudentDao{
private static SqlMapClient sqlMapClient = null;
static{
try{
Reader reader = Resources.getResourceAsReader("SqlMapConfig.xml");//字符输入流
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
}catch(){}
}
public void addStudent(Student student){
Student student = new Student();
student.setSid(100);
student.setSname("jordan");
student.setMajor("player");
student.setBirth(Date.ValueOf("1997-08-08"));
student.setScore(69);
try{
sqlMapClient.insert("insertStudent",student);
}catch(IOException e){
e.printStacks();
}
}
public void addStudentBySequence(Student student){
//通过自增长序列维护id,从数据库获取主键值然后再插入数据
Student student = new Student();
student.setSname("james");
student.setMajor("player");
student.setBirth(Date.ValueOf("2003-08-08"));
student.setScore(69);
try{
sqlMapClient.insert("insertStudentBySequence",student);
}catch(IOException e){
e.printStacks();
}
}
public void deleteStudentById(int id){
try{
sqlMapClient.delete("deleteStudentById",id);//若删除成功,返回删除的行数
}catch(IOException e){
e.printStacks();
}
}
public void updateStudent(Student student){
Student student = new Student();
student.setId(24);
student.setSname("kobe");
student.setMajor("player");
student.setBirth(Date.ValueOf("2007-08-08"));
student.setScore(81);
try{
sqlMapClient.update("updateStudentById",student);//若更新成功,返回删除的行数
}catch(IOException e){
e.printStacks();
}
}
public List<Student> queryAllStudent(){
List<Student> studentList = null;
try{
list<Student> studentList = sqlMapClient.queryForList("selectAllStudent");
}catch(IOException e){
e.printStacks();
}
return studentList;
}
public List<Student> queryStudentByName(String name){
List<Student> studentList = null;
try{
studentList = sqlMapClient.queryForList("selectStudentByName",name);
}catch(IOException e){
e.printStacks();
}
return studentList;
}
public Student queryStudentById(int id){
Student student = null;
try{
list<Student> studentList = sqlMapClient.queryForList("selectStudentById",id);
}catch(IOException e){
e.printStacks();
}
return student;
}
}
优点:
sql语句与程序代码分离,增强了移植性(与JDBC的比较)
缺点:
sql语句需要手动编写,参数parameterClass只能一个