æ¥æºï¼cnblogs.com/homejim/p/9909657.html
MyBatis 令人å欢çä¸å¤§ç¹æ§å°±æ¯å¨æ SQLã å¨ä½¿ç¨Â JDBC çè¿ç¨ä¸ï¼Â æ ¹æ®æ¡ä»¶è¿è¡Â SQL çæ¼æ¥æ¯å¾éº»ç¦ä¸å¾å®¹æåºéçã MyBatis å¨æ SQL çåºç°ï¼Â 解å³äºè¿ä¸ªéº»ç¦ã
MyBatiséè¿Â OGNL æ¥è¿è¡å¨æ SQL ç使ç¨çãç®åï¼Â å¨æ SQL æ¯æ以ä¸å ç§æ ç¾ï¼
1 æ°æ®åå¤
为äºåé¢çæ¼ç¤ºï¼ å建äºä¸ä¸ª Maven é¡¹ç® mybatis-dynamic, å建äºå¯¹åºçæ°æ®åºå表
DROP TABLE IF EXISTS `student`;CREATE TABLE `student` (`student_id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ç¼å·',`name` varchar(20) DEFAULT NULL COMMENT 'å§å',`phone` varchar(20) DEFAULT NULL COMMENT 'çµè¯',`email` varchar(50) DEFAULT NULL COMMENT 'é®ç®±',`sex` tinyint(4) DEFAULT NULL COMMENT 'æ§å«',`locked` tinyint(4) DEFAULT NULL COMMENT 'ç¶æ(0:æ£å¸¸,1:éå®)',`gmt_created` datetime DEFAULT CURRENT_TIMESTAMP COMMENT 'åå
¥æ°æ®åºçæ¶é´',`gmt_modified` datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'ä¿®æ¹çæ¶é´',`delete` int(11) DEFAULT NULL,
  PRIMARY KEY (`student_id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='å¦ç表';
对åºç项ç®ç»æ
2 if æ ç¾
if æ ç¾æ¯æ们æ常使ç¨çã å¨æ¥è¯¢ãå é¤ãæ´æ°çæ¶åå¾å¯è½ä¼ä½¿ç¨å°ãÂ å¿ é¡»ç»å test å±æ§èå使ç¨ã
2.1 å¨ WHERE æ¡ä»¶ä¸ä½¿ç¨ if æ ç¾
è¿æ¯å¸¸è§çä¸ç§ç°è±¡ï¼Â æ们å¨è¿è¡ææ¡ä»¶æ¥è¯¢çæ¶åï¼Â å¯è½ä¼æå¤ç§æ åµã
2.1.1 æ¥è¯¢æ¡ä»¶
æ ¹æ®è¾å ¥çå¦çä¿¡æ¯è¿è¡æ¡ä»¶æ£ç´¢
- å½åªè¾å ¥ç¨æ·åæ¶ï¼Â 使ç¨ç¨æ·åè¿è¡æ¨¡ç³æ£ç´¢ï¼
- å½åªè¾å ¥æ§å«æ¶ï¼ 使ç¨æ§å«è¿è¡å®å ¨å¹é
- å½ç¨æ·ååæ§å«é½åå¨æ¶ï¼ ç¨è¿ä¸¤ä¸ªæ¡ä»¶è¿è¡æ¥è¯¢å¹é æ¥è¯¢
2.1.2 å¨æ SQL
æ¥å£å½æ°
    List selectByStudentSelective(Student student);
对åºçå¨æ SQL
  <select id="selectByStudentSelective" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">select"Base_Column_List" />from studentwhere 1=1
    <if test="name != null and name !=''">and name like concat('%', #{name}, '%')if>
    <if test="sex != null">and sex=#{sex}if>select>
å¨æ¤Â SQL è¯å¥ä¸ï¼Â where 1=1 æ¯å¤æ¡ä»¶æ¼æ¥æ¶çå°æå·§ï¼Â åé¢çæ¡ä»¶æ¥è¯¢å°±å¯ä»¥é½ç¨Â and äºã
åæ¶ï¼ æ们添å äº if æ ç¾æ¥å¤çå¨æ SQL
    <if test="name != null and name !=''">
      and name like concat('%', #{name}, '%')if>
    <if test="sex != null">
      and sex=#{sex}if>
æ¤Â if æ ç¾ç test å±æ§å¼æ¯ä¸ä¸ªç¬¦å OGNL ç表达å¼ï¼Â 表达å¼å¯ä»¥æ¯Â true æ falseã å¦æ表达å¼è¿åçæ¯æ°å¼ï¼Â å0为 false, é 0 为 true;
2.1.3 æµè¯
     @Testpublic void selectByStudent() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student search = new Student();
        search.setName("æ");
        System.out.println("åªæååæ¶çæ¥è¯¢");
        List studentsByName = studentMapper.selectByStudentSelective(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        search.setName(null);
        search.setSex((byte) 1);
        System.out.println("åªææ§å«æ¶çæ¥è¯¢");
        List studentsBySex = studentMapper.selectByStudentSelective(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        System.out.println("å§ååæ§å«åæ¶åå¨çæ¥è¯¢");
        search.setName("æ");
        List studentsByNameAndSex = studentMapper.selectByStudentSelective(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        sqlSession.commit();
        sqlSession.close();
    }
åªæååæ¶çæ¥è¯¢ï¼ åéçè¯å¥åç»æ
æ¥è¯¢çæ¡ä»¶åªåéäº
where 1=1 and name like concat('%', ?, '%')Â
åªææ§å«æ¶çæ¥è¯¢ï¼ åéçè¯å¥åç»æ
æ¥è¯¢çæ¡ä»¶åªåéäº
where 1=1 and sex=?Â
å§ååæ§å«åæ¶åå¨çæ¥è¯¢ï¼ åéçè¯å¥åç»æ
æ¥è¯¢æ¡ä»¶
where 1=1 and name like concat('%', ?, '%') and sex=?Â
2.2 å¨ UPDATE æ´æ°åä¸ä½¿ç¨ if æ ç¾
ææ¶åæ们ä¸å¸ææ´æ°ææçå段ï¼Â åªæ´æ°æååçå段ã
2.2.1 æ´æ°æ¡ä»¶
åªæ´æ°æååçå段ï¼Â 空å¼ä¸æ´æ°ã
2.2.1 å¨æ SQL
æ¥å£æ¹æ³
    int updateByPrimaryKeySelective(Student record);
对åºç SQL
  "updateByPrimaryKeySelective" parameterType="com.homejim.mybatis.entity.Student">
    update student
    <set>
      <if test="name != null">
        `name` = #{name,jdbcType=VARCHAR},if>
      <if test="phone != null">
        phone = #{phone,jdbcType=VARCHAR},if>
      <if test="email != null">
        email = #{email,jdbcType=VARCHAR},if>
      <if test="sex != null">
        sex = #{sex,jdbcType=TINYINT},if>
      <if test="locked != null">
        locked = #{locked,jdbcType=TINYINT},if>
      <if test="gmtCreated != null">
        gmt_created = #{gmtCreated,jdbcType=TIMESTAMP},if>
      <if test="gmtModified != null">
        gmt_modified = #{gmtModified,jdbcType=TIMESTAMP},if>set>where student_id = #{studentId,jdbcType=INTEGER}
2.2.3 æµè¯
    @Testpublic void updateByStudentSelective() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setStudentId(1);
        student.setName("ææ");
        student.setPhone("13838438888");
        System.out.println(studentMapper.updateByPrimaryKeySelective(student));
        sqlSession.commit();
        sqlSession.close();
    }
ç»æå¦ä¸
2.3 å¨ INSERT å¨ææå ¥ä¸ä½¿ç¨ if æ ç¾
æ们æå ¥æ°æ®åºä¸çä¸æ¡è®°å½ï¼Â ä¸æ¯æ¯ä¸ä¸ªå段é½æå¼çï¼Â èæ¯å¨æååçã å¨è¿æ¶å使ç¨Â if æ ç¾ï¼Â å¯å¸®æ们解å³è¿ä¸ªé®é¢ã
2.3.1 æå ¥æ¡ä»¶
åªæé空å±æ§ææå ¥ã
2.3.2 å¨æSQL
æ¥å£æ¹æ³
    int insertSelective(Student record);
对åºçSQL
"insertSelective"Â parameterType="com.homejim.mybatis.entity.Student">
    insert into student"(" suffix=")" suffixOverrides=",">
      <if test="studentId != null">
        student_id,if>
      <if test="name != null">
        `name`,if>
      <if test="phone != null">
        phone,if>
      <if test="email != null">
        email,if>
      <if test="sex != null">
        sex,if>
      <if test="locked != null">
        locked,if>
      <if test="gmtCreated != null">
        gmt_created,if>
      <if test="gmtModified != null">
        gmt_modified,if>"values (" suffix=")" suffixOverrides=",">
      <if test="studentId != null">#{studentId,jdbcType=INTEGER},if>
      <if test="name != null">#{name,jdbcType=VARCHAR},if>
      <if test="phone != null">#{phone,jdbcType=VARCHAR},if>
      <if test="email != null">#{email,jdbcType=VARCHAR},if>
      <if test="sex != null">#{sex,jdbcType=TINYINT},if>
      <if test="locked != null">#{locked,jdbcType=TINYINT},if>
      <if test="gmtCreated != null">#{gmtCreated,jdbcType=TIMESTAMP},if>
      <if test="gmtModified != null">#{gmtModified,jdbcType=TIMESTAMP},if>
è¿ä¸ªÂ SQL 大家åºè¯¥å¾çæï¼Â æ¯ç«æ¯èªå¨çæçã
2.3.3 æµè¯
    @Testpublic void insertByStudentSelective() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setName("å°é£æº");
        student.setPhone("13838438899");
        student.setEmail("[email protected]");
        student.setLocked((byte) 0);
        System.out.println(studentMapper.insertSelective(student));
        sqlSession.commit();
        sqlSession.close();
    }
对åºçç»æ
SQL ä¸ï¼Â åªæé空çå段æè¿è¡äºæå ¥ã
3 choose æ ç¾
choose when otherwise æ ç¾å¯ä»¥å¸®æ们å®ç°Â if else çé»è¾ãä¸ä¸ªÂ choose æ ç¾è³å°æä¸ä¸ªÂ when, æå¤ä¸ä¸ªotherwiseã
ä¸é¢æ¯ä¸ä¸ªæ¥è¯¢çä¾åã
3.1 æ¥è¯¢æ¡ä»¶
å设 name å ·æå¯ä¸æ§ï¼ æ¥è¯¢ä¸ä¸ªå¦ç
- å½Â studen_id æå¼æ¶ï¼Â 使ç¨Â studen_id è¿è¡æ¥è¯¢ï¼
- å½Â studen_id 没æå¼æ¶ï¼Â 使ç¨Â name è¿è¡æ¥è¯¢ï¼
- å¦åè¿å空
3.2 å¨æSQL
æ¥å£æ¹æ³
    Student selectByIdOrName(Student record);
对åºçSQL
  <select id="selectByIdOrName" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">select"Base_Column_List" />from studentwhere 1=1
      <when test="studentId != null">and student_id=#{studentId}when>
      <when test="name != null and name != ''">and name=#{name}when>and 1=2select>
3.3 æµè¯
 @Testpublic void selectByIdOrName() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student student = new Student();
        student.setName("å°é£æº");
        student.setStudentId(1);
        Student studentById = studentMapper.selectByIdOrName(student);
        System.out.println("æ ID åæ ¹æ®Â ID è·å");
        System.out.println(ToStringBuilder.reflectionToString(studentById, ToStringStyle.MULTI_LINE_STYLE));
        student.setStudentId(null);
        Student studentByName = studentMapper.selectByIdOrName(student);
        System.out.println("没æ ID åæ ¹æ®Â name è·å");
        System.out.println(ToStringBuilder.reflectionToString(studentByName, ToStringStyle.MULTI_LINE_STYLE));
        student.setName(null);
        Student studentNull = studentMapper.selectByIdOrName(student);
        System.out.println("没æ ID å name, è¿å null");
        Assert.assertNull(studentNull);
        sqlSession.commit();
        sqlSession.close();
    }
æ ID åæ ¹æ® ID è·åï¼ ç»æ
没æ ID åæ ¹æ® name è·å
没æ ID å name, è¿å null
4 trim(setãwhere)
è¿ä¸ä¸ªå ¶å®è§£å³çæ¯ç±»ä¼¼çé®é¢ã å¦æ们å¨ååé¢ç[å¨Â WHERE æ¡ä»¶ä¸ä½¿ç¨Â if æ ç¾] SQL çæ¶åï¼Â where 1=1 è¿ä¸ªæ¡ä»¶æ们æ¯ä¸å¸æåå¨çã
4.1 where
4.1.1 æ¥è¯¢æ¡ä»¶
æ ¹æ®è¾å ¥çå¦çä¿¡æ¯è¿è¡æ¡ä»¶æ£ç´¢ã
- å½åªè¾å ¥ç¨æ·åæ¶ï¼Â 使ç¨ç¨æ·åè¿è¡æ¨¡ç³æ£ç´¢ï¼
- å½åªè¾å ¥æ§å«æ¶ï¼ 使ç¨æ§å«è¿è¡å®å ¨å¹é
- å½ç¨æ·ååæ§å«é½åå¨æ¶ï¼ ç¨è¿ä¸¤ä¸ªæ¡ä»¶è¿è¡æ¥è¯¢å¹é æ¥è¯¢
ä¸ä½¿ç¨Â where 1=1ã
4.1.2 å¨æ SQL
å¾æ¾ç¶ï¼ æ们è¦è§£å³è¿å 个é®é¢
å½æ¡ä»¶é½ä¸æ»¡è¶³æ¶ï¼Â æ¤æ¶Â SQL ä¸åºè¯¥è¦ä¸è½æ where ï¼Â å¦å导è´åºé
å½Â if ææ¡ä»¶æ»¡è¶³æ¶ï¼Â SQL ä¸éè¦æ whereï¼Â ä¸ç¬¬ä¸ä¸ªæç«ç if æ ç¾ä¸ç and | or çè¦å»æ
è¿æ¶åï¼Â æ们å¯ä»¥ä½¿ç¨Â where æ ç¾ã
æ¥å£æ¹æ³
    List selectByStudentSelectiveWhereTag(Student student);
对åºç SQL
  <select id="selectByStudentSelectiveWhereTag" resultMap="BaseResultMap" parameterType="com.homejim.mybatis.entity.Student">select"Base_Column_List" />from student
   <where>
    <if test="name != null and name !=''">and name like concat('%', #{name}, '%')if>
    <if test="sex != null">and sex=#{sex}if>where>select>
4.1.3 æµè¯
    @Testpublic void selectByStudentWhereTag() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        Student search = new Student();
        search.setName("æ");
        System.out.println("åªæååæ¶çæ¥è¯¢");
        List studentsByName = studentMapper.selectByStudentSelectiveWhereTag(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsByName.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        search.setSex((byte) 1);
        System.out.println("å§ååæ§å«åæ¶åå¨çæ¥è¯¢");
        List studentsBySex = studentMapper.selectByStudentSelectiveWhereTag(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsBySex.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        System.out.println("å§ååæ§å«é½ä¸åå¨æ¶æ¥è¯¢");
        search.setName(null);
        search.setSex(null);
        List studentsByNameAndSex = studentMapper.selectByStudentSelectiveWhereTag(search);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(studentsByNameAndSex.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        sqlSession.commit();
        sqlSession.close();
    }
åªæååæ¶çæ¥è¯¢, æ where
å§ååæ§å«åæ¶åå¨çæ¥è¯¢ï¼ æ where
å§ååæ§å«é½ä¸åå¨æ¶æ¥è¯¢, æ¤æ¶ï¼Â where ä¸ä¼ååºç°äºã
4.2 set
set æ ç¾ä¹ç±»ä¼¼ï¼ å¨ [2.2 å¨ UPDATE æ´æ°åä¸ä½¿ç¨ if æ ç¾] ä¸ï¼ å¦ææ们çæ¹æ³ updateByPrimaryKeySelective 没æ使ç¨
4.3 trim
set å whereÂ å ¶å®é½æ¯Â trim æ ç¾çä¸ç§ç±»åï¼Â 该两ç§åè½é½å¯ä»¥ä½¿ç¨Â trim æ ç¾è¿è¡å®ç°ã
4.3.1 trim æ¥è¡¨ç¤º where
å¦ä»¥ä¸ç where æ ç¾ï¼ æ们ä¹å¯ä»¥åæ
<trim prefix="where" prefixOverrides="AND |OR">trim>
表示å½Â trim ä¸å«æå 容æ¶ï¼Â æ·»å  whereï¼Â ä¸ç¬¬ä¸ä¸ªä¸ºÂ and æ or æ¶ï¼Â ä¼å°å ¶å»æã èå¦æ没æå 容ï¼Â åä¸æ·»å  whereã
4.3.2 trim æ¥è¡¨ç¤º set
ç¸åºçï¼ set æ ç¾å¯ä»¥å¦ä¸è¡¨ç¤º
<trim prefix="SET" suffixOverrides=",">trim>
表示å½Â trim ä¸å«æå 容æ¶ï¼Â æ·»å  setï¼Â ä¸æåçå 容为 , æ¶ï¼Â ä¼å°å ¶å»æã è没æå 容ï¼Â ä¸æ·»å  set
4.3.3 trim çå 个å±æ§
- prefix: å½Â trim å ç´ å å«æå 容æ¶ï¼Â å¢å  prefix ææå®çåç¼
- prefixOverrides: å½Â trim å ç´ å å«æå 容æ¶ï¼Â å»é¤Â prefixOverrides æå®ç åç¼
- suffix: å½Â trim å ç´ å å«æå 容æ¶ï¼Â å¢å  suffix ææå®çåç¼
- suffixOverridesï¼Â å½Â trim å ç´ å å«æå 容æ¶ï¼Â å»é¤Â suffixOverrides æå®çåç¼
5 foreach æ ç¾
foreach æ ç¾å¯ä»¥å¯¹æ°ç»ï¼Â Map æå®ç°Â Iterable æ¥å£ã
foreach ä¸æ以ä¸å 个å±æ§ï¼
- collection:Â å¿ å¡«ï¼Â éå/æ°ç»/Mapçå称.
- item: åéåã å³ä»è¿ä»£ç对象ä¸ååºçæ¯ä¸ä¸ªå¼
- index: 索å¼çå±æ§åã å½è¿ä»£ç对象为 Map æ¶ï¼Â 该å¼ä¸ºÂ Map ä¸ç Key.
- open: 循ç¯å¼å¤´çå符串
- close: 循ç¯ç»æçå符串
- separator: æ¯æ¬¡å¾ªç¯çåé符
å ¶ä»çæ¯è¾å¥½ç解ï¼Â collection ä¸çå¼åºè¯¥æä¹è®¾å®å¢ï¼
è·æ¥å£æ¹æ³ä¸çåæ°ç¸å ³ã
1. åªæä¸ä¸ªæ°ç»åæ°æéååæ°
é»è®¤æ åµï¼Â éåcollection=listï¼Â æ°ç»æ¯collection=array
æ¨èï¼Â 使ç¨Â @Param æ¥æå®åæ°çå称ï¼Â å¦æ们å¨åæ°å@Param("ids")ï¼Â å就填å collection=ids
2. å¤åæ°
å¤åæ°è¯·ä½¿ç¨ @Param æ¥æå®ï¼ å¦åSQLä¸ä¼å¾ä¸æ¹ä¾¿
3. åæ°æ¯Map
æå®ä¸ºÂ Map ä¸ç对åºç Key å³å¯ãÂ å ¶å®ä¸é¢ç @Param æåä¹æ¯è½¬å为 Map çã
4. åæ°æ¯å¯¹è±¡
使ç¨å±æ§.å±æ§å³å¯ã
5.1 å¨ where ä¸ä½¿ç¨ foreach
å¨Â whereæ¡ä»¶ä¸ä½¿ç¨ï¼Â å¦æidéåæ¥è¯¢ï¼Â æidéåå é¤çã
5.1.1 æ¥è¯¢æ¡ä»¶
æ们å¸ææ¥è¯¢ç¨æ·Â id éåä¸çææç¨æ·ä¿¡æ¯ã
5.1.2 å¨æ SQL
å½æ°æ¥å£
    List selectByStudentIdList(List ids);
å¯¹åº SQL
  <select id="selectByStudentIdList" resultMap="BaseResultMap">select"Base_Column_List" />from studentwhere student_id in
    <foreach collection="list" item="id" open="(" close=")" separator="," index="i">#{id}foreach>select>
5.1.3 æµè¯
    @Testpublic void selectByStudentIdList() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List ids = new LinkedList<>();
        ids.add(1);
        ids.add(3);
        List students = studentMapper.selectByStudentIdList(ids);for (int i = 0; i             System.out.println(ToStringBuilder.reflectionToString(students.get(i), ToStringStyle.MULTI_LINE_STYLE));
        }
        sqlSession.commit();
        sqlSession.close();
    }
ç»æ
5.2 foreach å®ç°æ¹éæå ¥
å¯ä»¥éè¿foreachæ¥å®ç°æ¹éæå ¥ã
5.2.1 å¨æSQL
æ¥å£æ¹æ³
    int insertList(List students);
对åºçSQL
  "insertList">insert into student(name, phone, email, sex, locked)
    values
    <foreach collection="list" item="student" separator=",">
      (#{student.name}, #{student.phone},#{student.email},#{student.sex},#{student.locked}
      )foreach>
5.2.2 æµè¯
    @Testpublic void insertList() {
        SqlSession sqlSession = null;
        sqlSession = sqlSessionFactory.openSession();
        StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
        List students = new LinkedList<>();
        Student stu1 = new Student();
        stu1.setName("æ¹é01");
        stu1.setPhone("13888888881");
        stu1.setLocked((byte) 0);
        stu1.setEmail("[email protected]");
        stu1.setSex((byte) 1);
        students.add(stu1);
        Student stu2 = new Student();
        stu2.setName("æ¹é02");
        stu2.setPhone("13888888882");
        stu2.setLocked((byte) 0);
        stu2.setEmail("[email protected]");
        stu2.setSex((byte) 0);
        students.add(stu2);
        System.out.println(studentMapper.insertList(students));
        sqlSession.commit();
        sqlSession.close();
    }
ç»æ
6 bind æ ç¾
bind æ ç¾æ¯éè¿Â OGNL 表达å¼å»å®ä¹ä¸ä¸ªä¸ä¸æçåéï¼Â è¿æ ·æ¹ä¾¿æ们使ç¨ã
å¦å¨ selectByStudentSelective æ¹æ³ä¸ï¼ æå¦ä¸
<if test="name != null and name !=''">and name like concat('%', #{name}, '%')if>
å¨Â MySQL ä¸ï¼Â 该å½æ°æ¯æå¤åæ°ï¼Â ä½å¨Â Oracle ä¸åªæ¯æ两个åæ°ã é£ä¹æ们å¯ä»¥ä½¿ç¨Â bind æ¥è®©è¯¥Â SQL 达å°æ¯æ两个æ°æ®åºçä½ç¨
<if test="name != null and name !=''">
     <bind name="nameLike" value="'%'+name+'%'"/>
     and name like #{nameLike}if>
æ´æ¹åçæ¥è¯¢ç»æå¦ä¸
7 代ç
使ç¨ç¤ºä¾ï¼
https://github.com/homejim/mybatis-examples
æè¿çæï¼
1. å享ä¸ä»½Javaæ¶æå¸å¦ä¹ èµæï¼
2. Mavenæå ¨æç¨ï¼çäºå¿ æï¼
3. Springä¸ç18个注解ï¼ä½ ä¼å 个ï¼
4. å¨Â IDEA ä¸è°è¯Â Bugï¼çæ¯å¤ªå害äºï¼ï¼
5. é¢è¯ä¸ç 10 个åï¼ä½ 踩è¿å 个ï¼
ââé¿æå ³æ³¨Java大å端ââ
æ³åæï¼è·åæµ·éé¢è¯é¢ï¼