IBatis完成单表基本的数据库操作 模糊查询

首先要加入:ibatis-2.3.4.726.jar和数据库驱动包

1。新建一个数据库连接的属性文件如下:

driver = com.mysql.jdbc.Driver
url = jdbc:mysql://localhost:3306/mp
username = root
password = 123

2。iBatis的配置文件:sqlmap-config.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="sqlMap.properties" />
	<!-- 配置连接数据库的一些属性 -->
	<transactionManager type="JDBC">
		<dataSource type="SIMPLE">
			<!-- name部分固定冲部分,value中的${}里的名称要与sqlMap.properties属性文件中一样 -->
			<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/mengya/bean/Student.xml"/>
</sqlMapConfig>

 

sqlMap.properties就是上面的数据库连接的属性文件的名称。

3。构造一个我们数据库操作的对象:

package com.mengya.bean;

import java.sql.Date;

public class Student {
	private Integer stu_id;

	private String stu_name;

	private Integer stu_age;

	private float stu_score;

	private Date stu_birth;

	public Integer getStu_age() {
		return stu_age;
	}

	public void setStu_age(Integer stu_age) {
		this.stu_age = stu_age;
	}

	public Date getStu_birth() {
		return stu_birth;
	}

	public void setStu_birth(Date stu_birth) {
		this.stu_birth = stu_birth;
	}

	public Integer getStu_id() {
		return stu_id;
	}

	public void setStu_id(Integer stu_id) {
		this.stu_id = stu_id;
	}

	public String getStu_name() {
		return stu_name;
	}

	public void setStu_name(String stu_name) {
		this.stu_name = stu_name;
	}

	public float getStu_score() {
		return stu_score;
	}

	public void setStu_score(float stu_score) {
		this.stu_score = stu_score;
	}

	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append("stu_id=");
		sb.append(stu_id);
		sb.append("		stu_name=");
		sb.append(stu_name);
		sb.append("		stu_age=");
		sb.append(stu_age);
		sb.append("		stu_score=");
		sb.append(stu_score);
		sb.append("		stu_birth=");
		sb.append(stu_birth);
		return sb.toString();
	}
}

 4。构造一个iBatis的核心类SqlMapClient的对象的工具类:

import java.io.IOException;
import java.io.Reader;
import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

public class SqlMapClientUitl {
	private static SqlMapClientUitl sqlMapClientUitl;
	private static SqlMapClient sqlMapClient = null;
	private Reader reader = null;
	private SqlMapClientUitl() {
		try {
			reader = Resources.getResourceAsReader("sqlmap-config.xml");
			sqlMapClient = SqlMapClientBuilder.buildSqlMapClient(reader);
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public static SqlMapClientUitl createSqlMapClientUitl() {
		if (null == sqlMapClientUitl) {
			synchronized (SqlMapClientUitl.class) {
				if (null == sqlMapClientUitl) {
					sqlMapClientUitl = new SqlMapClientUitl();
				}
			}
		}
		return sqlMapClientUitl;
	}
	public SqlMapClient getSqlMapClient() {
		return sqlMapClient;
	}
}

 

5。构造IBatis操作数据库的SQL配置文件Student.xml

<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">

<sqlMap namespace="PhoneBook">
	
	<!-- 
		注意:1,SQL语句有返回值类型则需要配置上
			 2,传参只能传一个,多个可以封装成一个对象。那##之间的参数要与resultClass中的属相同
	 -->
		
	<typeAlias alias="Student" type="com.mengya.bean.Student" />

	<select id="queryAllStu" resultClass="Student">
		select * from student
	</select>

	<select id="queryStuByID" parameterClass="int"
		resultClass="Student">
		select * from student where stu_id =#id#
	</select>
	<!-- ##之间的参数要与Student中的属相同 -->
	<insert id="addStu" parameterClass="Student">
		insert into student
		values(#stu_id#,#stu_name#,#stu_age#,#stu_score#,#stu_birth#)
	</insert>

	<delete id="delStuByID" parameterClass="int">
		delete from student where stu_id = #id#
	</delete>

	<update id="updateStudentByStu" parameterClass="Student">
		update student set
		stu_name=#stu_name#,stu_age=#stu_age#,stu_score=#stu_score#,stu_birth=#stu_birth#
		where stu_id=#stu_id#
	</update>
	
	<!-- 模糊查询 -->
	<select id="queryStuByLikeName" parameterClass="String" resultClass="Student">
		select * from student where stu_name like '%$stu_name$%';
	</select>
	
	<!-- 模糊查询,推荐写法这样的查询条件要灵活些 -->
	<select id="queryStuByAllLikeName" parameterClass="String" resultClass="Student">
		select * from student where stu_name like '#stu_name#';
	</select>
	
</sqlMap>

 

6。完成的Student的DAO:

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

import com.ibatis.sqlmap.client.SqlMapClient;
import com.mengya.bean.Student;
import com.mengya.dao.inter.StudentDAO;
import com.mengya.util.SqlMapClientUitl;

public class StudentDAOImple implements StudentDAO {

	private SqlMapClient sqlMapClient;

	@SuppressWarnings("unchecked")
	public List<Student> queryAllStu() {
		List<Student> stuList = null;
		sqlMapClient = SqlMapClientUitl.createSqlMapClientUitl()
				.getSqlMapClient();
		try {
			stuList = sqlMapClient.queryForList("queryAllStu");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stuList;
	}

	public Student queryStuByID(int id) {
		sqlMapClient = SqlMapClientUitl.createSqlMapClientUitl()
				.getSqlMapClient();
		Student stu = null;
		try {
			stu = (Student) sqlMapClient.queryForObject("queryStuByID", id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return stu;
	}

	public void addStudent(Student stu) {
		sqlMapClient = SqlMapClientUitl.createSqlMapClientUitl()
				.getSqlMapClient();
		try {
			sqlMapClient.insert("addStu", stu);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void delStudent(int id) {
		sqlMapClient = SqlMapClientUitl.createSqlMapClientUitl().getSqlMapClient();
		try {
			sqlMapClient.delete("delStuByID",id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public void updateStu(Student stu) {
		sqlMapClient = SqlMapClientUitl.createSqlMapClientUitl().getSqlMapClient();
		try {
			sqlMapClient.update("updateStudentByStu", stu);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@SuppressWarnings("unchecked")
	public List<Student> queryStuByLikeName(String name) {
		sqlMapClient =SqlMapClientUitl.createSqlMapClientUitl().getSqlMapClient();
		try {
			return sqlMapClient.queryForList("queryStuByLikeName", name);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}

 

7。测试:

public class StuentTest {
	@Test
	public void queryAllStu(){
		StudentDAO studao = new StudentDAOImple();
		List<Student> stuList = studao.queryAllStu();
		for(Student stu : stuList){
			System.out.println(stu);
		}
	}
	
	@Test
	public void queryStuByID(){
		StudentDAO studao = new StudentDAOImple();
		System.out.println(studao.queryStuByID(102));
	}
	@Test
	public void addStu(){
		StudentDAO studao = new StudentDAOImple();
		Student stu = new Student();
		stu.setStu_id(null);
		stu.setStu_name("小酱油");
		stu.setStu_score(85f);
		stu.setStu_age(22);
		stu.setStu_birth(java.sql.Date.valueOf("1987-09-16"));
		studao.addStudent(stu);
	}
	@Test
	public void delStu(){
		StudentDAO studao = new StudentDAOImple();
		studao.delStudent(103);
	}
	@Test
	public void updStu(){
		StudentDAO studao = new StudentDAOImple();
		Student stu = studao.queryStuByID(102);
		stu.setStu_age(22);
		stu.setStu_score(100);
		studao.updateStu(stu);
	}
	
	@Test
	public void queryLikeName(){
		StudentDAO studao = new StudentDAOImple();
		List<Student> stuList = studao.queryStuByLikeName("小");
		for(Student stu : stuList){
			System.out.println(stu);
		}
	}
	
	@Test
	public void queryALLLikeName(){
		StudentDAO studao = new StudentDAOImple();
		List<Student> stuList = studao.queryStuByLikeName("%小%");
		for(Student stu : stuList){
			System.out.println(stu);
		}
	}
}

 

 

你可能感兴趣的:(sql,bean,mysql,ibatis,jdbc)