之前在项目中用到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代码封装的更多相关文章

  1. 通用mybatis单表操作接口

    <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "- ...

  2. PHP单表操作mysqli数据库类的封装

    class DB{ private $options=array( 'database_type' => 'mysql', 'database_name' => 'test', 'serv ...

  3. python 全栈开发,Day71(模型层-单表操作)

    昨日内容回顾 1. {% include '' %} 2. extend base.html: <html> ..... ..... ..... {% block content%} {% ...

  4. 09-hibernate单表操作(1)

    1,单一主键 2,基本类型 3,对象类型 4,组件属性 5,单表操作 单一主键 常用生成策略: assigned 有程序员生成(手工) native 由数据库底层,如果是mysql是increment ...

  5. Django---ORM简介丶单表操作丶增删改查

    一丶ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  6. ORM之单表操作

    ORM简介 MVC或者MVC框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人员的 ...

  7. Django系列(三):单表操作

    1.ORM简介 MTV或者MTV框架中包括一个重要的部分,就是ORM,它实现了数据模型与数据库的解耦,即数据模型的设计不需要依赖于特定的数据库,通过简单的配置就可以轻松更换数据库,这极大的减轻了开发人 ...

  8. Mybatis多表操作

    一:引言 在学习完前面的mybatis基本语法后,大家都有个认知,这个Mybatis太强大了,比之前使用JDBC写方便多了,但是你们当初在使用原生JDBC写SQL查询的时候有没有遇到过多表查询呢?肯定 ...

  9. 学习MySQL之单表操作(二)

    ##单表操作 ##创建表 CREATE TABLE t_employee( empno ), ename ), job ), MGR ), Hiredate DATE DEFAULT '0000-00 ...

随机推荐

  1. 编译安装bluez5.44

    1.下载 2. configure 提示需要glib 3.yum install glib 4.还是提示glib 5.yum install glib-devel 下载编译glib make inst ...

  2. Netty 源码 NioEventLoop(一)初始化

    Netty 源码 NioEventLoop(一)初始化 Netty 系列目录(https://www.cnblogs.com/binarylei/p/10117436.html) Netty 基于事件 ...

  3. boost::asio 学习草稿

    http://www.gamedev.net/blog/950/entry-2249317-a-guide-to-getting-started-with-boostasio/ 可以多个线程拥有io_ ...

  4. Python 函数装饰器简明教程

    定义类的静态方法时,就使用了装饰器.其实面向对象中的静态方法都是使用了装饰器. @staticmethod def jump(): print(" 3 meters high") ...

  5. OC调用Swift

    Step by step swift integration for Xcode Objc-based project: Create new *.swift file (in Xcode) or a ...

  6. 2018.09.16 bzoj1086: [SCOI2005]王室联邦(贪心)

    传送门 就是给树分块. 对于一个节点. 如果它的几棵子树加起来超过了下限,就把它们分成一块. 这样每次可能会剩下几个节点. 把它们都加入栈中最顶上那一块就行了. 代码: #include<bit ...

  7. hdu-1063(大数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1063 思路:1.大数乘法模板 2.考虑小数点的位置 3.乘法前后判断前后道0 参考文章:https:/ ...

  8. 第七章 介词(La préposition )

    法语中的介词是将两个同类或非同类的词连接在一起,把一个句中成分同另一个句子成分联系起来,并表明两者之间关系的词类.介词是一种无词性变化的词类. ★介词的形式 ()简单形式的介词,如: ()复合形式的介 ...

  9. C#基础:在using中创建对象

    在using中创建的对象的类必须是实现了IDispose接口的类,示例代码如下: static void Main(string[] args) { Method(); Console.WriteLi ...

  10. 二)spring 集成 ehcache jgroups 集群

    依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-co ...