一、Ibaties简介:
iBatis是apache的一个开源项目,一个O/R Mapping解决方案,iBatis最大的特点就是小巧,灵活,上手快。如果不需要太多复杂的功能,iBatis是能满足你的要求,又足够灵活的最简单的解决方案。
二、优点:
n 上手快,容易学
n Sql与程序相分离,维护型好
n 移植性强
n 简化项目中的分工
三、缺点:
n 需要自己动手写sql语句
n 参数数量只能有一个
四、环境配置
Tomact6.0+eclipse+jdk+ibatis-2.3.4.726.jar+oracle_10g_10.2.0.4.jar
五、ibatis的总配置文件的配置:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<!--引入连接数据库的属性文件-->
<properties resource="com/cn/sqlMap.properties"/>
<!--事物管理配置-->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property value="${driver}" name="JDBC.Driver"/>
<property value="${url}" name="JDBC.ConnectionURL"/>
<property value="${username}" name="JDBC.Username"/>
<property value="${password}" name="JDBC.Password"/>
</dataSource>
</transactionManager>
<!--注册配置文件-->
<sqlMap resource="com/cn/Student.xml" />
</sqlMapConfig>
六、新建一个Student 类,:
package com.cn;
import java.io.Serializable;
public class Student implements Serializable{
private Integer id;
private String sname;
private Integer age;
public Student()
{
System.out.println("无参构造函数");
}
public Student(Integer id, String sname, Integer age) {
super();
this.id = id;
this.sname = sname;
this.age = age;
}
public Integer getId() {
return id;
}
public Integer getSid() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
七、新建一个接口 StudentInterface :
package test.ibaties;
import java.util.List;
public interface StudentInterface {
/**
* 添加学生
* @param student
*/
public void addStudent(Student student);
/**
* 按sequence方法添加Student
* @param student
*/
public void addStudentBySequence(Student student);
/**
* 按id删除学生
* @param id
*/
public void deleteStudent(int id);
/**
* 更新学生
* @param student
*/
public void updateStudent(Student student);
/**
* 查询所有学生信息
* @return
*/
public List<Student> queryAllStudent();
/**
* 按姓名模糊查询学生信息
* @param sname
* @return
*/
public List<Student> queryByStudentName(String sname);
/**
* 按学生id查询出指定的学生信息
* @param id
* @return
*/
public Student queryStudentById(int id);
/**
* 按学生的id和姓名查询学生信息
* @param id
* @param name
* @return
*/
public Student queryStudentByIdAndName(Student student);
}
八、实现接口StudentInterface 的子类 StudentImp
package test.ibaties.imp;
import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.cn.Student;
import com.ibatis.sqlmap.client.SqlMapClient;
import test.ibaties.StudentInterface;
public class StudentImp implements StudentInterface {
private static SqlMapClient sqlMapClient=null;
static
{
try {
//读取SqlMapClient配置文件
Reader reader=com.ibatis.common.resources.Resources.getResourceAsReader("com/cn/sql-map-config.xml");
sqlMapClient=com.ibatis.sqlmap.client.SqlMapClientBuilder.buildSqlMapClient(reader);
//关闭读取
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void addStudent(Student student) {
try {
sqlMapClient.insert("addStudent", student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void deleteStudent(int id) {
try {
System.out.println(sqlMapClient.delete("deleteStudentById", id));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Student> queryAllStudent() {
List<Student> listStudent=null;
try {
listStudent=sqlMapClient.queryForList("queryAllStudent");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listStudent;
}
public List<Student> queryByStudentName(String sname) {
List<Student> listStudent=null;
try {
listStudent=sqlMapClient.queryForList("queryStudentByName", sname);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return listStudent;
}
public void updateStudent(Student student) {
try {
System.out.println(sqlMapClient.update("updateStudentById", student));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Student queryStudentById(int id) {
Student student=null;
try {
student=(Student) sqlMapClient.queryForObject("queryStudentById", id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return student;
}
public Student queryStudentByIdAndName(Student student) {
Student s=null;
try {
s=(Student) sqlMapClient.queryForObject("queryStudentByIdAndName", student);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return s;
}
public void addStudentBySequence(Student student) {
try {
sqlMapClient.insert("addStudentBySequence", student);
System.out.println("id:"+student.getId());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
九、映射文件的配置Studnet.xml
<?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>
<typeAlias alias="Student" type="com.cn.Student"/>
<!-- 查询所有的Student信息 -->
<select id="queryAllStudent" resultClass="Student">
select * from Student
</select>
<!-- 按id查询出指定的Student -->
<select id="queryStudentById" parameterClass="int" resultClass="Student">
select * from Student where id=#id#
</select>
<!-- 模糊查询 -->
<select id="queryStudentByName" parameterClass="String" resultClass="Student">
select *
from Student
where sname like '$sname$'
</select>
<!-- 按id和sname查询Student -->
<statement id="queryStudentByIdAndName" parameterClass="Student" resultClass="Student">
select * from Student
where id=#id# and sname=#sname#
</statement>
<!-- 新增Student信息 -->
<insert id="addStudent" parameterClass="Student" >
insert into Student(id, sname, age)
values(#sid#,#sname#,#age#)
</insert>
<!-- sequence方法向数据库添加数据 -->
<insert id="addStudentBySequence" parameterClass="Student">
<selectKey resultClass="int" keyProperty="id">
select studentPKSequence.nextval from dual
</selectKey>
insert into Student(id,sname,age)
values(#id#,#sname#,#age#)
</insert>
<!-- 删除Student -->
<delete id="deleteStudentById" parameterClass="int">
delete from student where id=#sid#
</delete>
<!-- 更新student -->
<update id="updateStudentById" parameterClass="Student">
update Student
set
sname=#sname#,
age=#age#
where id=#id#
</update>
</sqlMap>
十、