(1)由於在以前兩個案例已經詳細說明了mybatis-con.xml的配置了,這裏就不一一例舉了java
下面是關於用mybatis怎樣去動態的去映射sql,在案例中我都標明瞭怎麼使用,我就把主要的一些實現給你們例出來 sql
1.接口類數據庫
package edu.nf.mybatis2.dao; import edu.nf.mybatis2.entity.StudentInfo; import java.util.List; import java.util.Map; /** * @Author lance * @Date 2018/9/13 0013 */ public interface StuDaoInf { /** *批量刪除 */ void deleteStudent(List<Integer> list); /** *批量添加 */ void addBachStudent(List<StudentInfo> parms); /** *動態查詢 */ List<StudentInfo> dynamicQuery(Map<String,Object> parms); /** *動態選擇條件查詢 */ List<StudentInfo> dynamicQuery2(Map<String,Object> parms); }
二、接口實現類:apache
package edu.nf.mybatis2.dao.Impl; import edu.nf.mybatis2.dao.StuDaoInf; import edu.nf.mybatis2.entity.StudentInfo; import edu.nf.mybatis2.utils.MyBatisUtils; import org.apache.ibatis.session.SqlSession; import java.util.List; import java.util.Map; /** * 描述: * 實現類 * * @author lance * @create 2018-09-13 11:40 */ public class StuDaoImpl implements StuDaoInf { @Override public void deleteStudent(List<Integer> list) { SqlSession session = MyBatisUtils.getSqlSession(); try { StuDaoInf dao = session.getMapper(StuDaoInf.class); //批量刪除 dao.deleteStudent(list); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally { session.close(); } } @Override public void addBachStudent(List<StudentInfo> parms) { SqlSession session = MyBatisUtils.getSqlSession(); try { StuDaoInf daoInf = session.getMapper(StuDaoInf.class); //批量添加用戶信息 daoInf.addBachStudent(parms); session.commit(); } catch (Exception e) { e.printStackTrace(); session.rollback(); }finally { session.close(); } } @Override public List<StudentInfo> dynamicQuery(Map<String, Object> parms) { SqlSession session = MyBatisUtils.getSqlSession(); List<StudentInfo> list = null; try { StuDaoInf daoInf = session.getMapper(StuDaoInf.class); //動態查詢用戶信息 list = daoInf.dynamicQuery(parms); } catch (Exception e) { e.printStackTrace(); }finally { session.close(); } return list; } @Override public List<StudentInfo> dynamicQuery2(Map<String, Object> parms) { SqlSession session = MyBatisUtils.getSqlSession(); List<StudentInfo> list = null; try { StuDaoInf daoInf = session.getMapper(StuDaoInf.class); return list = daoInf.dynamicQuery2(parms); } catch (Exception e) { e.printStackTrace(); }finally { session.close(); } return list; } }
三、mapper配置映射(主要是看這裏)session
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="edu.nf.mybatis2.dao.StuDaoInf"> <resultMap id="stuMap" type="studentInfo"> <id property="sid" column="s_id"></id> <result property="stuName" column="s_name"></result> <result property="age" column="s_age"/> <result property="sex" column="s_sex"/> <result property="orgin" column="s_origin"/> <result property="tel" column="s_tel"/> <!--若是一個對象包含有另外一個對象的引用時,要使用association引用, property指向實體類中的字段名,resultMap指向另外一個實體類的resultMap的 id--> <association property="clazz" resultMap="classMap"/> </resultMap> <!--classMap--> <resultMap id="classMap" type="classInfo"> <id property="cid" column="c_id"></id> <result property="claName" column="c_name"/> </resultMap> <!--批量添加用戶, 參數是一個List集合的實體類型,遍歷集合將一個實體數據插入到數據庫中--> <insert id="addBachStudent" parameterType="java.util.List" > insert into stu_info(s_name,s_age,s_sex,s_origin,s_tel,c_id)values <foreach collection="list" item="stu" separator=","> (#{stu.stuName},#{stu.age},#{stu.sex},#{stu.orgin},#{stu.tel},#{stu.clazz.cid}) </foreach> </insert> <!--動態查詢拼接sql語句,根據map集合中的key值與數據庫中的值來進行匹配(test中的值就是集合中的 key值),若是key的值不爲空,就根據對應的value值來進行查詢,若是爲空就忽略if裏面的語句--> <select id="dynamicQuery" resultMap="stuMap" parameterType="java.util.Map"> select s_id ,s_name,s_age,s_sex,s_origin,s_tel,c_id from stu_info <where> <if test="sid !=null and sid !=''"> and s_id = #{sid} </if> <if test="stuName !=null and stuName !=''"> and s_name = #{stuName} </if> <if test="age !=null and age !=''"> and s_age = #{age} </if> <if test="sex !=null and sex !=''"> and s_sex = #{sex} </if> <if test="origin !=null and origin !=''"> and s_origin = #{origin} </if> </where> </select> <!--動態選擇條件查詢,otherwise是可選參數--> <select id="dynamicQuery2" parameterType="java.util.Map" resultMap="stuMap"> select s_id ,s_name,s_age,s_sex,s_origin,s_tel,c_id from stu_info <choose> <when test="sid != null"> where s_id = #{sid} </when> <when test="stuName !=null and stuName!=''"> where s_name = #{stuName} </when> <when test="age != null and age!=''"> where s_age = #{age} </when> <otherwise> order by s_id desc </otherwise> </choose> </select> <!--根據集合中的全部ID來批量刪除 完整語句 :delete from stu_info where 1=1 and s_id in(1,2,3,4) open:開始遍歷時的拼接字符串 close:結束時拼接的字符串 separator:用逗號隔開1,2,3條件,在最後一個條件時會自動刪除最後一個逗號--> <delete id="deleteStudent" parameterType="java.util.List" > delete from stu_info <where> <foreach collection="list" item="sid" open="and s_id in (" close=") " separator=","> #{sid} </foreach> </where> </delete> </mapper>
測試類mybatis
package edu.nf.mybatis2.dao.studaotest; import edu.nf.mybatis2.dao.Impl.StuDaoImpl; import edu.nf.mybatis2.dao.StuDaoInf; import edu.nf.mybatis2.entity.ClassInfo; import edu.nf.mybatis2.entity.StudentInfo; import org.junit.Test; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 描述: * 測試類 * * @author lance * @create 2018-09-13 14:40 */ public class StuDaoImplTest { //批量刪除 @Test public void testDeleteBachStudent(){ List<Integer> list = new ArrayList<>(); list.add(4); list.add(5); list.add(6); new StuDaoImpl().deleteStudent(list); } @Test public void testAddBachStudent(){ //批量添加 List<StudentInfo> list = new ArrayList<>(); StudentInfo stu1 = new StudentInfo(); stu1.setStuName("admin"); stu1.setAge(19); stu1.setSex(1); stu1.setOrgin("廣東信宜"); stu1.setTel("1110"); ClassInfo classInfo = new ClassInfo(); classInfo.setCid(1); stu1.setClazz(classInfo); StudentInfo stu2 = new StudentInfo(); stu2.setStuName("test"); stu2.setAge(19); stu2.setSex(1); stu2.setOrgin("廣東信宜電白"); stu2.setTel("1110"); ClassInfo classInfo1 = new ClassInfo(); classInfo.setCid(1); stu2.setClazz(classInfo); list.add(stu1); list.add(stu2); StuDaoInf stuDaoInf = new StuDaoImpl(); stuDaoInf.addBachStudent(list); } @Test public void testDynamicQuery(){ Map<String,Object> map = new HashMap<>(); map.put("stuName","stu1"); StuDaoInf daoInf = new StuDaoImpl(); //動態查詢 List<StudentInfo> list = daoInf.dynamicQuery(map); for (StudentInfo stu : list) { System.out.println(stu); } } @Test public void testDynamicQuery2(){ Map<String,Object> map = new HashMap<>(); map.put("stuName","stu1"); StuDaoInf daoInf = new StuDaoImpl(); //動態選擇條件查詢 List<StudentInfo> list = daoInf.dynamicQuery2(map); System.out.println(list.size()); for (StudentInfo stu : list) { System.out.println(stu); } } }