一 Ibatis的使用:
1.1創建數據庫ibatis和表student
1.2創建實體類student.java與student表要對應
1.3導入相關jar包:
ibatis-2.3.4.726.jar
Mysql-connector.jar(或者是oracle的驅動包)
1.4引入配置文件
數據庫連接信息:sqlMap.properties:
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ibatis
username=root
password=123456
針對數據庫操作的配置文件(比如操作的是student表):student.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.fasdq.domain.Student"/>
<select id="selectAllStudent" resultClass="student">
select *
from student
</select>
<select id="selectStudentById" parameterClass="int" resultClass="student">
select *
from student
where id = #id#
</select>
<insert id="insertStudent" parameterClass="student">
insert into student(id, name, age)
values (#id#,#name#,#age#)
</insert>
<delete id="deleteStudentById" parameterClass="int">
delete
from student
where id = #id#
</delete>
<update id = "updateById" parameterClass = "student">
update student
set name = #name#,
id = #id#,
age = #age#
where id = #id#
</update>
<select id="selectStudentByName" parameterClass="String" resultClass="student">
select *
from student
where name like '$name$'
</select>
</sqlMap>
Ibatis主配置文件:SqlMapConfig.xml:
<?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="sqlMap.properties"/>
<transactionManager type="JDBC" >
<dataSource type="SIMPLE">
<property value="${driverClassName}" 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="student.xml"/>
</sqlMapConfig>
1.5 crud操作
public class StudentDaoImpl implements StudentDao {
//SqlMapClient對象,所有操作都是由它來執行
private static SqlMapClient smc = null;
static{
try {
//讀取主配置文件
Reader reader = com.ibatis.common.resources
.Resources.getResourceAsReader("SqlMapConfig.xml");
//獲得SqlMapClient對象
smc = com.ibatis.sqlmap.client
.SqlMapClientBuilder.buildSqlMapClient(reader);
//關閉流
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//添加操作
public void addStudent(Student s) {
try {
//insertStudent對應的是student.xml一個sql語句id,s參數
smc.insert("insertStudent", s);
} catch (SQLException e) {
e.printStackTrace();
}
}
public void addStudentBySequence(Student s) {
// TODO Auto-generated method stub
}
//根據id刪除
public void deleteStudentById(int id) {
int result = 0;
try {
//刪除成功的話返回1
result = smc.delete("deleteStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println(result);
}
//查詢所有的
public List<Student> queryAllStudent() {
List<Student> list = null;
try {
list =smc.queryForList("selectAllStudent");
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
//根據id查找
public Student queryStudentById(int id){
Student s = null;
try {
s = (Student) smc.queryForObject("selectStudentById", id);
} catch (SQLException e) {
e.printStackTrace();
}
return s;
}
//更新
public void updateStudent(Student s){
try {
System.out.println(smc.update("updateById", s));
} catch (SQLException e) {
e.printStackTrace();
}
}
//模糊查詢,根據姓名查詢
public List<Student> selectStudentByName(String name){
List<Student> list = null;
try {
list = smc.queryForList("selectStudentByName", name);
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public static void main(String[] args) {
StudentDaoImpl sdi = new StudentDaoImpl();
System.out.println(sdi.selectStudentByName("%tom%"));
}
public void updateStudentById(int id) {
// TODO Auto-generated method stub
}
}