码迷,mamicode.com
首页 > 编程语言 > 详细

mybatis单表操作实现完全java代码封装

时间:2016-08-29 00:06:54      阅读:567      评论:0      收藏:0      [点我收藏+]

标签:

之前在项目中用到mybtis操作数据库时都是手动写sql,对于我这种sql水平不是很好地人来说痛苦死了;动态查询的sql我表示到现在还不会写呀!

还好,利用数据库表反向生成的工具可以帮我解决大部分的sql;(mybatis generator 你懂的)

首先利用反向生成可以帮我们自动生成实体类,dao接口,dao映射文件;

在dao映射文件如下所示:

<?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="bz.sunlight.dao.UserMapper" >

  <resultMap id="BaseResultMap" type="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    <id column="id" property="id" jdbcType="VARCHAR" />

    <result column="name" property="name" jdbcType="VARCHAR" />

    <result column="password" property="password" jdbcType="VARCHAR" />

  </resultMap>

  

  <sql id="Example_Where_Clause" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    <where >

      <foreach collection="oredCriteria" item="criteria" separator="or" >

        <if test="criteria.valid" >

          <trim prefix="(" suffix=")" prefixOverrides="and" >

            <foreach collection="criteria.criteria" item="criterion" >

              <choose >

                <when test="criterion.noValue" >

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue" >

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue" >

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue" >

                  and ${criterion.condition}

                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Update_By_Example_Where_Clause" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    <where >

      <foreach collection="example.oredCriteria" item="criteria" separator="or" >

        <if test="criteria.valid" >

          <trim prefix="(" suffix=")" prefixOverrides="and" >

            <foreach collection="criteria.criteria" item="criterion" >

              <choose >

                <when test="criterion.noValue" >

                  and ${criterion.condition}

                </when>

                <when test="criterion.singleValue" >

                  and ${criterion.condition} #{criterion.value}

                </when>

                <when test="criterion.betweenValue" >

                  and ${criterion.condition} #{criterion.value} and #{criterion.secondValue}

                </when>

                <when test="criterion.listValue" >

                  and ${criterion.condition}

                  <foreach collection="criterion.value" item="listItem" open="(" close=")" separator="," >

                    #{listItem}

                  </foreach>

                </when>

              </choose>

            </foreach>

          </trim>

        </if>

      </foreach>

    </where>

  </sql>

  <sql id="Base_Column_List" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    id, name, password

  </sql>

  <select id="selectByExample" resultMap="BaseResultMap" parameterType="bz.sunlight.entity.UserExample" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    select

    <if test="distinct" >

      distinct

    </if>

    <include refid="Base_Column_List" />

    from user

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

    <if test="orderByClause != null" >

      order by ${orderByClause}

    </if>

  </select>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.String" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    select 

    <include refid="Base_Column_List" />

    from user

    where id = #{id,jdbcType=VARCHAR}

  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.String" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    delete from user

    where id = #{id,jdbcType=VARCHAR}

  </delete>

  <delete id="deleteByExample" parameterType="bz.sunlight.entity.UserExample" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    delete from user

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

  </delete>

  <insert id="insert" parameterType="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    insert into user (id, name, password

      )

    values (#{id,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR}

      )

  </insert>

  <insert id="insertSelective" parameterType="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    insert into user

    <trim prefix="(" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        id,

      </if>

      <if test="name != null" >

        name,

      </if>

      <if test="password != null" >

        password,

      </if>

    </trim>

    <trim prefix="values (" suffix=")" suffixOverrides="," >

      <if test="id != null" >

        #{id,jdbcType=VARCHAR},

      </if>

      <if test="name != null" >

        #{name,jdbcType=VARCHAR},

      </if>

      <if test="password != null" >

        #{password,jdbcType=VARCHAR},

      </if>

    </trim>

  </insert>

  <select id="countByExample" parameterType="bz.sunlight.entity.UserExample" resultType="java.lang.Integer" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    select count(*) from user

    <if test="_parameter != null" >

      <include refid="Example_Where_Clause" />

    </if>

  </select>

  <update id="updateByExampleSelective" parameterType="map" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    update user

    <set >

      <if test="record.id != null" >

        id = #{record.id,jdbcType=VARCHAR},

      </if>

      <if test="record.name != null" >

        name = #{record.name,jdbcType=VARCHAR},

      </if>

      <if test="record.password != null" >

        password = #{record.password,jdbcType=VARCHAR},

      </if>

    </set>

    <if test="_parameter != null" >

      <include refid="Update_By_Example_Where_Clause" />

    </if>

  </update>

  <update id="updateByExample" parameterType="map" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    update user

    set id = #{record.id,jdbcType=VARCHAR},

      name = #{record.name,jdbcType=VARCHAR},

      password = #{record.password,jdbcType=VARCHAR}

    <if test="_parameter != null" >

      <include refid="Update_By_Example_Where_Clause" />

    </if>

  </update>

  <update id="updateByPrimaryKeySelective" parameterType="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    update user

    <set >

      <if test="name != null" >

        name = #{name,jdbcType=VARCHAR},

      </if>

      <if test="password != null" >

        password = #{password,jdbcType=VARCHAR},

      </if>

    </set>

    where id = #{id,jdbcType=VARCHAR}

  </update>

  <update id="updateByPrimaryKey" parameterType="bz.sunlight.entity.User" >

    <!--

      WARNING - @mbggenerated

      This element is automatically generated by MyBatis Generator, do not modify.

      This element was generated on Sat Aug 27 23:42:15 CST 2016.

    -->

    update user

    set name = #{name,jdbcType=VARCHAR},

      password = #{password,jdbcType=VARCHAR}

    where id = #{id,jdbcType=VARCHAR}

  </update>

 </mapper>

用这些生成的sql语句基本上可以解决我们单表查询中的所有情景;而不必我们再去手写sql(太操蛋了),是不是有点回到另外一个大框架中了呀;

例如:

我要新增客户:

那么在service层的代码就可以这么写:

@Override

public void saveUser(User record) {

userMapper.insert(record);

 }

直接用反向生成的insert方法来进行插入;

你可能会觉得没什么太多意思:这种sql语句太容易写了;

那么查询呢?根据条件查询,条件比较多得情况下呢?更新,需要动态更新呢?

查询:

@Override

public List<User> queryUserByName(String name) {

UserExample example=new UserExample();

example.createCriteria().andNameEqualTo(name);   //我只需要在这里添加查询条件即可,添加的查询条件有很多可供选择的;Criteria这个对象是不是很熟悉呀!

List<User> users = userMapper.selectByExample(example);

return users;

}

这样就将根据姓名查询的服务写好了,还可以有like,between等多种方法可以供选择;

更新:

有时候表单提交上来的数据不是很完整,更新需要写动态sql以防止数据库的数据被更新成null;

现在我们可以这样操作:

@Override

public void updateById(User user,String id) {

UserExample example=new UserExample();

example.createCriteria().andIdEqualTo(id);

userMapper.updateByExampleSelective(user, example);

 }

动态sql已经反向生成好了,我们只需要将需要更新的条件封装到example类中即可;

当然,多表关联暂时还无法这么使用,关于多表关联可以关注我的另外一篇博文;

这样,是不是mybatis也实现了全封装的操作;

 

mybatis单表操作实现完全java代码封装

标签:

原文地址:http://www.cnblogs.com/beppezhang/p/beppe1.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!