ibatis的探索和应用,实现ibatis的CRUD功能,ibatis的优缺点
来源:互联网 发布:jmeter for mac安装 编辑:程序博客网 时间:2024/05/16 07:47
一、ibatis简介:ibatis是apache的一个开源项目,一个ORMapping解决方案,iBatis最大的特点就是小巧,上手快。如果不需要大多的功能,iBatis是能满足你的要求又是够灵活的最简单的解决方案。
二、搭建环境:
首先,添加jar包:分别是oracle的jar包,和iBatis的jar包,iBatis的jar包可以在网上直接下载,必须带有SqlMapClient类的jar包才行;
2,接下来,创建数据库Student,包含字段sid,sname,major.birth,score
3,建立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 ="com/itcast/SqlMap.properties"/><transactionManager type="JDBC"><dataSource type="SIMPLE"><property name="${driver}" value="JDBC.Driver"/><property name="${url}" value="JDBC.ConnectionURL"/><property name="${username}" value="JDBC.Username"/><property name="${password}" value="JDBC.Password"/></dataSource></transactionManager><properties resource ="com/itcast/Student.xml"/></SqlMapConfig>
上面的driver,url,username,password是不是很熟悉,jdbc中的老朋友
再配完上面的SqlMapConfig,那还要给对应的driver,url,username,password赋值啊,创建一个SqlMap.properties文件,写入
driver=oracle.jdbc.driver.OracleDriver
url=jdbc\:oracle\:thin\:@192.168.2.144\:8080\:1521\:orcl
username=aaa
password=123
3,完成上述,就可以建立Student实体类了,Get/Set一下
4,创建接口类IStudentDAO,完成CRUD操作
package com.itcast;import java.util.List;public interface IStudentDAO {public void addStudent(Student student);public void addStudentBySequence(Student student);public void deleteStudentById(int id);public void updateStudentById(Student student);public List<Student> queryAllStudent();public List<Student> queryStudentByName(String name);public Student queryStudentById(int id);}
5,创建Student.xml文件封装sql语句,这里也是体现ibatis高效简便的地方
<?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"><SqlMap><typeAlias alias="Student" type="com.itcast.Student"/><!--这里的id是对应到类中的,resultClass是返回值类型 --><select id="selectAllStudent" resultClass="Student">select * from student</select><select id="selectStudentById" parameterClass="int" resultClass ="Student">select *from studentwheresid="#sid"</select><!--插入,parameterClass参数的类型,#夹着表示这是一个需要传入的数据,而且最好是与插入的表列名想一致 --><insert id="insertStudent" parameterClass ="Student">insert into Student(sid, sname, major, birth, score)Values (#sid#,#sname#,#smajor#,#birth#,#score#)</insert><!-- 这里的#sid#,#夹着表示这是一个需要传入的数据,相当于占位符没有意义,parameterClass参数的类型 --><deleteid="deleteStudentById" parameterClass="int">delete fromStudent wheresid = #sid#</delete><!--parameterClass中的Student不区分大小写--><update id="updateStudentById" parameterClass="Student">updateStudentsetsname=#sname#,major=#major#,score=#score#,birth=#birth#where sid=#sid#</update><!--模糊查询,%$sname$% --><select id="selectStudentByName" parameterClass="String" resultClass="Student">selectsid,sname,major,birth,scorefromStudentwheresnamelike'%$sname$%'</select><!-- sql主键生成方式首先,要创建一个sql序列,create sequenceStudentPKSquence start with 1 increment by 1; --><insert id="insertStudentBySequence"parameterClass="Student"><selectKeyresultClass="int"keyProperty="sid">selectStudentPKSequence.nextValfromdual</selectKey>insertintoStudent(sid, sname, major, birth, score)Values (#sid#,#sname#,#smajor#,#birth#,#score#)</insert></SqlMap>
对应的实现类IStudentDAOImpl,
package com.itcast;import java.io.Reader;import java.sql.SQLException;import java.util.List;import com.ibatis.sqlmap.client.SqlMapClient;public class IStudentDAOImpl implements IStudentDAO {private static SqlMapClient sqlMapClient = null;static {try{//获取数据库连接数据Reader reader = com.ibatis.common.resources.Resources.getResourceAsReader("com.itcast.SqlMapConfig.xml");}catch(Exception e){e.printStackTrace();}}public void addStudent(Student student) {try {sqlMapClient.insert("insertStudent",student);} catch (SQLException e) {e.printStackTrace();}}public void addStudentBySequence(Student student) {try {sqlMapClient.insert("insertStudentBySequence",student);} catch (SQLException e) {e.printStackTrace();}}public void deleteStudentById(int id) {try {sqlMapClient.insert("deleteStudentById",id);} catch (SQLException e) {e.printStackTrace();}}public List<Student> queryAllStudent() {List<Student> studentList = null;try {//这里selectAllStudent就是Student.xml中的id,必须一一对应的studentList = sqlMapClient.queryForList("selectAllStudent");} catch (SQLException e) {e.printStackTrace();}return studentList;}public Student queryStudentById(int id) {Student student = null;try {student = (Student) sqlMapClient.queryForObject("selectStudentById");} catch (SQLException e) {e.printStackTrace();}return student;}public List<Student> queryStudentByName(String name) {List<Student> studentList = null;try {//这里selectAllStudent就是Student.xml中的id,必须一一对应的studentList = sqlMapClient.queryForList("selectStudentByName",name);} catch (SQLException e) {e.printStackTrace();}return studentList;}public void updateStudentById(Student student) {try { sqlMapClient.update("updateStudentById",student);} catch (SQLException e) {e.printStackTrace();}}}
综上,ibatis的CRUD已经结束了。
下面说下iBatis的优缺点吧:
与JDBC相比较,优点:
1)减少代码量61%;2)简单,架构级性能增强;3)sql语句与程序代码分离,(这是它最明显的优势)
4)简化项目中的分工;5)增强了可移植性;
缺点:1)sql需要自己写;2)参数数量只能一个,parameterClass
- ibatis的探索和应用,实现ibatis的CRUD功能,ibatis的优缺点
- ibatis的CRUD代码
- ibatis实现CRUD功能
- Ibatis的优缺点
- IBATIS的优缺点
- iBATIS的组件和实现功能
- ibatis的简单应用
- ibatis的CRUD代码 -------懂这些,ibatis差不多就会用了。
- ibatis的实现
- ibatis与hibernate的优缺点以及实用性功能比较
- IBatis设计实现的功能(1)
- ibatis #和$的区别
- ibatis#和$的用法
- ibatis #和$的区别
- IBATIS#和$的区别
- ibatis #和$ 的区别
- ibatis #和$的区别
- ibatis #和$的区别
- HTTP文件断点上传
- ORA-28056:Writing audit records to Windows EventLog failed
- 程序员学习能力提升三要素
- ORA-27101: shared memory realm does not exist
- 看热闹dede模板下载与设置
- ibatis的探索和应用,实现ibatis的CRUD功能,ibatis的优缺点
- thread safe vs reentrant
- linux块设备驱动
- Fragment error:The specified child already has a parent.
- 微务宝开发“微活动”功能助推企业活动营销
- 关于MFC程序不能定位输入点
- 字符数组与字符串常量
- Direct3D编程第一个程序框架
- oracle to_date函数的用法