Mybatis的增删改查
以Stu为例,Stu.xml配置文件如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd" > <mapper namespace="stu"> <!-- 查询结果使用sql语句 --> <select id="findAll" resultType="Stu"> select * from stu </select> <!-- resultMap --> <resultMap type="Stu" id="stumap"> <id property="sid" column="s_id" /> <result property="sname" column="s_name" /> <result property="ssex" column="s_sex" /> <result property="sbirth" column="s_birth" /> </resultMap> <!-- resultMap对应resultMap的id值 --> <select id="findAll2" resultMap="stumap"> select * from stu </select> <!-- 参数查询1 普通类型(参数任意名) --> <select id="findparams1" resultMap="stumap" parameterType="int"> select * from stu s where s.s_id = #{id} </select> <!-- 参数查询2 类类型(参数类属性名) --> <select id="findparams2" resultMap="stumap" parameterType="Stu"> select * from stu s where s.s_id = #{sid} </select> <!-- 参数查询3 map类型(参数为key值) --> <!-- 用法最灵活!!! --> <select id="findparams3" resultMap="stumap" parameterType="map"> select * from stu s where s.s_id = #{id} </select> <!-- 查询一个 --> <select id="findOne" resultMap="stumap" parameterType="int"> select * from stu s where s.s_id = #{id} </select> <!-- 模糊查询1 --> <select id="findLike" resultMap="stumap" parameterType="String"> select * from stu s where s.s_name like "%"#{xxx}"%" </select> <!-- 模糊查询2 --> <select id="findLike2" resultMap="stumap" parameterType="String"> select * from stu s where s.s_name like #{xxx} </select> <!-- add --> <insert id="add" parameterType="Stu"> insert into stu(s_name,s_sex,s_birth) values(#{sname},#{ssex},#{sbirth}) </insert> <!-- update --> <update id="update" parameterType="Stu"> update stu set s_name=#{sname},s_sex=#{ssex},s_birth=#{sbirth} where s_id=#{sid} </update> <!-- delete --> <delete id="delete" parameterType="int"> delete from stu where s_id=#{xx} </delete> </mapper>相应的测试类如下:
package com.it.test; import java.io.Reader; import java.sql.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import com.it.entity.Stu; import com.it.util.MyBatisUtil; public class Test { // 查询所有 public static void findAll() { try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); SqlSession session = factory.openSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findAll"); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 查询所有2 public static void findAll2() { try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); SqlSession session = factory.openSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findAll2"); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 查询所有3 public static void findAll3() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findAll2"); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 根据参数查询1 public static void findParams1() { try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); SqlSession session = factory.openSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findparams1", 1); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 根据参数查询2 public static void findParams2() { try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); SqlSession session = factory.openSession(); Stu stu = new Stu(); stu.setSid(3); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findparams2", stu); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 根据参数查询3 public static void findParams3() { try { Reader reader = Resources.getResourceAsReader("mybatis.xml"); SqlSessionFactory factory = new SqlSessionFactoryBuilder() .build(reader); SqlSession session = factory.openSession(); Stu stu = new Stu(); stu.setSid(3); Map<String, Object> map = new HashMap<String, Object>(); map.put("id", 2); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findparams3", map); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 查询一个 public static void findOne() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 Stu s = session.selectOne("stu.findOne", 1); System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); session.close(); } catch (Exception e) { e.printStackTrace(); } } // 模糊查询1 public static void findLike() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findLike", "o"); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 模糊查询2 public static void findLike2() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 List<Stu> ls = session.selectList("stu.findLike2", "%y%"); for (Stu s : ls) { System.out.println(s.getSid() + "\t" + s.getSname() + "\t" + s.getSsex() + "\t" + s.getSbirth()); } session.close(); } catch (Exception e) { e.printStackTrace(); } } // 增加 public static void Add() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 Stu s = new Stu(0, "zake", "男", Date.valueOf("1990-10-11")); int r = session.insert("stu.add", s); System.out.println(r); // 提交 session.commit(); session.close(); } catch (Exception e) { e.printStackTrace(); } } // 修改 public static void Update() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 Stu s = new Stu(4, "zakey", "男", Date.valueOf("1995-10-11")); session.update("stu.update", s); session.commit(); session.close(); } catch (Exception e) { e.printStackTrace(); } } // 删除 public static void Delete() { try { SqlSession session = MyBatisUtil.getSession(); // 实体类映射文件对应的sql语句 session.delete("stu.delete", 4); session.commit(); session.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { /* * findParams1(); findParams2(); findParams3(); */ // findAll3(); // findOne(); // findLike(); Add(); } }