Mybatis,怀疑--使用--感悟

本文介绍MyBatis框架的使用方式及特点,通过实例演示代码生成工具如何简化开发工作,包括实体类、示例类、Mapper接口及XML配置文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正当我疯狂地学习hibernate的时候,接到新项目的电话面试,说底层用的是Mybatis.因为只是听说过,在学习Hibernate的时候,也接触过一些理论方面的东西,因为都属于ORM框架,对两者进行过简单的比较,但都是偏向于Hibernate的角度。所以说,自己对Mybatis可以说是几乎什么都不知道。

    现在还记得那天接到面试电话时的场景,听到Mybatis,很熟悉,但其实是很陌生。关于mybatis,问
的一个问题就是:mybatis的使用方式?因为没有使用过,当然回答的是不知道了。接完面试的电话之
后,自己就开始上网寻找答案。
    Mybatis的使用方式包括两种:基于传统方式statementid;基于mapper接口方式调用。答案很具体,但对自己来说,很抽象,因为没有任何接触。
    那天,闲着没事,自己又查了查关于Hibernate与Mybatis的比较问题。总结如下:
    1. Hibernate的真正掌握要比Mybatis来得难些。Mybatis框架相对简单很容易上手,但也相对简陋些。
    2. Hibernate 与Mybatis都是流行的持久层开发框架,但Hibernate开发社区相对多热闹些,支持的工具也多,更新也快,当前最高版本4.1.8。 而Mybatis相对平静,工具较少,当前最高版本3.2。
    3. Hibernate和MyBatis都有相应的代码生成工具。可以生成简单基本的DAO层方法。
    4. 针对高级查询,Mybatis需要手动编写SQL语句,以及ResultMap。而Hibernate有良好的映射机
制,开发者无需关心SQL的生成与结果映射,可以更专注于业务流程。
    至今,自己仍然记得当时对Mybatis有着很大的一个疑问:居然要将SQL写在配置文件里,这是多么麻
烦的一件事情,要是让我开发,我看都不想看了吧。
    而如今,接触底层用mybatis的这个项目近两个月了,我对mybatis有了实践之后,不得不说一句自己很片面呀。下面就简单看看mybatis的使用:
    有一个好的开发工具,真的可以节省很多工作量。使用mybatis的代码生成工具, mybatis generator, Dao层的代码都不用我们写了,并且很大程度上都能满足我们的开发需要。
    每个表生成对应的文件包括:~Mapper.java文件/~.java文件/~Example.java文件以及~Mapper.xml文件。下面是某数据库表生成的对应的四个文件代码如下:
[java]  view plain  copy
  1. //广告地区实体,与数据库表对应  
  2. package com.gac.adv.entity;  
  3.   
  4. import com.gac.core.entity.BaseEntity;  
  5. import javax.validation.constraints.*;  
  6.   
  7. public class AdvertRegion extends BaseEntity {  
  8.     /** 
  9.      * 广告id 
  10.      */  
  11.     private Long advId;  
  12.   
  13.     /** 
  14.      * 地区id 
  15.      */  
  16.     private Long regionId;  
  17.   
  18.     /** 
  19.      * 是否删除0-否;1-是 
  20.      */  
  21.     private Boolean isDeleted;  
  22.   
  23.     /** 
  24.      * nullable:false,length:19 
  25.      */  
  26.     @NotNull  
  27.     public Long getAdvId() {  
  28.         return advId;  
  29.     }  
  30.   
  31.     public void setAdvId(Long advId) {  
  32.         this.advId = advId;  
  33.     }  
  34.   
  35.     /** 
  36.      * nullable:false,length:19 
  37.      */  
  38.     @NotNull  
  39.     public Long getRegionId() {  
  40.         return regionId;  
  41.     }  
  42.   
  43.     public void setRegionId(Long regionId) {  
  44.         this.regionId = regionId;  
  45.     }  
  46.   
  47.     /** 
  48.      * nullable:false,length:0 
  49.      */  
  50.     @NotNull  
  51.     public Boolean getIsDeleted() {  
  52.         return isDeleted;  
  53.     }  
  54.   
  55.     public void setIsDeleted(Boolean isDeleted) {  
  56.         this.isDeleted = isDeleted;  
  57.     }  
  58. }  
  59. //广告地区实体对应的Example  
  60. package com.gac.adv.entity.example;  
  61.   
  62. import com.gac.core.entity.example.AbstractExample;  
  63. import com.gac.core.entity.example.GeneratedCriteria;  
  64. import java.util.List;  
  65.   
  66. public class AdvertRegionExample extends AbstractExample {  
  67.   
  68.     @Override  
  69.     public Criteria or() {  
  70.         return (Criteria)super.or();  
  71.     }  
  72.   
  73.     @Override  
  74.     public Criteria createCriteria() {  
  75.         return (Criteria)super.createCriteria();  
  76.     }  
  77.   
  78.     @Override  
  79.     protected Criteria createCriteriaInternal() {  
  80.         return new Criteria();  
  81.     }  
  82.   
  83.     public class Criteria extends GeneratedCriteria {  
  84.   
  85.         protected Criteria() {  
  86.             super();  
  87.         }  
  88.   
  89.         public Criteria andAdvIdIsNull() {  
  90.             addCriterion("adv_id is null");  
  91.             return (Criteria) this;  
  92.         }  
  93.   
  94.         public Criteria andAdvIdIsNotNull() {  
  95.             addCriterion("adv_id is not null");  
  96.             return (Criteria) this;  
  97.         }  
  98.   
  99.         public Criteria andAdvIdEqualTo(Long value) {  
  100.             addCriterion("adv_id =", value, "advId");  
  101.             return (Criteria) this;  
  102.         }  
  103.   
  104.         public Criteria andAdvIdNotEqualTo(Long value) {  
  105.             addCriterion("adv_id <>", value, "advId");  
  106.             return (Criteria) this;  
  107.         }  
  108.   
  109.         public Criteria andAdvIdGreaterThan(Long value) {  
  110.             addCriterion("adv_id >", value, "advId");  
  111.             return (Criteria) this;  
  112.         }  
  113.   
  114.         public Criteria andAdvIdGreaterThanOrEqualTo(Long value) {  
  115.             addCriterion("adv_id >=", value, "advId");  
  116.             return (Criteria) this;  
  117.         }  
  118.   
  119.         public Criteria andAdvIdLessThan(Long value) {  
  120.             addCriterion("adv_id <", value, "advId");  
  121.             return (Criteria) this;  
  122.         }  
  123.   
  124.         public Criteria andAdvIdLessThanOrEqualTo(Long value) {  
  125.             addCriterion("adv_id <=", value, "advId");  
  126.             return (Criteria) this;  
  127.         }  
  128.   
  129.         public Criteria andAdvIdIn(List<Long> values) {  
  130.             addCriterion("adv_id in", values, "advId");  
  131.             return (Criteria) this;  
  132.         }  
  133.   
  134.         public Criteria andAdvIdNotIn(List<Long> values) {  
  135.             addCriterion("adv_id not in", values, "advId");  
  136.             return (Criteria) this;  
  137.         }  
  138.   
  139.         public Criteria andAdvIdBetween(Long value1, Long value2) {  
  140.             addCriterion("adv_id between", value1, value2, "advId");  
  141.             return (Criteria) this;  
  142.         }  
  143.   
  144.         public Criteria andAdvIdNotBetween(Long value1, Long value2) {  
  145.             addCriterion("adv_id not between", value1, value2, "advId");  
  146.             return (Criteria) this;  
  147.         }  
  148.   
  149.         public Criteria andRegionIdIsNull() {  
  150.             addCriterion("region_id is null");  
  151.             return (Criteria) this;  
  152.         }  
  153.   
  154.         public Criteria andRegionIdIsNotNull() {  
  155.             addCriterion("region_id is not null");  
  156.             return (Criteria) this;  
  157.         }  
  158.   
  159.         public Criteria andRegionIdEqualTo(Long value) {  
  160.             addCriterion("region_id =", value, "regionId");  
  161.             return (Criteria) this;  
  162.         }  
  163.   
  164.         public Criteria andRegionIdNotEqualTo(Long value) {  
  165.             addCriterion("region_id <>", value, "regionId");  
  166.             return (Criteria) this;  
  167.         }  
  168.   
  169.         public Criteria andRegionIdGreaterThan(Long value) {  
  170.             addCriterion("region_id >", value, "regionId");  
  171.             return (Criteria) this;  
  172.         }  
  173.   
  174.         public Criteria andRegionIdGreaterThanOrEqualTo(Long value) {  
  175.             addCriterion("region_id >=", value, "regionId");  
  176.             return (Criteria) this;  
  177.         }  
  178.   
  179.         public Criteria andRegionIdLessThan(Long value) {  
  180.             addCriterion("region_id <", value, "regionId");  
  181.             return (Criteria) this;  
  182.         }  
  183.   
  184.         public Criteria andRegionIdLessThanOrEqualTo(Long value) {  
  185.             addCriterion("region_id <=", value, "regionId");  
  186.             return (Criteria) this;  
  187.         }  
  188.   
  189.         public Criteria andRegionIdIn(List<Long> values) {  
  190.             addCriterion("region_id in", values, "regionId");  
  191.             return (Criteria) this;  
  192.         }  
  193.   
  194.         public Criteria andRegionIdNotIn(List<Long> values) {  
  195.             addCriterion("region_id not in", values, "regionId");  
  196.             return (Criteria) this;  
  197.         }  
  198.   
  199.         public Criteria andRegionIdBetween(Long value1, Long value2) {  
  200.             addCriterion("region_id between", value1, value2, "regionId");  
  201.             return (Criteria) this;  
  202.         }  
  203.   
  204.         public Criteria andRegionIdNotBetween(Long value1, Long value2) {  
  205.             addCriterion("region_id not between", value1, value2, "regionId");  
  206.             return (Criteria) this;  
  207.         }  
  208.   
  209.         public Criteria andIsDeletedIsNull() {  
  210.             addCriterion("is_deleted is null");  
  211.             return (Criteria) this;  
  212.         }  
  213.   
  214.         public Criteria andIsDeletedIsNotNull() {  
  215.             addCriterion("is_deleted is not null");  
  216.             return (Criteria) this;  
  217.         }  
  218.   
  219.         public Criteria andIsDeletedEqualTo(Boolean value) {  
  220.             addCriterion("is_deleted =", value, "isDeleted");  
  221.             return (Criteria) this;  
  222.         }  
  223.   
  224.         public Criteria andIsDeletedNotEqualTo(Boolean value) {  
  225.             addCriterion("is_deleted <>", value, "isDeleted");  
  226.             return (Criteria) this;  
  227.         }  
  228.   
  229.         public Criteria andIsDeletedGreaterThan(Boolean value) {  
  230.             addCriterion("is_deleted >", value, "isDeleted");  
  231.             return (Criteria) this;  
  232.         }  
  233.   
  234.         public Criteria andIsDeletedGreaterThanOrEqualTo(Boolean value) {  
  235.             addCriterion("is_deleted >=", value, "isDeleted");  
  236.             return (Criteria) this;  
  237.         }  
  238.   
  239.         public Criteria andIsDeletedLessThan(Boolean value) {  
  240.             addCriterion("is_deleted <", value, "isDeleted");  
  241.             return (Criteria) this;  
  242.         }  
  243.   
  244.         public Criteria andIsDeletedLessThanOrEqualTo(Boolean value) {  
  245.             addCriterion("is_deleted <=", value, "isDeleted");  
  246.             return (Criteria) this;  
  247.         }  
  248.   
  249.         public Criteria andIsDeletedIn(List<Boolean> values) {  
  250.             addCriterion("is_deleted in", values, "isDeleted");  
  251.             return (Criteria) this;  
  252.         }  
  253.   
  254.         public Criteria andIsDeletedNotIn(List<Boolean> values) {  
  255.             addCriterion("is_deleted not in", values, "isDeleted");  
  256.             return (Criteria) this;  
  257.         }  
  258.   
  259.         public Criteria andIsDeletedBetween(Boolean value1, Boolean value2) {  
  260.             addCriterion("is_deleted between", value1, value2, "isDeleted");  
  261.             return (Criteria) this;  
  262.         }  
  263.   
  264.         public Criteria andIsDeletedNotBetween(Boolean value1, Boolean value2) {  
  265.             addCriterion("is_deleted not between", value1, value2, "isDeleted");  
  266.             return (Criteria) this;  
  267.         }  
  268.     }  
  269. }  
  270. //广告地区Mapper  
  271. package com.gac.adv.dao;  
  272.   
  273. import java.util.List;  
  274.   
  275. import com.gac.adv.entity.AdvertRegion;  
  276. import com.gac.adv.entity.example.AdvertRegionExample;  
  277. import com.gac.core.dao.BaseMapper;  
  278.   
  279. public interface AdvertRegionMapper extends BaseMapper<AdvertRegion, AdvertRegionExample> {  
  280.     //自定义的批量插入方法  
  281.     void insertBatch(List<AdvertRegion> advertRegion);  
  282. }  
[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <!-- 广告地区表Xml -->  
  2. <?xml version="1.0" encoding="UTF-8" ?>  
  3. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >  
  4. <mapper namespace="com.gac.adv.dao.AdvertRegionMapper" >  
  5.   <resultMap id="BaseResultMap" type="com.gac.adv.entity.AdvertRegion" >  
  6.     <id column="id" property="id" jdbcType="BIGINT" />  
  7.     <result column="adv_id" property="advId" jdbcType="BIGINT" />  
  8.     <result column="region_id" property="regionId" jdbcType="BIGINT" />  
  9.     <result column="is_deleted" property="isDeleted" jdbcType="BIT" />  
  10.     <result column="create_by" property="createBy" jdbcType="BIGINT" />  
  11.     <result column="create_time" property="createTime" jdbcType="TIMESTAMP" />  
  12.     <result column="update_by" property="updateBy" jdbcType="BIGINT" />  
  13.     <result column="update_time" property="updateTime" jdbcType="TIMESTAMP" />  
  14.   </resultMap>  
  15.   <sql id="Example_Where_Clause" >  
  16.     <where >  
  17.       <foreach collection="oredCriteria" item="criteria" separator="or" >  
  18.         <if test="criteria.valid" >  
  19.           <trim prefix="(" suffix=")" prefixOverrides="and" >  
  20.             <foreach collection="criteria.criteria" item="criterion" >  
  21.               <choose >  
  22.                 <when test="criterion.noValue" >  
  23.                   and ${criterion.condition}  
  24.                 </when>  
  25.                 <when test="criterion.singleValue" >  
  26.                   and ${criterion.condition} #{criterion.value}  
  27.                 </when>  
  28.                 <when test="criterion.betweenValue" >  
  29.                   and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}  
  30.                 </when>  
  31.                 <when test="criterion.listValue" >  
  32.                   and ${criterion.condition}  
  33.                   <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >  
  34.                     #{listItem}  
  35.                   </foreach>  
  36.                 </when>  
  37.               </choose>  
  38.             </foreach>  
  39.           </trim>  
  40.         </if>  
  41.       </foreach>  
  42.     </where>  
  43.   </sql>  
  44.   <sql id="Update_By_Example_Where_Clause" >  
  45.     <where >  
  46.       <foreach collection="example.oredCriteria" item="criteria" separator="or" >  
  47.         <if test="criteria.valid" >  
  48.           <trim prefix="(" suffix=")" prefixOverrides="and" >  
  49.             <foreach collection="criteria.criteria" item="criterion" >  
  50.               <choose >  
  51.                 <when test="criterion.noValue" >  
  52.                   and ${criterion.condition}  
  53.                 </when>  
  54.                 <when test="criterion.singleValue" >  
  55.                   and ${criterion.condition} #{criterion.value}  
  56.                 </when>  
  57.                 <when test="criterion.betweenValue" >  
  58.                   and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}  
  59.                 </when>  
  60.                 <when test="criterion.listValue" >  
  61.                   and ${criterion.condition}  
  62.                   <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >  
  63.                     #{listItem}  
  64.                   </foreach>  
  65.                 </when>  
  66.               </choose>  
  67.             </foreach>  
  68.           </trim>  
  69.         </if>  
  70.       </foreach>  
  71.     </where>  
  72.   </sql>  
  73.   <sql id="Base_Column_List" >  
  74.     id, adv_id, region_id, is_deleted, create_by, create_time, update_by, update_time  
  75.   </sql>  
  76.   <select id="selectByExample" resultMap="BaseResultMap" parameterType="com.gac.adv.entity.example.AdvertRegionExample" >  
  77.     select  
  78.     <if test="distinct" >  
  79.       distinct  
  80.     </if>  
  81.     <include refid="Base_Column_List" />  
  82.     from cms_adv_region  
  83.     <if test="_parameter != null" >  
  84.       <include refid="Example_Where_Clause" />  
  85.     </if>  
  86.     <if test="orderByClause != null" >  
  87.       order by ${orderByClause}  
  88.     </if>  
  89.     <if test="pageStart!=null and pageStart gt -1" >  
  90.        limit ${pageStart},${pageSize}  
  91.     </if>  
  92.   </select>  
  93.   <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >  
  94.     select   
  95.     <include refid="Base_Column_List" />  
  96.     from cms_adv_region  
  97.     where id = #{id,jdbcType=BIGINT}  
  98.   </select>  
  99.   <delete id="deleteByPrimaryKey" parameterType="java.lang.Long" >  
  100.     delete from cms_adv_region  
  101.     where id = #{id,jdbcType=BIGINT}  
  102.   </delete>  
  103.   <delete id="deleteByExample" parameterType="com.gac.adv.entity.example.AdvertRegionExample" >  
  104.     delete from cms_adv_region  
  105.     <if test="_parameter != null" >  
  106.       <include refid="Example_Where_Clause" />  
  107.     </if>  
  108.   </delete>  
  109.   <insert id="insert" parameterType="com.gac.adv.entity.AdvertRegion">  
  110.     insert into cms_adv_region (id, adv_id, region_id,   
  111.       is_deleted, create_by, create_time,   
  112.       update_by, update_time)  
  113.     values (#{id,jdbcType=BIGINT}, #{advId,jdbcType=BIGINT}, #{regionId,jdbcType=BIGINT},   
  114.       #{isDeleted,jdbcType=BIT}, #{createBy,jdbcType=BIGINT}, #{createTime,jdbcType=TIMESTAMP},   
  115.       #{updateBy,jdbcType=BIGINT}, #{updateTime,jdbcType=TIMESTAMP})  
  116.   </insert>  
  117.   <insert id="insertSelective" parameterType="com.gac.adv.entity.AdvertRegion" >  
  118.     insert into cms_adv_region  
  119.     <trim prefix="(" suffix=")" suffixOverrides="," >  
  120.       <if test="id != null" >  
  121.         id,  
  122.       </if>  
  123.       <if test="advId != null" >  
  124.         adv_id,  
  125.       </if>  
  126.       <if test="regionId != null" >  
  127.         region_id,  
  128.       </if>  
  129.       <if test="isDeleted != null" >  
  130.         is_deleted,  
  131.       </if>  
  132.       <if test="createBy != null" >  
  133.         create_by,  
  134.       </if>  
  135.       <if test="createTime != null" >  
  136.         create_time,  
  137.       </if>  
  138.       <if test="updateBy != null" >  
  139.         update_by,  
  140.       </if>  
  141.       <if test="updateTime != null" >  
  142.         update_time,  
  143.       </if>  
  144.     </trim>  
  145.     <trim prefix="values (" suffix=")" suffixOverrides="," >  
  146.       <if test="id != null" >  
  147.         #{id,jdbcType=BIGINT},  
  148.       </if>  
  149.       <if test="advId != null" >  
  150.         #{advId,jdbcType=BIGINT},  
  151.       </if>  
  152.       <if test="regionId != null" >  
  153.         #{regionId,jdbcType=BIGINT},  
  154.       </if>  
  155.       <if test="isDeleted != null" >  
  156.         #{isDeleted,jdbcType=BIT},  
  157.       </if>  
  158.       <if test="createBy != null" >  
  159.         #{createBy,jdbcType=BIGINT},  
  160.       </if>  
  161.       <if test="createTime != null" >  
  162.         #{createTime,jdbcType=TIMESTAMP},  
  163.       </if>  
  164.       <if test="updateBy != null" >  
  165.         #{updateBy,jdbcType=BIGINT},  
  166.       </if>  
  167.       <if test="updateTime != null" >  
  168.         #{updateTime,jdbcType=TIMESTAMP},  
  169.       </if>  
  170.     </trim>  
  171.   </insert>  
  172.     
  173.  <insert id="insertBatch" parameterType="java.util.ArrayList" >  
  174.     insert into cms_adv_region (adv_id, region_id, is_deleted, create_by, create_time, update_by, update_time)  
  175.       values         
  176.      <foreach collection="list" index = "index" item = "item" separator="," >       
  177.       <trim prefix="(" suffix=")" suffixOverrides="," >  
  178.       <choose>  
  179.       <when test="item.advId != null" >  
  180.         #{item.advId,jdbcType=BIGINT},  
  181.       </when>  
  182.       <otherwise>  
  183.           '',  
  184.         </otherwise>  
  185.       </choose>  
  186.       <choose>  
  187.       <when test="item.regionId != null" >  
  188.         #{item.regionId,jdbcType=BIGINT},  
  189.       </when>  
  190.        <otherwise>  
  191.           0,  
  192.         </otherwise>  
  193.       </choose>  
  194.       <choose>  
  195.       <when test="item.isDeleted != null" >  
  196.         #{item.isDeleted,jdbcType=BIT},  
  197.       </when>  
  198.        <otherwise>  
  199.           0,  
  200.         </otherwise>  
  201.       </choose>  
  202.       <choose>  
  203.       <when test="item.createBy != null" >  
  204.         #{item.createBy,jdbcType=BIGINT},  
  205.       </when>  
  206.       <otherwise>  
  207.           0,  
  208.         </otherwise>  
  209.       </choose>  
  210.       <choose>  
  211.       <when test="item.createTime != null" >  
  212.         #{item.createTime,jdbcType=TIMESTAMP},  
  213.       </when>  
  214.        <otherwise>  
  215.          now(),  
  216.         </otherwise>  
  217.       </choose>  
  218.       <choose>  
  219.       <when test="item.updateBy != null" >  
  220.         #{item.updateBy,jdbcType=BIGINT},  
  221.       </when>  
  222.        <otherwise>  
  223.           0,  
  224.         </otherwise>  
  225.       </choose>  
  226.       <choose>  
  227.       <when test="item.updateTime != null" >  
  228.         #{item.updateTime,jdbcType=TIMESTAMP},  
  229.       </when>  
  230.        <otherwise>  
  231.          now(),  
  232.         </otherwise>  
  233.       </choose>  
  234.       </trim>      
  235.      </foreach>          
  236.   </insert>  
  237.     
  238.   <select id="countByExample" parameterType="com.gac.adv.entity.example.AdvertRegionExample" resultType="java.lang.Integer" >  
  239.     select count(*) from cms_adv_region  
  240.     <if test="_parameter != null" >  
  241.       <include refid="Example_Where_Clause" />  
  242.     </if>  
  243.   </select>  
  244.   <update id="updateByExampleSelective" parameterType="map" >  
  245.     update cms_adv_region  
  246.     <set >  
  247.       <if test="record.id != null" >  
  248.         id = #{record.id,jdbcType=BIGINT},  
  249.       </if>  
  250.       <if test="record.advId != null" >  
  251.         adv_id = #{record.advId,jdbcType=BIGINT},  
  252.       </if>  
  253.       <if test="record.regionId != null" >  
  254.         region_id = #{record.regionId,jdbcType=BIGINT},  
  255.       </if>  
  256.       <if test="record.isDeleted != null" >  
  257.         is_deleted = #{record.isDeleted,jdbcType=BIT},  
  258.       </if>  
  259.       <if test="record.createBy != null" >  
  260.         create_by = #{record.createBy,jdbcType=BIGINT},  
  261.       </if>  
  262.       <if test="record.createTime != null" >  
  263.         create_time = #{record.createTime,jdbcType=TIMESTAMP},  
  264.       </if>  
  265.       <if test="record.updateBy != null" >  
  266.         update_by = #{record.updateBy,jdbcType=BIGINT},  
  267.       </if>  
  268.       <if test="record.updateTime != null" >  
  269.         update_time = #{record.updateTime,jdbcType=TIMESTAMP},  
  270.       </if>  
  271.     </set>  
  272.     <if test="_parameter != null" >  
  273.       <include refid="Update_By_Example_Where_Clause" />  
  274.     </if>  
  275.   </update>  
  276.   <update id="updateByExample" parameterType="map" >  
  277.     update cms_adv_region  
  278.     set id = #{record.id,jdbcType=BIGINT},  
  279.       adv_id = #{record.advId,jdbcType=BIGINT},  
  280.       region_id = #{record.regionId,jdbcType=BIGINT},  
  281.       is_deleted = #{record.isDeleted,jdbcType=BIT},  
  282.       create_by = #{record.createBy,jdbcType=BIGINT},  
  283.       create_time = #{record.createTime,jdbcType=TIMESTAMP},  
  284.       update_by = #{record.updateBy,jdbcType=BIGINT},  
  285.       update_time = #{record.updateTime,jdbcType=TIMESTAMP}  
  286.     <if test="_parameter != null" >  
  287.       <include refid="Update_By_Example_Where_Clause" />  
  288.     </if>  
  289.   </update>  
  290.   <update id="updateByPrimaryKeySelective" parameterType="com.gac.adv.entity.AdvertRegion" >  
  291.     update cms_adv_region  
  292.     <set >  
  293.       <if test="advId != null" >  
  294.         adv_id = #{advId,jdbcType=BIGINT},  
  295.       </if>  
  296.       <if test="regionId != null" >  
  297.         region_id = #{regionId,jdbcType=BIGINT},  
  298.       </if>  
  299.       <if test="isDeleted != null" >  
  300.         is_deleted = #{isDeleted,jdbcType=BIT},  
  301.       </if>  
  302.       <if test="createBy != null" >  
  303.         create_by = #{createBy,jdbcType=BIGINT},  
  304.       </if>  
  305.       <if test="createTime != null" >  
  306.         create_time = #{createTime,jdbcType=TIMESTAMP},  
  307.       </if>  
  308.       <if test="updateBy != null" >  
  309.         update_by = #{updateBy,jdbcType=BIGINT},  
  310.       </if>  
  311.       <if test="updateTime != null" >  
  312.         update_time = #{updateTime,jdbcType=TIMESTAMP},  
  313.       </if>  
  314.     </set>  
  315.     where id = #{id,jdbcType=BIGINT}  
  316.   </update>  
  317.   <update id="updateByPrimaryKey" parameterType="com.gac.adv.entity.AdvertRegion" >  
  318.     update cms_adv_region  
  319.     set adv_id = #{advId,jdbcType=BIGINT},  
  320.       region_id = #{regionId,jdbcType=BIGINT},  
  321.       is_deleted = #{isDeleted,jdbcType=BIT},  
  322.       create_by = #{createBy,jdbcType=BIGINT},  
  323.       create_time = #{createTime,jdbcType=TIMESTAMP},  
  324.       update_by = #{updateBy,jdbcType=BIGINT},  
  325.       update_time = #{updateTime,jdbcType=TIMESTAMP}  
  326.     where id = #{id,jdbcType=BIGINT}  
  327.   </update>  
  328. </mapper>  
    有了这些代码,我们就可以直接在业务逻辑层直接写增删改查数据表的代码,如新增一条记录:
[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /** 
  2.  * 增加广告投放城市 
  3.  * @param userId 
  4.  * @param advId 
  5.  * @param regionIds 
  6.  */  
  7. @Transactional(rollbackFor = Exception.class)  
  8. public void addAdvertRegion(Long userId,Long advId,List<Long> regionIds){  
  9.     List<AdvertRegion> advertRegions=new ArrayList<AdvertRegion>();  
  10.     for (Long regionId : regionIds) {  
  11.         AdvertRegion advertRegion=new AdvertRegion();  
  12.         advertRegion.setCreateBy(userId);  
  13.         advertRegion.setCreateTime(new Date());  
  14.         advertRegion.setRegionId(regionId);  
  15.         advertRegion.setAdvId(advId);  
  16.         advertRegion.setUpdateBy(userId);  
  17.         advertRegion.setUpdateTime(new Date());  
  18.         advertRegions.add(advertRegion);  
  19.     }  
  20.     advertRegionMapper.insertBatch(advertRegions);        
  21. }  
    上面生成的代码,可以满足基本的增删改查,如果是比较复杂的一些操作,如批量更新/插入/复杂的sql等,我们可以自己在Xml中定义一个新的方法即可,也是非常方便的。
    在项目的使用过程中,也遇到过一些问题,比如想要获取新插入记录的id,想要按某个字段去排序,想要返回分页的数据,这些都可以从mybatis着手去解决,而且非常简单,并不需要繁琐的代码,繁琐的程序。
    现在想想,当你不知道某个东西的时候,真的不能妄下定论,因为,你还不知道等到你了解了接触了它之后,会产生怎样的火花。但我们可以怀疑,可以猜想,这样最后你得到的答案也一定会带给自己更深
刻的感受,就好像当初我对Mybatis的怀疑一样。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值