mybatis应知应会

本篇讲述的内容有:

  • mybatis输入输出类型
  • bean属性和表字段不一致问题
  • mybatis参数传递问题
  • mybatis常用的标签
  • mybatis返回主键id
  • mybatis逆向工程

输入/输出类型(传入参数和返回值问题)

<select id="getUsersByStatus" parameterType="java.lang.Integer" resultMap="BaseResultMap">…</select>

1、输入类型 parameterType

表示mapper接口中的方法入参类型为int(等同于java.lang.Integer),当方法入参包含多种类型时,可以使用Map映射(个人建议,也可以参照下面的参数传递问题解决方案),例如存在这样的SQL
select  id,user_name,age,address,classes,status,is_vip,register_time  from  vip_user  where  name like '%'+?+'%'  and is_vip=?  and  register_time>?

name是String类型,is_vip是int类型,register_time是String类型,此时就需要使用parameterType="java.util.Map"来映射
<select id="getUserVipInfo" parameterType="java.util.Map" resultMap="BaseResultMap">
  select  id,user_name,age,address,classes,status,is_vip,register_time  from  vip_user  where  name like #{name}  and is_vip = #{isVip}  and  register_time > #{registerTime}
</select>

其中的#{ }里面值的name、isVIP、registerTime可以自定义,但必须和Map中的key一致,如果是单个入参,则可通过 @Param("name")注解 来约定。
对应的逻辑中(比如说service中)设值/调用
……
Map<String,Object> map = new HashMap<>();
map.put("name",userVip.getName());
map.put("isVip",userVip.getIsVip);
map.put("registerTime",userVip.getRegisterTIme());
List<UserVip> userVipList = userVipMapper.getUserVipInfo(map);

java.lang.Integer就等用于int,mybatis会自动匹配,下同。

2、输出类型 resultType和resultMap
resultType可接受的值为String、int、bean、Map等,resultMap主要是用于高级映射,例如联合查询或者需要使用自定义的结果集时,bean的字段属性和表的列名称不一致时,例如上述sql中使用到的 resultMap="BaseResultMap" 
<resultMap id="BaseResultMap" type="×.×.×.×.UserVip" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="name" property="name" jdbcType="VARCHAR" />
     <result column="is_vip" property="isVip" jdbcType="INT" />
     <result column="register_time" property="registerTime" jdbcType="VARCHAR" />
     ………
</resultMap>

bean字段和表字段不一致问题

解决方案:

1、查询时设置别名

select id,user_name as username,age,address,classes,status,is_vip as isvip,register_time as registertime from vip_user where ……

2、自定义结果集<resultMap>来映射字段和属性

<resultMap id="BaseResultMap" type="×.×.×.×.UserVip" >
    <id column="id" property="id" jdbcType="BIGINT" />
    <result column="name" property="name" jdbcType="VARCHAR" />
     <result column="is_vip" property="isVip" jdbcType="INT" />
     <result column="register_time" property="registerTime" jdbcType="VARCHAR" />
     ………
</resultMap>

参数传递问题

方式一:#{N},N表示第几个,从0开始 

<select id="getUserVipInfo" parameterType="int" resultMap="BaseResultMap">
  select id,user_name,age,address,classes,status,is_vip,register_time from vip_user where id = #{0} and is_vip = #{1}
</select>

方式二: 固定参数的方式

<select id="getUserVipInfo" parameterType="int" resultMap="BaseResultMap">
  select id,user_name,age,address,classes,status,is_vip,register_time from vip_user where id = #{id} and is_vip = #{isVip}
</select>

如上两种方式,必须是当parameterType都是统一类型时才行。

方式三: map的形式传递,

形如上面 输入/输出类型里提到的,最常用,不赘述

mybatis常用标签

1、resultMap标签

用来映射自定义查询结果集,例如查询结果是VO之类的
<resultMap id="" type="">
    <id column="id" property="id" jdbcType="INT"/>
    <result column="name" property="user_name" jdbcType="VARCHAR"/>
</resultMap>

2、sql标签

用来定义表中出现的所有字段,方便其它地方调用
<sql id="Base_Column_List" >
    id, order_id, phone, item_id, status, register_time, is_vip, create_time, update_time
</sql>

3、include标签

引用公用代码,例如上面的SQL标签
<select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Long" >
    select
    <include refid="Base_Column_List" />
    from user_vip
    where id = #{id,jdbcType=BIGINT}
</select>

4、select、insert、delete、update标签

相对应增删改查操作,不赘述

5、where标签

(经常使用,避免使用where 1=1),如果某一个条件符合,会动态拼接where,如下示例,当定第一个条件order_id为空,phone不为空,则where标签会智能的去除and连接符
<select id="countRecords" parameterType="java.util.Map" resultType="java.lang.Integer">
    SELECT COUNT(*) FROM vip_user
    <where>
        <if test="orderId != null">
            order_id=#{orderId}
        </if>
        <if test="phone != null">
            and phone=#{phone}
        </if>
    </where>
</select>

6、if标签

用来动态判断属性,和el表达式有点类似
<select id="query" parameterType="java.util.Map" resultMap="BaseResultMap">
   select
   <include refid="Base_Column_List"/>
   from user_vip
    <where>
        <if test="orderId != null">
            order_id=#{orderId}
        </if>
     </where>
</select>

7、foreach标签

常用来批量操作时拼接条件,其中item表示循环体的别名,collection可以为list、Map、array
<update id="batchUpdate" parameterType="list">
    update points_mall_virtual_code_info set notice_status=1,status=1 where order_id in
    <foreach item="item" index="index" open="(" separator="," close=")" collection="list">
        #{item}
    </foreach>
</update>

新增返回主键id

<insert id="insert" useGeneratedKeys="true" keyProperty="id" keyColumn="id" ……

keyProperty表示pojo的属性,keyColumn表示表的主键名称,useGeneratedKeys="true"表示要使用自增主键


逆向工程插件-生成pojo和xml

这个比较简单,直接移步至我的博客资源 click here  下载这个资源后直接用idea或eclipse打开并修改数据库连接信息、表名、targetPackage目录即可,运行MyBatisGeneratorMain类的main方法即可生成pojo和xml,拷贝至你的项目即可,解耦于项目,非常方便。

猜你喜欢

转载自blog.csdn.net/fanrenxiang/article/details/80392342