iBatis 简介:
iBatis 是apache 的一个开源项目,一个O/R Mapping 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。
搭建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:
- package org.eztx.cn.entity;
- import java.sql.Date;
- public class Student {
- // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题
- private int id;
- private String name;
- private Date birthday;
- 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 Date getBirthday() {
- return birthday;
- }
- public void setBirthday(Date birthday) {
- this.birthday = birthday;
- }
- public float getScore() {
- return score;
- }
- public void setScore(float score) {
- this.score = score;
- }
- @Override
- public String toString() {
- return "id=" + id + "\tName=" + name + "\tBirthday=" + birthday + "\tScore="
- + score + "\n";
- }
- }
SqlMap.properties :
- driver=com.mysql.jdbc.Driver
- url=jdbc:mysql://mysql.sql47.cdncenter.net:3306/sq_maenze?useUnicode=true&characterEncoding=UTF-8
- username=sq_maenze
- password=mez199023
Student.xml :
说明:
如果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 :
StudentDao :
- package org.eztx.cn.services;
- import java.util.List;
- import org.eztx.cn.entity.Student;
- public interface StudentDao {
- /**
- * 添加学生信息
- *
- * @param student
- * 学生实体
- * @return 返回是否添加成功
- */
- public boolean addStudent(Student student);
- /**
- * 根据学生id删除学生信息
- *
- * @param id
- * 学生id
- * @return 删除是否成功
- */
- public boolean deleteStudentById(int id);
- /**
- * 更新学生信息
- *
- * @param student
- * 学生实体
- * @return 更新是否成功
- */
- public boolean updateStudent(Student student);
- /**
- * 查询全部学生信息
- *
- * @return 返回学生列表
- */
- public List<Student> selectAllStudent();
- /**
- * 根据学生姓名模糊查询学生信息
- *
- * @param name
- * 学生姓名
- * @return 学生信息列表
- */
- public List<Student> selectStudentByName(String name);
- /**
- * 根据学生id查询学生信息
- *
- * @param id
- * 学生id
- * @return 学生对象
- */
- public Student selectStudentById(int id);
- }
StudentDaoImpl :
- package org.eztx.cn.services.impl;
- import java.io.IOException;
- import java.io.Reader;
- import java.sql.SQLException;
- import java.util.List;
- import org.eztx.cn.entity.Student;
- import org.eztx.cn.services.StudentDao;
- import com.ibatis.common.resources.Resources;
- import com.ibatis.sqlmap.client.SqlMapClient;
- import com.ibatis.sqlmap.client.SqlMapClientBuilder;
- public class StudentDaoImpl implements StudentDao {
- private static SqlMapClient sqlMapClient = null;
- // 读取配置文件
- static {
- try {
- Reader reader = Resources
- .getResourceAsReader("org/eztx/cn/xml/SqlMapConfig.xml");
- sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
- reader.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- public boolean 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;
- }
- public boolean 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;
- }
- public boolean 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 :
- package org.eztx.cn;
- import java.sql.Date;
- import java.util.List;
- import org.eztx.cn.entity.Student;
- import org.eztx.cn.services.impl.StudentDaoImpl;
- public class testMyBatis {
- public static void main(String[] args) {
- //add();
- //delete();
- select();
- // update();
- }
- /**
- * 添加
- */
- private static void add() {
- StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
- System.out.println("==============================");
- System.out.println("测试插入");
- Student addStudent = new Student();
- addStudent.setName("李四");
- addStudent.setBirthday(Date.valueOf("2011-09-02"));
- addStudent.setScore(88);
- System.out.println(studentDaoImpl.addStudent(addStudent));
- System.out.println("==============================");
- System.out.println("测试根据id查询");
- System.out.println(studentDaoImpl.selectStudentById(1));
- System.out.println("==============================");
- System.out.println("测试模糊查询");
- List<Student> mohuLists = studentDaoImpl.selectStudentByName("李");
- for (Student student : mohuLists) {
- System.out.println(student);
- }
- }
- /**
- * 删除
- */
- private static void delete() {
- StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
- System.out.println("根据id删除学生信息");
- System.out.println(studentDaoImpl.deleteStudentById(1));
- }
- /**
- * 更新
- */
- private static void update() {
- StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
- System.out.println("测试更新学生信息");
- Student updateStudent = new Student();
- updateStudent.setId(1);
- updateStudent.setName("李四1");
- updateStudent.setBirthday(Date.valueOf("2011-08-07"));
- updateStudent.setScore(21);
- System.out.println(studentDaoImpl.updateStudent(updateStudent));
- }
- /**
- * 查询
- */
- private static void select() {
- StudentDaoImpl studentDaoImpl = new StudentDaoImpl();
- System.out.println("测试查询所有");
- List<Student> students = studentDaoImpl.selectAllStudent();
- for (Student student : students) {
- System.out.println(student);
- }
- }
- }