ibatis3.0 增删改查 模糊查询 和 多个参数查询 demo
忙了三天,把ibatis3.0 进行了学习,下面是使用ibatis3.0 对数据库进行增删改查的操作,同时有模糊查询、按序列添加、多参数查询的操作,希望对大家有所帮助!
我马上就要用这个技术做项目了,还要加深掌握,愿与有志者一起加油!
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <properties resource="com/yhte/config/SqlMap.properties"/> <typeAliases> <typeAlias type="com.yhte.bean.Student" alias="student"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="com/yhte/bean/Student.xml"/> </mappers></configuration> <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="bin"> <select id="selectAllStudent" resultType="student"> select * from student </select> <select id="selectStudentById" parameterType="int" resultType="student"> select * from student where sid=#{sid} </select> <select id="selectStudentBy23" parameterType="java.util.Map" resultType="student"> select * from student where sname=#{sname} and major=#{major} </select> <!--注意 #sid#,#sname#,#major#,#birth#,#score#) 对应Student类中的get方法如getSid --> <insert id="insertStudent" parameterType="student"> insert into Student(sid, sname, major, birth, score) values (#{sid},#{sname},#{major},#{birth},#{score}) </insert> <delete id="deleteStudent" parameterType="int"> delete from student where sid=#{sid} </delete> <update id="updateStudent" parameterType="student"> update student set sid=#{sid}, sname=#{sname}, major=#{major}, birth=#{birth}, score=#{score} where sid=#{sid} </update> <select id="selectStudentByName" parameterType="String" resultType="student" > select sid,sname,major,birth,score from student where sname like #{sname} </select> <insert id="insertStudentBySequence" parameterType="student" > <selectKey resultType="int" keyProperty="sid" order="BEFORE" > select SEQ_student.nextVal from dual </selectKey> insert into Student(sid, sname, birth, major, score) values(#{sid}, #{sname}, #{birth}, #{major}, #{score}) </insert> </mapper>package com.yhte.util;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class IbatisSessionFactory { public SqlSessionFactory buildSqlSessionFactory() { try { String resource = "com/yhte/config/SqlMapConfig.xml"; Reader reader = Resources.getResourceAsReader(resource); return new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { System.out.println("failed to build SqlSessionFactory"); e.printStackTrace(); return null; } } public SqlSession getSession(){ return buildSqlSessionFactory().openSession(); } } package com.yhte.dao;import java.util.HashMap;import java.util.List;import java.util.Map;import org.apache.ibatis.session.SqlSession;import com.yhte.bean.Student;import com.yhte.util.IbatisSessionFactory;public class IStudentDAOImpl extends IbatisSessionFactory implements IStudentDAO { public void addStudent(Student student) { SqlSession s = null;try {s = this.getSession();s.insert("insertStudent", student);s.commit();System.out.println("添加成功"); } catch (Exception e) {System.out.println("添加失败"); e.printStackTrace();}finally{if(s != null)s.close();}} public void addStudentBySequence(Student student) {SqlSession s = null;try {//1 获取sid//2 插入sids = this.getSession();s.insert("insertStudentBySequence", student);s.commit();System.out.println("按序列主键添加成功");//System.out.println("sid="+student.getSid());} catch (Exception e) {System.out.println("按序列主键添加失败");e.printStackTrace();}}public void deleteStudentById(int id) { SqlSession s = null;try {s = this.getSession();s.delete("deleteStudent", id);s.commit();System.out.println("删除成功"); } catch (Exception e) {System.out.println("删除失败");e.printStackTrace();}finally{if(s!=null) s.close();}}public List<Student> queryAllStudent() { return this.getSession().selectList("bin.selectAllStudent"); }public Student queryStudentById(int id) {SqlSession s = null;Student student = null;try {s = this.getSession();student = (Student)s.selectOne("bin.selectStudentById", id);s.commit();System.out.println("按sid查询成功");} catch (Exception e) {System.out.println("按sid查询失败");e.printStackTrace();}return student;}public List<Student> queryStudentByName(String name) {SqlSession s = null;List<Student> studentList = null;try {s = this.getSession();studentList = s.selectList("selectStudentByName", name);System.out.println("模糊查询成功" );} catch (Exception e) {System.out.println("模糊查询失败");e.printStackTrace();}finally{s.close();}return studentList;}public void updateStudentById(Student student) {SqlSession s = null;try {s = this.getSession();s.update("updateStudent", student);s.commit();System.out.println("按sid更新成功");//this.getSession().update("updateStudent", student);//System.out.println(this.getSession().update("updateStudent", student));} catch (Exception e) {System.out.println("按sid更新失败");e.printStackTrace();}finally{if(s != null) s.close();}}public List<Student> queryStudentBy23(String sname, String major) {Map map = new HashMap();map.put("sname", sname);map.put("major", major);List<Student> studentList = null;SqlSession s =null;try {s = this.getSession();studentList = s.selectList("selectStudentBy23", map);System.out.println("按23查询成功");} catch (Exception e) {System.out.println("按23查询失败");e.printStackTrace();}finally{if(s != null) s.close();}return studentList;}}package com.yhte.test;import java.sql.Date;import com.yhte.bean.Student;import com.yhte.dao.IStudentDAO;import com.yhte.dao.IStudentDAOImpl;public class curdemo {/** * @param args */public static void main(String[] args) {IStudentDAO dao = new IStudentDAOImpl(); // dao.deleteStudentById(104);//view all/*for(Student student:dao.queryAllStudent()){System.out.println(student);}*/ //System.out.println(dao.queryStudentById(1));//add /*Student student = new Student();student.setSid(104);student.setSname("xuyissss");student.setMajor("gameffffff");student.setBirth(Date.valueOf("2008-02-03"));student.setScore(9);dao.addStudent(student);*///update /* Student student = new Student();student.setSid(103);student.setSname("xiaohuqq");student.setMajor("maoyi");student.setBirth(Date.valueOf("2009-02-03"));student.setScore(90);dao.updateStudentById(student);*///模糊查询/*for(Student student:dao.queryStudentByName("__ao%")){System.out.println(student);}*///addBySequence /*Student student = new Student();student.setSname("chaolin");student.setMajor("zejing");student.setBirth(Date.valueOf("2008-02-03"));student.setScore(9);dao.addStudentBySequence(student);*///按条件23查询//模糊查询for(Student student:dao.queryStudentBy23("chn","ze")){System.out.println(student);}}}