ibatis的探索和应用,实现ibatis的CRUD功能,ibatis的优缺点

一、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 	student
		where	sid="#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参数的类型 -->
	<delete	id="deleteStudentById" parameterClass="int">
		delete
	    from	Student 
		where	sid = #sid#
	</delete>
	
	<!--parameterClass中的Student不区分大小写-->
	<update id="updateStudentById" parameterClass="Student">
		update	Student
		set		sname=#sname#,major=#major#,score=#score#,birth=#birth#
		where 	sid=#sid#
	</update>
	
	<!--模糊查询,%$sname$% -->
	<select id="selectStudentByName" parameterClass="String" resultClass="Student">
		select	sid,sname,major,birth,score
		from	Student
		where	sname	like	'%$sname$%'
	</select>
	
	<!-- sql主键生成方式
		首先,要创建一个sql序列,create sequence	StudentPKSquence start with 1 increment by 1;
	 -->
	<insert id="insertStudentBySequence"	parameterClass="Student">
		<selectKey	resultClass="int"	keyProperty="sid">
			select	StudentPKSequence.nextVal
			from	dual
		</selectKey>
		insert	into	Student(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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值