iBatis简单使用

刚入新公司,公司的项目是几年前的,Dao层用的是iBatis,于是乎就学习了iBatis,简单的介绍下,废话不多说,直接代码,这个是小Demo结构:引入iBatisjar包,mysql驱动包和junit测试,如下:

实体类Student.java具体内容:

package com.test;

import java.util.Date;

public class Student {
	
	private Integer id;
	
	private String name;
	
	private Integer age;
	
	private String hobby;
	
	private Date riqi;
	
	private Double score;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getHobby() {
		return hobby;
	}

	public void setHobby(String hobby) {
		this.hobby = hobby;
	}

	public Date getRiqi() {
		return riqi;
	}

	public void setRiqi(Date riqi) {
		this.riqi = riqi;
	}

	public Double getScore() {
		return score;
	}

	public void setScore(Double score) {
		this.score = score;
	}

	@Override
	public String toString() {
		return "Student [id=" + id + ", name=" + name + ", age=" + age + ", hobby=" + hobby + ", riqi=" + riqi
				+ ", score=" + score + "]";
	}
}

 

接口IStudentDao.java具体内容:

package com.test;

import java.sql.SQLException;
import java.util.List;

public interface IStudentDao {
	
	// 1.这个是完整的全部插入数据,需要主键返回判断数据有没有插入成功
	public Integer insertAllData(Student student) throws SQLException;
	
	// 2.这个是随机动态插入部分数据,需要主键返回判断数据有没有插入成功
	public Integer dynamicInsertData(Student student) throws SQLException;
	
	// 3.这个是更新全部数据,需要返回数字判断数据有没有更新成功
	public Integer updateAllDate(Student student) throws SQLException;
	
	// 4.这个是动态更新数据,需要返回数字判断有没有更新成功
	public Integer dynamicUpdateDate(Student student) throws SQLException;
	
	// 5.这个是根据任意条件查询数据
	public List<Student> queryAllDateByAnyCondition(Student student) throws SQLException;
}

 

接口实现类StudentDaoImpl.java具体内容:

 

package com.test;

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;

public class StudentDaoImpl implements IStudentDao {

	// 加载配置文件
	private static SqlMapClient sqlMapClient = null;
	static {
		try {
			Reader reader = Resources.getResourceAsReader("com/test/SqlMapConfig.xml");
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	// 1.这个是完整的全部插入数据,需要主键返回判断数据有没有插入成功
	@Override
	public Integer insertAllData(Student student) throws SQLException {
		return (Integer) sqlMapClient.insert("student.insertAllData", student);
	}

	// 2.这个是随机动态插入部分数据,需要主键返回判断数据有没有插入成功
	@Override
	public Integer dynamicInsertData(Student student) throws SQLException {
		return (Integer) sqlMapClient.insert("student.dynamicInsertData", student);
	}

	// 3.这个是更新全部数据,需要返回数字判断数据有没有更新成功
	@Override
	public Integer updateAllDate(Student student) throws SQLException {
		return sqlMapClient.update("student.updateAllDate", student);
	}

	// 4.这个是动态更新数据,需要返回数字判断有没有更新成功
	@Override
	public Integer dynamicUpdateDate(Student student) throws SQLException {
		return sqlMapClient.update("student.dynamicUpdateDate", student);
	}

	// 5.这个是根据任意条件查询数据
	@Override
	public List<Student> queryAllDateByAnyCondition(Student student) throws SQLException {
		return (List<Student>) sqlMapClient.queryForList("student.queryAllDateByAnyCondition", student);
	}

}

测试类TestDemo.java具体类容:

package com.test;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.junit.Test;

public class TestDemo {

	// 1.这个是完整的全部插入数据,需要主键返回判断数据有没有插入成功
	@Test
	public void insertAllData() throws SQLException {
		IStudentDao studentDao = new StudentDaoImpl();
		Student student = new Student();
		student.setName("小林子");
		student.setAge(20);
		student.setRiqi(new Date());
		student.setHobby("打篮球");
		student.setScore(20d);
		int id = studentDao.insertAllData(student);
		System.out.println(id);
		if (id > 0) {
			System.out.println("插入全部数据成功");
		} else {
			System.out.println("插入数据失败");
		}
	}

	// 2.这个是随机动态插入部分数据,需要主键返回判断数据有没有插入成功
	@Test
	public void dynamicInsertData() throws SQLException {
		Student student = new Student();
		student.setHobby("上网");
		student.setName("小红");
		student.setScore(67D);
		student.setRiqi(new Date());
		IStudentDao studentDao = new StudentDaoImpl();
		int id = studentDao.dynamicInsertData(student);
		System.out.println(id);
		if (id > 0) {
			System.out.println("动态插入数据成功");
		} else {
			System.out.println("动态插入数据失败");
		}
	}

	// 3.这个是更新全部数据,需要返回数字判断数据有没有更新成功
	@Test
	public void updateAllDate() throws SQLException {
		IStudentDao studentDao = new StudentDaoImpl();
		Student student = new Student();
		student.setName("小刚");
		student.setHobby("冲浪");
		student.setScore(700D);
		student.setRiqi(new Date());
		student.setId(3);
		student.setAge(10);
		int id = studentDao.updateAllDate(student);// 返回的是1
		System.out.println(id);
		if (id > 0) {
			System.out.println("全部数据更新成功");
		} else {
			System.out.println("全部数据更新失败");
		}
	}

	// 4.这个是动态更新数据,需要返回数字判断有没有更新成功
	@Test
	public void dynamicUpdateDate() throws SQLException {
		IStudentDao studentDao = new StudentDaoImpl();
		Student student = new Student();
		student.setName("小刚12");
		student.setHobby("冲浪");
		student.setScore(70D);
		student.setRiqi(new Date());
		student.setId(3);
		// student.setAge(10123);
		int id = studentDao.dynamicUpdateDate(student);// 返回的是1
		System.out.println(id);
		if (id > 0) {
			System.out.println("全部数据更新成功");
		} else {
			System.out.println("全部数据更新失败");
		}
	}

	// 5.这个是根据任意条件查询数据
	@Test
	public void queryAllDate() throws SQLException {
		IStudentDao studentDao = new StudentDaoImpl();
		Student student = new Student();
		student.setName("小刚");
		student.setId(4);
		List<Student> s = studentDao.queryAllDateByAnyCondition(student);
		System.out.println(s);
	}

}
 

数据库配置文件具体内容:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///web_01
username=root
password=root

 

SqlMapConfig.xml具体内容:

 

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig  PUBLIC "-//iBATIS.com//DTD SQL Map
Config 2.0/" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	
	<!-- 配置连接数据库的账号密码等信息 -->
	<properties resource="com/test/SqlMap.properties"/>
	
	<!-- 开启使用命名空间 -->
	<settings useStatementNamespaces="true"/>

	<!-- 读取配置连接数据库的账号密码等信息 -->
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<property value="${driver}" name="JDBC.Driver"/>
			<property value="${url}" name="JDBC.ConnectionURL"/>
			<property value="${username}" name="JDBC.Username"/>
			<property value="${password}" name="JDBC.Password"/>
		</dataSource>
	</transactionManager>
	
	<sqlMap resource="com/test/Student.xml"/>
	
</sqlMapConfig>

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 namespace="student">
	<!-- 1.这个是完整的全部插入数据,需要主键返回判断数据有没有插入成功,这里不能有resultClass = "int",不然会报错 -->
	<insert id="insertAllData" parameterClass="com.test.Student">
		insert into student
		(
		name, age, hobby, riqi, score
		)
		values
		(
		#name#,
		#age#, #hobby#, #riqi#, #score#
		)
		<selectKey keyProperty="id" resultClass="int">
			select @@IDENTITY as id
		</selectKey>
	</insert>

	<!-- // 2.这个是随机动态插入部分数据,需要主键返回判断数据有没有插入成功 -->
	<insert id="dynamicInsertData" parameterClass="com.test.Student">
		insert into `student`
		(
		<dynamic prepend=" ">
			<isNotEmpty property="name" prepend=",">`name`</isNotEmpty>
			<isNotEmpty property="age" prepend=",">`age`</isNotEmpty>
			<isNotEmpty property="hobby" prepend=",">`hobby`</isNotEmpty>
			<isNotEmpty property="riqi" prepend=",">riqi</isNotEmpty>
		</dynamic>
		)
		values
		(
		<dynamic prepend=" ">
			<isNotEmpty property="name" prepend=",">#name#</isNotEmpty>
			<isNotEmpty property="age" prepend=",">#age#</isNotEmpty>
			<isNotEmpty property="hobby" prepend=",">#hobby#</isNotEmpty>
			<isNotEmpty property="riqi" prepend=",">#riqi#</isNotEmpty>
		</dynamic>
		)
		<selectKey keyProperty="id" resultClass="int">
			select @@IDENTITY as id
		</selectKey>
	</insert>

	<!-- // 3.这个是更新全部数据,需要返回数字判断数据有没有更新成功 -->
	<update id="updateAllDate" parameterClass="com.test.Student">
		update student set
		name = #name#,
		age = #age#,
		hobby = #hobby#,
		riqi = #riqi#,
		score =#score#
		where id = #id#
	</update>

	<!-- // 4.这个是动态更新数据,需要返回数字判断有没有更新成功,方法一: -->
<!-- 	<update id="dynamicUpdateDate" parameterClass="com.test.Student"> update 
		student set 
			<isNotEmpty property="name">`name`=#name#,</isNotEmpty> 
			<isNotEmpty property="age">`age`=#age#,</isNotEmpty> 
			<isNotEmpty property="hobby">hobby=#hobby#,</isNotEmpty> 
			<isNotEmpty property="riqi">riqi=#riqi#,</isNotEmpty> 
			<isNotEmpty property="score">score=#score#</isNotEmpty> 
		where id = #id# </update> -->

	<!-- // 4.这个是动态更新数据,需要返回数字判断有没有更新成功,方法二: -->
	<update id="dynamicUpdateDate" parameterClass="com.test.Student">
		update student
		<dynamic prepend="set">
			<isNotNull prepend="," property="name">name=#name#</isNotNull>
			<isNotNull prepend="," property="age">age=#age#</isNotNull>
			<isNotNull prepend="," property="hobby">hobby=#hobby#</isNotNull>
			<isNotNull prepend="," property="riqi">riqi=#riqi#</isNotNull>
			<isNotNull prepend="," property="score">score=#score#</isNotNull>
		</dynamic>
		where id=#id#
	</update>
	
	<!-- // 5.这个是根据任意条件查询数据 -->
	<select id = "queryAllDateByAnyCondition" parameterClass="com.test.Student" resultClass="com.test.Student">
		select id, name, age, hobby, riqi, score from student
		<dynamic prepend="where">
			<isNotEmpty prepend="and" property="name">name = #name#</isNotEmpty>
			<isNotEmpty prepend="or " property="id">id = #id#</isNotEmpty >
		</dynamic>
	</select>
	
	

</sqlMap>

 

 

 

结果运行数据截图:

如有不足的地方请大家指出来

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值