ibaits的一个简单的完整的例子
2015-08-28 15:22
543 查看
ibaits的简单介绍:
iBatis 是apache 的一个开源项目,一个O/R Mapping(对象/关系映射) 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。
搭建ibaits环境需要的一些资源jar包:
ibatis-2.3.4.726.jar 、
mysql-connector-java-5.1.20-bin.jar
下面是配置相关文件:
1、首先配置一个SqlMapConfig.xml文件:
注:它的里面是与jdbc(连接数据库)一些相关的配置和引入实体类的映射文件
3、接着配置一个实体类的映射文件,暂且就用Student表示实体,对应文件为Student.xml:
注:它的里面是对实体的一些操作,即增、删、改、查。
4、接下来就是实体bean和dao了
Student实体类:
实体dao接口:
dao的实现类:
好了,到此为止基本的配置文件和相关的类就写好了,接下来就是写一些测试了:
注:此文原创。
iBatis 是apache 的一个开源项目,一个O/R Mapping(对象/关系映射) 解决方案,iBatis 最大的特点就是小巧,上手很快。如果不需要太多复杂的功能,iBatis 是能够满足你的要求又足够灵活的最简单的解决方案,现在的iBatis 已经改名为Mybatis 了。
搭建ibaits环境需要的一些资源jar包:
ibatis-2.3.4.726.jar 、
mysql-connector-java-5.1.20-bin.jar
下面是配置相关文件:
1、首先配置一个SqlMapConfig.xml文件:
注:它的里面是与jdbc(连接数据库)一些相关的配置和引入实体类的映射文件
1 <?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="com/configs/SqlMap.properties"/> <transactionManager type="JDBC"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="${driver}"/> <property name="JDBC.ConnectionURL" value="${url}"/> <property name="JDBC.Username" value="${username}"/> <property name="JDBC.Password" value="${password}"/> </dataSource> </transactionManager> <sqlMap resource="com/configs/Student.xml"/> </sqlMapConfig>
<properties resource="com/configs/SqlMap.properties"/>是引入的属性配置,它的里面是跟数据库连接的一些属性,具体见下。 2、配置一个属性文件SqlMap.properties:在[b]SqlMapConfig.xml里面通过el表达式获取相应的值 [/b]
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/user username=root password=111111
3、接着配置一个实体类的映射文件,暂且就用Student表示实体,对应文件为Student.xml:
注:它的里面是对实体的一些操作,即增、删、改、查。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN" "http://www.ibatis.com/dtd/sql-map-2.dtd"> <sqlMap> <typeAlias alias="Student" type="com.domain.Student"/> <!-- 查询记录 --> <select id="selectAllStudent" resultClass="Student"> <!-- resultClass与上方sqlMap定义的映射对应 --> select * from Student </select> <!-- 根據指定id查詢 --> <select id="selectStudentById" parameterClass="integer" resultClass="Student"> select * from Student where sid = #sid# </select> <!-- 模糊查询 --> <select id="selectStudentByName" parameterClass="String" resultClass="Student"> select sid,sname,birthday,score from student where sname like '%$sname$%' </select> <!-- 插入记录 --> <insert id="insertStudent" parameterClass="Student"> insert into Student (sid,sname,birthday,score) values (#sid#,#sname#,#birthday#,#score#) </insert> <!-- **通过序列增加学生 --> <insert id="insertStudentBySequence" parameterClass="Student"> <selectKey resultClass="integer" keyProperty="sid"> select studentPKSequence.nextVal from dual </selectKey> insert into Student(sid,sname,birthday,score) values(#sid#,#sname#,#birthday#,#score#) </insert> <!-- 删除记录 --> <delete id="deleteStudentById" parameterClass="integer"> delete from Student where sid = #sid# </delete> <!-- 修改记录 --> <update id="updateStudentById" parameterClass="Student"> update Student set sname = #sname#, birthday = #birthday#, score = #score# where sid = #sid# </update> </sqlMap>
4、接下来就是实体bean和dao了
Student实体类:
package com.domain; import java.sql.Date; public class Student { private Integer sid = 0; private String sname = null; private Date birthday = null; private float score = 0; public Integer getSid() { return sid; } public void setSid(Integer sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } 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() { String context = "sid=:" + sid + "\tsname=:" + sname + "\tbirthday=:" + birthday + "'\tscore=:" + score; return context; } }
实体dao接口:
package com.dao; import java.util.List; import com.domain.Student; public interface IStudentDao { /* * 添加学生 */ public void addStudent(Student student); /* * 通过序列增加学生 */ public void addStudentBySequence(Student student); /* * 根据传入的id删除学生 */ public void deleteStudentById(Integer id); /* * 根据传入的id修改学生 */ public void updateStudentById(Student student); /* * 查询所有学生信息 */ public List<Student> queryAllStudent(); /* * 根据传入的名称查询学生 */ public List<Student> queryStudentByName(String name); /* * 根据传入的id查询学生 */ public Student queryStudentById(Integer id); }
dao的实现类:
package com.dao.impl; import java.io.Reader; import java.sql.SQLException; import java.util.List; import com.dao.IStudentDao; import com.domain.Student; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; public class IStudentDaoImpl implements IStudentDao { private static SqlMapClient sqlMapClient = null; /** * 在内存中只加载一次 */ static { try { /* * 讀取配置文件 */ Reader reader = Resources.getResourceAsReader("com/configs/SqlMapConfig.xml"); /* * 创建SqlMapClient接口的变量实例 */ sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader); /* * 关闭字符流 */ reader.close(); } catch (Exception e) { e.printStackTrace(); } } /* * 添加学生 * @see com.dao.IStudentDao#addStudent(com.domain.Student) */ public void addStudent(Student student) { try { sqlMapClient.insert("insertStudent", student); } catch (Exception e) { e.printStackTrace(); } } /* * (non-Javadoc) * @see com.dao.IStudentDao#addStudentBySequence(com.domain.Student) */ public void addStudentBySequence(Student student) { try { //1、从数据库获取主键值 //2、向数据库插入数据 sqlMapClient.insert("insertStudentBySequence", student); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /* * (non-Javadoc) * @see com.dao.IStudentDao#deleteStudentById(java.lang.Integer) */ public void deleteStudentById(Integer id) { try { sqlMapClient.delete("deleteStudentById", id); } catch (Exception e) { e.printStackTrace(); } } /* * (non-Javadoc) * @see com.dao.IStudentDao#updateStudentById(com.domain.Student) */ public void updateStudentById(Student student) { try { System.out.println(sqlMapClient.update("updateStudentById", student)); } catch (Exception e) { e.printStackTrace(); } } /* * (non-Javadoc) * @see com.dao.IStudentDao#queryAllStudent() */ @SuppressWarnings("unchecked") public List<Student> queryAllStudent() { List<Student> students = null; try { students = sqlMapClient.queryForList("selectAllStudent"); } catch (Exception e) { e.printStackTrace(); } return students; } /* * (non-Javadoc) * @see com.dao.IStudentDao#queryStudentByName(java.lang.String) */ @SuppressWarnings("unchecked") public List<Student> queryStudentByName(String name) { List<Student> students = null; try { students = sqlMapClient.queryForList("selectStudentByName", name); } catch (Exception e) { e.printStackTrace(); } return students; } /* * (non-Javadoc) * @see com.dao.IStudentDao#queryStudentById(java.lang.Integer) */ public Student queryStudentById(Integer id) { Student student = null; try { student = (Student) sqlMapClient.queryForObject("selectStudentById", id); } catch (Exception e) { e.printStackTrace(); } return student; } }
好了,到此为止基本的配置文件和相关的类就写好了,接下来就是写一些测试了:
package com.test; import java.sql.Date; import com.dao.IStudentDao; import com.dao.impl.IStudentDaoImpl; import com.domain.Student; public class TestDao { public static void main(String[] args) { IStudentDao iStudentDao = new IStudentDaoImpl(); /* System.out.println("*************查詢所有學生*************"); for(Student student:iStudentDao.queryAllStudent()) { System.out.println(student); } System.out.println(); System.out.println("************根據id查詢************"); System.out.println(iStudentDao.queryStudentById(2)); System.out.println(); System.out.println("***********插入记录*************"); Student student = new Student(); student.setSid(5); student.setSname("小wang"); student.setBirthday(new Date(1992-04-19)); student.setScore(89); iStudentDao.addStudent(student); System.out.println("end"); System.out.println(); System.out.println("***********删除记录*************"); iStudentDao.deleteStudentById(5);*/ /*System.out.println(); System.out.println("***********修改记录*************"); Student student1 = new Student(); student1.setSid(5); student1.setSname("小wang231321"); student1.setBirthday(new Date(1992-04-19)); student1.setScore(89); iStudentDao.updateStudentById(student1);*/ System.out.println(); System.out.println("***********模糊查询记录*************"); System.out.println(iStudentDao.queryStudentByName("w")); } }
5、最后,对ibaits的一些优缺点总结: 优点:(和jdbc相比较) (1)、减少了不少的代码量 (2)、简单 (3)、架构级性能增强 (4)、sql语句与程序代码分离 (5)、简化项目中的分工 (6)、增强了移植性 缺点: (1)、sql代码需要自己写 (2)、参数数量只有一个
注:此文原创。
相关文章推荐
- solr搭建企业级应用搜索(转自http://blog.csdn.net/zdshare/article/details/17756121)
- waiting for device
- The Responder Chain(响应链)
- * daemon not running. starting it now * ADB server didn't ACK * failed to start daemon *
- Go 语言sync中waitgroup使用.小实例
- Python_使用smtplib和email模块发送邮件
- OpenStack Mnaila 2015-08-27 IRC会议内容
- POJ3411--Paid Roads
- HDU 3376--Matrix Again【最大费用最大流 && 经典建图】
- notifier chain — 内核通知链
- Airbnb入华,或重蹈Uber覆辙?
- RAID5和RAID10,哪种RAID更适合你(下)
- RAID5和RAID10,哪种RAID更适合你(上)
- hadoop1 & hadoop2 fair-schduler 配置和使用
- hadoop1 & hadoop2 fair-schduler 配置和使用
- LeetCode题解:Container With Most Water
- 对select()参数fdset的完整理解 http://blog.csdn.net/maray/article/details/8285775
- LeetCode题解:Climbing Stairs
- OGG-01705 input trail file './dirdat/fc075295' is greater than the size of the file
- Windows Server 2008 如何部署Domain Controller