class="postTitle">iBatis简单入门教程
转自:http://www.cnblogs.com/ycxyyzw/archive/2012/10/13/2722567.html
?
iBatis 简介:
iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。
官网为:http://www.mybatis.org/
?
搭建iBatis 开发环境:
1 、导入相关的jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar
2 、编写配置文件:
Jdbc 连接的属性文件
总配置文件, SqlMapConfig.xml
关于每个实体的映射文件(Map 文件)
?
Demo :
Student.java:
Java代码
-
package com.iflytek.entity;
-
-
import java.sql.Date;
-
-
publicclass Student {
-
privateint id;
-
private String name;
-
private Date birth;
-
privatefloat score;
-
-
publicint getId() {
-
return id;
- }
-
-
publicvoid setId(int id) {
-
this.id = id;
- }
-
-
public String getName() {
-
return name;
- }
-
-
publicvoid setName(String name) {
-
this.name = name;
- }
-
-
public Date getBirth() {
-
return birth;
- }
-
-
publicvoid setBirth(Date birth) {
-
this.birth = birth;
- }
-
-
publicfloat getScore() {
-
return score;
- }
-
-
publicvoid setScore(float score) {
-
this.score = score;
- }
-
- @Override
-
public String toString() {
-
return"id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="
-
+ score + "\n";
- }
-
- }
?
SqlMap.properties :
Properties代码
- driver=com.mysql.jdbc.Driver
-
url=jdbc:mysql://localhost:3306/ibatis
- username=root
-
password=123
?
Student.xml :
Xml代码
- <?xmlversion="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>
-
<typeAliasalias="Student"type="com.iflytek.entity.Student"/>
-
-
<selectid="selectAllStudent"resultClass="Student">
- select * from
- tbl_student
- </select>
-
-
<selectid="selectStudentById"parameterClass="int"resultClass="Student">
-
select * from tbl_student where id=#id#
- </select>
-
-
<selectid="selectStudentByName"parameterClass="String"
-
resultClass="Student">
- select name,birth,score from tbl_student where name like
- '%$name$%'
- </select>
-
-
<insertid="addStudent"parameterClass="Student">
- insert into
- tbl_student(name,birth,score) values
- (#name#,#birth#,#score#);
-
<selectKeyresultClass="int"keyProperty="id">
- select @@identity as inserted
- <!-- 还有一点需要注意的是不同的数据库生产商生成主键的方式不一样,有些是预先生成 (pre-generate)主键的,如Oracle和PostgreSQL。
-
有些是事后生成(post-generate)主键的,如MySQL和SQL Server 所以如果是Oracle数据库,则需要将selectKey写在insert之前 -->
- </selectKey>
- </insert>
-
-
<deleteid="deleteStudentById"parameterClass="int">
-
delete from tbl_student where id=#id#
- </delete>
-
-
<updateid="updateStudent"parameterClass="Student">
- update tbl_student set
-
name=#name#,birth=#birth#,score=#score# where id=#id#
- </update>
-
- </sqlMap>
?
说明:
如果xml 中没有ibatis 的提示,则window --> Preference--> XML-->XML Catalog---> 点击add
选择uri URI: 请选择本地文件系统上
iBatisDemo1/WebContent/WEB-INF/lib/sql-map-config-2.dtd 文件;
Key Type: 选择Schema Location;
Key: 需要联网的,不建议使用;
SqlMapConfig.xml :
Xml代码
- <?xmlversion="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>
-
<propertiesresource="com/iflytek/entity/SqlMap.properties"/>
-
<transactionManagertype="JDBC">
-
<dataSourcetype="SIMPLE">
-
<propertyname="JDBC.Driver"value="${driver}"/>
-
<propertyname="JDBC.ConnectionURL"value="${url}"/>
-
<propertyname="JDBC.Username"value="${username}"/>
-
<propertyname="JDBC.Password"value="${password}"/>
- </dataSource>
- </transactionManager>
-
<sqlMapresource="com/iflytek/entity/Student.xml"/>
- </sqlMapConfig>
?
StudentDao :
Java代码
-
package com.iflytek.dao;
-
-
import java.util.List;
-
-
import com.iflytek.entity.Student;
-
-
publicinterface StudentDao {
-
-
publicboolean addStudent(Student student);
-
-
publicboolean deleteStudentById(int id);
-
-
publicboolean updateStudent(Student student);
-
-
public List<Student> selectAllStudent();
-
-
public List<Student> selectStudentByName(String name);
-
-
public Student selectStudentById(int id);
-
- }
StudentDaoImpl :
Java代码
-
package com.iflytek.daoimpl;
-
-
import java.io.IOException;
-
import java.io.Reader;
-
import java.sql.SQLException;
-
import java.util.List;
-
-
import com.ibatis.common.resources.Resources;
-
import com.ibatis.sqlmap.client.SqlMapClient;
-
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
-
import com.iflytek.dao.StudentDao;
-
import com.iflytek.entity.Student;
-
-
publicclass StudentDaoImpl implements StudentDao {
-
-
privatestatic SqlMapClient sqlMapClient = null;
-
-
static {
-
try {
- Reader reader = Resources
-
.getResourceAsReader("com/iflytek/entity/SqlMapConfig.xml");
- sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
-
} catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
publicboolean addStudent(Student student) {
-
Object object = null;
-
boolean flag = false;
-
try {
-
object = sqlMapClient.insert("addStudent", student);
-
System.out.println("添加学生信息的返回值:" + object);
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
if (object != null) {
-
flag = true;
- }
-
return flag;
- }
-
-
publicboolean deleteStudentById(int id) {
-
boolean flag = false;
-
Object object = null;
-
try {
-
object = sqlMapClient.delete("deleteStudentById", id);
-
System.out.println("删除学生信息的返回值:" + object + ",这里返回的是影响的行数");
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
if (object != null) {
-
flag = true;
-
- }
-
return flag;
-
- }
-
-
publicboolean updateStudent(Student student) {
-
boolean flag = false;
-
Object object = false;
-
try {
-
object = sqlMapClient.update("updateStudent", student);
-
System.out.println("更新学生信息的返回值:" + object + ",返回影响的行数");
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
if (object != null) {
-
flag = true;
- }
-
return flag;
- }
-
-
public List<Student> selectAllStudent() {
-
List<Student> students = null;
-
try {
-
students = sqlMapClient.queryForList("selectAllStudent");
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
return students;
- }
-
-
public List<Student> selectStudentByName(String name) {
-
List<Student> students = null;
-
try {
-
students = sqlMapClient.queryForList("selectStudentByName",name);
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
return students;
- }
-
-
public Student selectStudentById(int id) {
-
Student student = null;
-
try {
- student = (Student) sqlMapClient.queryForObject(
-
"selectStudentById", id);
-
} catch (SQLException e) {
- e.printStackTrace();
- }
-
return student;
- }
- }
?
TestIbatis.java :
Java代码
-
package com.iflytek.test;
-
-
import java.sql.Date;
-
import java.util.List;
-
-
import com.iflytek.daoimpl.StudentDaoImpl;
-
import com.iflytek.entity.Student;
-
-
publicclass TestIbatis {
-
-
publicstaticvoid main(String[] args) {
-
StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
-
-
System.out.println("测试插入");
-
Student addStudent = new Student();
-
addStudent.setName("李四");
-
addStudent.setBirth(Date.valueOf("2011-09-02"));
-
addStudent.setScore(88);
- System.out.println(studentDaoImpl.addStudent(addStudent));
-
-
System.out.println("测试根据id查询");
-
System.out.println(studentDaoImpl.selectStudentById(1));
-
-
System.out.println("测试模糊查询");
-
List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
-
for (Student student : mohuLists) {
- System.out.println(student);
- }
-
-
System.out.println("测试查询所有");
- List<Student> students = studentDaoImpl.selectAllStudent();
-
for (Student student : students) {
- System.out.println(student);
- }
-
-
System.out.println("根据id删除学生信息");
-
System.out.println(studentDaoImpl.deleteStudentById(1));
-
-
System.out.println("测试更新学生信息");
-
Student updateStudent = new Student();
-
updateStudent.setId(1);
-
updateStudent.setName("李四1");
-
updateStudent.setBirth(Date.valueOf("2011-08-07"));
-
updateStudent.setScore(21);
- System.out.println(studentDaoImpl.updateStudent(updateStudent));
-
- }
- }
?
iBatis 的优缺点:
优点:
1、减少代码量,简单;
2、性能增强;
3、Sql 语句与程序代码分离;
4、增强了移植性;
缺点:
1、和Hibernate 相比,sql 需要自己写;
2、参数数量只能有一个,多个参数时不太方便;