博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ibatis入门学习
阅读量:6583 次
发布时间:2019-06-24

本文共 8428 字,大约阅读时间需要 28 分钟。

搭建iBatis 开发环境:

1 、导入相关的jar 包,ibatis-2.3.0.677.jar 、mysql-connector-java-5.1.6-bin.jar

2 、编写配置文件:

Jdbc 连接的属性文件 SqlMap.properties 

总配置文件, SqlMapConfig.xml

关于每个实体的映射文件(Map 文件)

Demo 

Student.java:

package com.iflytek.entity;import java.sql.Date;/** * @author xudongwang 2011-12-31 *  *         Email:xdwangiflytek@gmail.com *  */public class Student {    // 注意这里需要保证有一个无参构造方法,因为包括Hibernate在内的映射都是使用反射的,如果没有无参构造可能会出现问题    private int id;    private String name;    private Date birth;    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 getBirth() {        return birth;    }    public void setBirth(Date birth) {        this.birth = birth;    }    public float getScore() {        return score;    }    public void setScore(float score) {        this.score = score;    }    @Override    public String toString() {        return "id=" + id + "\tname=" + name + "\tmajor=" + birth + "\tscore="                + score + "\n";    }}

SqlMap.properties :

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/ibatisusername=rootpassword=123

Student.xml :

insert into tbl_student(name,birth,score) values (#name#,#birth#,#score#);
select @@identity as inserted
delete from tbl_student where id=#id#
update tbl_student set name=#name#,birth=#birth#,score=#score# where id=#id#

说明:

如果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 com.iflytek.dao;import java.util.List;import com.iflytek.entity.Student;/** * @author xudongwang 2011-12-31 *  *         Email:xdwangiflytek@gmail.com *  */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
selectAllStudent(); /** * 根据学生姓名模糊查询学生信息 * * @param name * 学生姓名 * @return 学生信息列表 */ public List
selectStudentByName(String name); /** * 根据学生id查询学生信息 * * @param id * 学生id * @return 学生对象 */ public Student selectStudentById(int id);}

StudentDaoImpl :

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;/** * @author xudongwang 2011-12-31 *  *         Email:xdwangiflytek@gmail.com *  */public class StudentDaoImpl implements StudentDao {    private static 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();        }    }    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
selectAllStudent() { List
students = null; try { students = sqlMapClient.queryForList("selectAllStudent"); } catch (SQLException e) { e.printStackTrace(); } return students; } public List
selectStudentByName(String name) { List
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 :

1 package com.iflytek.test; 2  3 import java.sql.Date; 4 import java.util.List; 5  6 import com.iflytek.daoimpl.StudentDaoImpl; 7 import com.iflytek.entity.Student; 8  9 /**10  * @author xudongwang 2011-12-3111  * 12  *         Email:xdwangiflytek@gmail.com13  * 14  */15 public class TestIbatis {16 17     public static void main(String[] args) {18         StudentDaoImpl studentDaoImpl = new StudentDaoImpl();19 20         System.out.println("测试插入");21         Student addStudent = new Student();22         addStudent.setName("李四");23         addStudent.setBirth(Date.valueOf("2011-09-02"));24         addStudent.setScore(88);25         System.out.println(studentDaoImpl.addStudent(addStudent));26 27         System.out.println("测试根据id查询");28         System.out.println(studentDaoImpl.selectStudentById(1));29 30         System.out.println("测试模糊查询");31         List
mohuLists = studentDaoImpl.selectStudentByName("李");32 for (Student student : mohuLists) {33 System.out.println(student);34 }35 36 System.out.println("测试查询所有");37 List
students = studentDaoImpl.selectAllStudent();38 for (Student student : students) {39 System.out.println(student);40 }41 42 System.out.println("根据id删除学生信息");43 System.out.println(studentDaoImpl.deleteStudentById(1));44 45 System.out.println("测试更新学生信息");46 Student updateStudent = new Student();47 updateStudent.setId(1);48 updateStudent.setName("李四1");49 updateStudent.setBirth(Date.valueOf("2011-08-07"));50 updateStudent.setScore(21);51 System.out.println(studentDaoImpl.updateStudent(updateStudent));52 53 }54 }

 

 

 

转载于:https://www.cnblogs.com/jiayongchao/archive/2013/04/06/3002691.html

你可能感兴趣的文章
war 宽度变窄
查看>>
set p4 environment in windows
查看>>
pl/sql development 查询的数据复制到excel
查看>>
自定义指令的参数
查看>>
python实现进度条
查看>>
Android 一个应用启动另一个应用的说明
查看>>
阿里云CentOS7服务器利用LVM分区挂载磁盘全记录
查看>>
Setting up the Web Admin Tool in LDAP 6.x to communicate via SSL
查看>>
SQL好习惯:编写支持可搜索的SQL
查看>>
Shadowbox
查看>>
【 程 序 员 】:伤不起的三十岁,你还有多远 ?
查看>>
openldap安装
查看>>
[leetcode]count and say
查看>>
润乾报表 - 缓存问题
查看>>
利用IFormattable接口自动参数化Sql语句
查看>>
泛型Dictionary的用法详解
查看>>
明晰三种常见存储技术:DAS、SAN和NAS
查看>>
ContentProvider简单介绍
查看>>
Visual Studio 2014 CTPs 下载 和C# 6.0 语言预览版介绍
查看>>
js混淆 反混淆 在线
查看>>