作者:鹏城飞将 | 来源:互联网 | 2014-05-28 15:41
iBATIS一词来源于“internet”和“abatis”的组合,是一个由ClintonBegin在2001年发起的开放源代码项目。最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。第一,搭建iBatis开发环境:官网为:http://www.mybatis.org/1、导入相关的jar包,
iBATIS一词来源于“internet”和“abatis”的组合,是一个由Clinton
Begin在2001年发起的开放源代码项目。最初侧重于密码软件的开发,现在是一个基于Java的持久层框架。
第一,搭建iBatis 开发环境: 官网为:http://www.mybatis.org/
1 、导入相关的jar 包,ibatis-2.3.0.677.jar
、mysql -connector-java-5.1.6-bin.jar
2 、编写配置文件:
1)jdbc 连接的属性文件 jdbc.proprties
2)关于每个实体的映射文件(Map 文件),如Student.xml
3)总配置文件SqlMapConfig.xml 加载1)和2)的配置文件。
第二,编写数据库配置文件 jdbc.proprties
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc\:mysql\://localhost\:3306/cyb?useUnicode\=true&characterEncoding\=UTF-8&noAccessToProcedureBodies\=true&autoReconect\=true
jdbc.username=ellll
jdbc.password=ellllcom
hibernate.show_sql=true
hibernate.format_sql=true
第二 定义实体映射文件
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
select * from plat_student
select * from plat_student where id=#id#
resultClass="Student">
select * from plat_student where name
like '%$name$%'
insert into
plat_student(name,password,teacher_id)
values (#name#,#password#,#teacher_id#);
select @@identity as inserted
delete from plat_student where id=#id#
update plat_student
set name=#name#,password=#password# where id=#id#
第三 编写总的配置文件jdbc.xml
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
第五 编写业务逻辑
package com.cyb.model;
import java.io.IOException;
import java.io.Reader;
import java.sql.SQLException;
import java.util.List;
import org.apache.ibatis.io.Resources;
import com.cyb.vo.Plat_student;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;
public class StudentDaoImpl {
private static SqlMapClient sqlMapClient = null;
// 读取配置文件
static {
try {
System.out.println("加载配置文件");
Reader reader =
Resources.getResourceAsReader("properties/jdbc.xml");
sqlMapClient =
SqlMapClientBuilder.buildSqlMapClient(reader);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public boolean addPlat_student(Plat_student
Plat_student) {
Object object = null;
boolean flag = false;
try {
object =
sqlMapClient.insert("addPlat_student", Plat_student);
System.out.println("添加学生信息的返回值:" +
object);
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
}
public boolean deletePlat_studentById(int id) {
boolean flag = false;
Object object = null;
try {
object =
sqlMapClient.delete("deletePlat_studentById", id);
System.out.println("删除学生信息的返回值:" +
object + ",这里返回的是影响的行数");
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
}
public boolean updatePlat_student(Plat_student
Plat_student) {
boolean flag = false;
Object object = false;
try {
object =
sqlMapClient.update("updatePlat_student", Plat_student);
System.out.println("更新学生信息的返回值:" +
object + ",返回影响的行数");
} catch (SQLException e) {
e.printStackTrace();
}
if (object != null) {
flag = true;
}
return flag;
}
public List
selectAllPlat_student() {
List Plat_students =
null;
try {
Plat_students =
sqlMapClient.queryForList("selectAllPlat_student");
} catch (SQLException e) {
e.printStackTrace();
}
return Plat_students;
}
public List
selectPlat_studentByName(String name) {
List Plat_students =
null;
try {
Plat_students =
sqlMapClient.queryForList("selectPlat_studentByName",name);
} catch (SQLException e) {
e.printStackTrace();
}
return Plat_students;
}
public Plat_student selectPlat_studentById(int id)
{
Plat_student Plat_student = null;
try {
Plat_student = (Plat_student)
sqlMapClient.queryForObject(
"selectPlat_studentById",
id);
} catch (SQLException e) {
e.printStackTrace();
}
return Plat_student;
}
}
第六 编写测试程序
package com.cyb.model;
import java.util.List;
import com.cyb.vo.Plat_student;
import com.cyb.vo.Plat_teacher;
public class TestIbatis {
public static void main(String[] args) {
StudentDaoImpl studentDaoImpl = new
StudentDaoImpl();
System.out.println("测试插入start");
Plat_student addStudent = new
Plat_student();
addStudent.setName("李四YYYY");
addStudent.setPassword("xxx");
addStudent.setTeacher_id("1");
System.out.println(addStudent.getIsset_Teacher_id());
System.out.println(studentDaoImpl.addPlat_student(addStudent));
System.out.println("测试插入end");
System.out.println("测试根据id查询");
System.out.println(studentDaoImpl.selectPlat_studentById(95));
System.out.println("测试模糊查询");
List mohuLists =
studentDaoImpl.selectPlat_studentByName("朱");
for (Plat_student student : mohuLists) {
System.out.println(student);
}
System.out.println("测试查询所有");
List students =
studentDaoImpl.selectAllPlat_student();
for (Plat_student student : students) {
System.out.println(student);
}
System.out.println("根据id删除学生信息");
System.out.println(studentDaoImpl.deletePlat_studentById(83));
System.out.println("测试更新学生信息");
Plat_student updateStudent = new
Plat_student();
//先查询,后删除
updateStudent=studentDaoImpl.selectPlat_studentById(80);
updateStudent.setName("陈盛岚");
System.out.println(studentDaoImpl.updatePlat_student(updateStudent));
}
}
自此,简单的ibatis框架就搭建完成了,已测试成功。