Ibatis使用实践-1
Ibatis使用轻巧方便,简单易学。基本原理是将程序中的sql抽取出来放入配置文件中,可以精确控制发出的SQL语句,对SQL的调优非常的方便。
不过也存在一些, 传递的参数最多只能有一个, 在缓存的方面未做任何的处理, 而这些正好是hibernate更加强大的地方。
一、Ibatis资料参考如下:
二、实例
1、定义POJO类:Student.java
package com.alibaba.ibatis.modules;
public class Student {
private int id;
private String name;
private String address;
private float score;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "id: " + id + ", name: " + name + ", address: " + address
+ ", score: " + score;
}
}
2、定义使用接口:StudentService.java
package com.alibaba.ibatis.service;
import java.util.List;
import com.alibaba.ibatis.modules.Student;
public interface StudentService {
public void addStudent(Student student);
public int updateStudent(Student student);
public int deleteStudent(Student student);
public Student getStudentById(int id);
public List getStudentsLike(String likeName);
public List getAllStudents();
}
增加StudentService的实现类:StudentServiceImpl.java
package com.alibaba.ibatis.service.impl;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import com.alibaba.ibatis.modules.Student;
import com.alibaba.ibatis.service.StudentService;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class StudentServiceImpl implements StudentService {
static SqlMapClient sqlMapClient = null;
static {
String resource = "SqlMapConfig.xml";
try {
Reader reader = Resources.getResourceAsReader(resource);
sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void addStudent(Student student) {
try {
sqlMapClient.insert("insertStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public int deleteStudent(Student student) {
int effectedRow = 0;
try {
effectedRow = sqlMapClient.delete("deleteStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
return effectedRow;
}
@SuppressWarnings("unchecked")
@Override
public List getAllStudents() {
List students = null;
try {
students = sqlMapClient.queryForList("getAllStudents");
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
@Override
public Student getStudentById(int id) {
Student student = null;
try {
student = (Student) sqlMapClient.queryForObject("getStudentById",
id);
} catch (SQLException e) {
e.printStackTrace();
}
return student;
}
@Override
public int updateStudent(Student student) {
int effectedRow = 0;
try {
effectedRow = sqlMapClient.update("updateStudent", student);
} catch (SQLException e) {
e.printStackTrace();
}
return effectedRow;
}
@SuppressWarnings("unchecked")
@Override
public List getStudentsLike(String likeName) {
List students = null;
try {
students = sqlMapClient.queryForList("getStudentsLike", likeName);
} catch (SQLException e) {
e.printStackTrace();
}
return students;
}
}
3、Ibatis的三个配置文件:
(1)与数据连接相关的配置文件jdbc.properties,
(2)操作具体POJO的增删改查的配置文件
(3)总控文件
(1)与数据连接相关的配置文件:jdbc.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis
username=ibatis
password=ibatis
Pool.MaximumActiveConnections=10
Pool.MaximumIdleConnections=5
Pool.MaximumCheckoutTime=120000
Pool.TimeToWait=500
(2)操作具体POJO的增删改查的配置文件:student.xml, 其中对模糊查询的情况需要特别的注意:必须使用$来做占位符,如:SELECT * FROM student where name like '%$name$%'
/p>
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
INSERT INTO
student(id, name, address, score)
VALUES(#id#,#name#,#address#,#score#)
SELECT * FROM student where id = #id#
SELECT * FROM student
SELECT * FROM student where name like '%$name$%'
UPDATE student set
name = #name#, address = #address#, score = #score# where id = #id#
DELETE FROM
student where id = #id# and name = #name# and address = #address# and
score = #score#
(3)总控文件:SqlMapConfig.xml
/p>
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
4、引入相应的依赖包:pom.xml
com.alibaba.external
sourceforge.ibatis
2.3.4
com.alibaba.external
jdbc.mysql.mysql-connector
5.1.6
经测试,service中的各接口方法成功执行