RoleMapper.java

public interface RoleMapper {

    public void add(Role role);

    public void update(Role role);

    public void delete(Role role);

    public List<Role> getRoleList(Role role);
}

RoleMapper.xml

<?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="cn.bdqn.dao.RoleMapper">
<!-- where/if(判断参数) - 将实体类不为空的属性作为where条件, 能智能的处理 and or ,不必担心多余导致语法错误-->
<!-- <select id="getRoleList" resultType="Role" parameterType="Role">
select * from role
<where>
<if test="roleCode != null">
and roleCode like CONCAT ('%',#{roleCode},'%')
</if>
<if test="roleName != null">
and roleName like CONCAT ('%',#{roleName},'%')
</if>
</where>
</select> --> <!-- if/trim代替where(判断参数) - 将实体类不为空的属性作为where条件 -->
<!-- <select id="getRoleList" resultType="Role" parameterType="Role">
select * from role
<trim prefix="where" prefixOverrides="and | or">
<if test="roleCode != null">
and roleCode like CONCAT ('%',#{roleCode},'%')
</if>
<if test="roleName != null">
and roleName like CONCAT ('%',#{roleName},'%')
</if>
</trim>
</select> --> <!-- choose(判断参数) - 按顺序将实体类第一个不为空的属性作为where条件 -->
<select id="getRoleList" resultType="Role" parameterType="Role">
select * from role
<where>
<choose>
<when test="roleCode != null">
and roleCode like CONCAT ('%',#{roleCode},'%')
</when>
<when test="roleName != null">
and roleName like CONCAT ('%',#{roleName},'%')
</when>
<otherwise></otherwise>
</choose>
</where>
</select> <insert id="add" parameterType="Role">
insert into role (roleCode,roleName)
values (#{roleCode},#{roleName})
</insert> <update id="update" parameterType="Role">
update role set roleCode=#{roleCode},roleName=#{roleName}
where id=#{id}
</update> <delete id="delete" parameterType="Role">
delete from role where id=#{id}
</delete> </mapper>

UserMapper.java

public interface UserMapper {

    public int count();

    public void add(User user);

    public void update(User user);

    public void delete(User user);

    public List<User> getUserList();

    //根据roleId获取用户列表
public List<User> getUserListByRoleId(Role role); //获取指定用户的地址列表(user表-address表:1对多关系)
public User getAddressListByUserId(User user); //根据条件,获取用户表数据列表(动态sql)
public List<User> searchUserList(User user); //根据部门条件,获取用户表数据列表-foreach_array
public List<User> getUserByDepId_foreach_array(String[] depIds); //根据部门条件,获取用户表数据列表-foreach_list
public List<User> getUserByDepId_foreach_list(List<String> depIdList); }

UserMapper.xml

<?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"> <!-- namespace的名字需要跟接口的类名一致 -->
<mapper namespace="cn.bdqn.dao.UserMapper">
<!--
1、resultMap属性:type为java实体类;id为此resultMap的标识
2、resultMap的子元素:
id – 一般对应到数据库中该行的ID,设置此项可以提高Mybatis性能.
result – 映射到JavaBean 的某个“简单类型”属性,String,int等.
association – 映射到JavaBean 的某个“复杂类型”属性,其他JavaBean类.
collection –复杂类型集合
--> <!--根据roleId获取用户列表: 当数据库中的字段信息与对象的属性不一致时需要通过resultMap来映射 -->
<!-- <resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleId" column="roleId"/>
<result property="roleName" column="roleName"/>
</resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select> --> <!-- 根据roleId获取用户列表 association start-->
<resultMap type="User" id="seachUserResult">
<result property="id" column="id"/>
<result property="userCode" column="userCode" />
<result property="userName" column="userName" />
<result property="roleId" column="roleId" />
<!-- <association property="role" javaType="Role" >
<result property="id" column="id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</association> -->
<association property="role" javaType="Role" resultMap="roleMap"/>
</resultMap> <resultMap type="Role" id="roleMap">
<result property="id" column="id"/>
<result property="roleCode" column="roleCode"/>
<result property="roleName" column="roleName"/>
</resultMap> <select id="getUserListByRoleId" parameterType="Role" resultMap="seachUserResult">
select u.*,r.roleCode as roleCode,r.roleName as roleName from user u,role r where u.roleId = r.id and u.roleId = #{id}
</select> <!-- association end--> <!-- 获取指定用户的地址列表(user表-address表:1对多关系) collection start-->
<resultMap type="User" id="userMap">
<id property="id" column="userId"/>
<collection property="addressList" ofType="Address">
<id property="id" column="a_id"/>
<result property="postCode" column="postCode"/>
<result property="addressContent" column="addressContent"/>
</collection>
</resultMap> <select id="getAddressListByUserId" parameterType="User" resultMap="userMap">
select *,a.id as a_id from user u,address a where u.id=a.userId and u.id=#{id}
</select>
<!-- collection end --> <resultMap type="User" id="seachUser">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
<result property="roleId" column="roleId"/>
<result property="roleName" column="roleName"/>
</resultMap> <!-- <select id="searchUserList" parameterType="User" resultMap="seachUser">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id
and u.roleId = #{roleId}
and u.userCode like CONCAT ('%',#{userCode},'%') //防止sql注入
and u.userName like CONCAT ('%',#{userName},'%')
</select> --> <!--
1、有些时候,sql语句where条件中,需要一些安全判断,例如按性别检索,如果传入的参数是空的,此时查询出的结果很可能是空的,也许我们需要参数为空时,是查出全部的信息。这是我们可以使用动态sql,增加一个判断,当参数不符合要求的时候,我们可以不去判断此查询条件。
2、mybatis 的动态sql语句是基于OGNL表达式的。可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类:
if 语句 (简单的条件判断)
choose (when,otherwize) ,相当于java 语言中的 switch ,与 jstl 中的choose 很类似.
trim (对包含的内容加上 prefix,或者 suffix 等,前缀,后缀)
where (主要是用来简化sql语句中where条件判断的,能智能的处理 and or ,不必担心多余导致语法错误)
set (主要用于更新时)
foreach (在实现 mybatis in 语句查询时特别有用)
--> <!-- if(判断参数) - 将实体类不为空的属性作为where条件 -->
<select id="searchUserList" parameterType="User" resultMap="seachUser">
select u.*,r.roleName as roleName from user u,role r where u.roleId = r.id
<if test="roleId!=null">
and u.roleId = #{roleId}
</if>
<if test="userCode != null">
and u.userCode like CONCAT ('%',#{userCode},'%')
</if>
<if test="userName != null">
and u.userName like CONCAT ('%',#{userName},'%')
</if>
</select> <select id="count" resultType="int">
select count(1) from user
</select> <insert id="add" parameterType="User">
insert into user (userCode,userName,userPassword)
values (#{userCode},#{userName},#{userPassword})
</insert> <!-- if/set(判断参数) - 将实体类不为空的属性更新 -->
<!-- <update id="update" parameterType="User">
update user
<set>
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="roleId != null">roleId=#{roleId}</if>
</set>
where id=#{id}
</update> --> <!-- if/trim代替set(判断参数) - 将实体类不为空的属性更新 -->
<update id="update" parameterType="User">
update user
<trim prefix="set" suffixOverrides=",">
<if test="userCode != null and userCode != ''">userCode=#{userCode},</if>
<if test="userName != null">userName=#{userName},</if>
<if test="userPassword != null">userPassword=#{userPassword},</if>
<if test="roleId != null">roleId=#{roleId}</if>
</trim>
where id=#{id}
</update> <!--注意: 你可以传递一个List实例或者数组作为参数对象传给MyBatis。
当你这么做的时候,MyBatis会自动将它包装在一个Map中,用名称在作为键。
List实例将会以“list”作为键,而数组实例将会以“array”作为键。
配置文件中的parameterType是可以不配置的-->
<resultMap type="User" id="userMapByDep">
<result property="id" column="id"/>
<result property="userCode" column="userCode"/>
<result property="userName" column="userName"/>
</resultMap>
<!-- foreach(循环array参数) - 作为where中in的条件 -->
<select id="getUserByDepId_foreach_array" resultMap="userMapByDep">
select * from user where depId in
<foreach collection="array" item="depIds" open="(" separator="," close=")">
#{depIds}
</foreach>
</select> <!-- foreach(循环List<String>参数) - 作为where中in的条件 -->
<select id="getUserByDepId_foreach_list" resultMap="userMapByDep">
select * from user where depId in
<foreach collection="list" item="depIdList" open="(" separator="," close=")">
#{depIdList}
</foreach>
</select> <delete id="delete" parameterType="User">
delete from user where id=#{id}
</delete> <select id="getUserList" resultType="User">
select * from user
</select>
</mapper>

mybatis 使用动态SQL的更多相关文章

  1. MyBatis的动态SQL详解

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑,本文详解mybatis的动态sql,需要的朋友可以参考下 MyBatis 的一个强大的特性之一通常是它 ...

  2. Mybatis解析动态sql原理分析

    前言 废话不多说,直接进入文章. 我们在使用mybatis的时候,会在xml中编写sql语句. 比如这段动态sql代码: <update id="update" parame ...

  3. MyBatis框架——动态SQL、缓存机制、逆向工程

    MyBatis框架--动态SQL.缓存机制.逆向工程 一.Dynamic SQL 为什么需要动态SQL?有时候需要根据实际传入的参数来动态的拼接SQL语句.最常用的就是:where和if标签 1.参考 ...

  4. 使用Mybatis实现动态SQL(一)

    使用Mybatis实现动态SQL 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:        *本章节适合有Mybatis基础者观看* 前置讲解 我现在写一个查询全部的 ...

  5. MyBatis探究-----动态SQL详解

    1.if标签 接口中方法:public List<Employee> getEmpsByEmpProperties(Employee employee); XML中:where 1=1必不 ...

  6. mybatis中的.xml文件总结——mybatis的动态sql

    resultMap resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功. 如果sql查询字段名和pojo的属性名不一致,可以通过re ...

  7. mybatis.5.动态SQL

    1.动态SQL,解决关联sql字符串的问题,mybatis的动态sql基于OGNL表达式 if语句,在DeptMapper.xml增加如下语句; <select id="selectB ...

  8. MyBatis的动态SQL详解-各种标签使用

    MyBatis的动态SQL是基于OGNL表达式的,它可以帮助我们方便的在SQL语句中实现某些逻辑. MyBatis中用于实现动态SQL的元素主要有: if choose(when,otherwise) ...

  9. 利用MyBatis的动态SQL特性抽象统一SQL查询接口

    1. SQL查询的统一抽象 MyBatis制动动态SQL的构造,利用动态SQL和自定义的参数Bean抽象,可以将绝大部分SQL查询抽象为一个统一接口,查询参数使用一个自定义bean继承Map,使用映射 ...

随机推荐

  1. SQL Server优化50法

    查询速度慢的原因很多,常见如下几种:    1.没有索引或者没有用到索引(这是查询慢最常见的问题,是程序设计的缺陷)    2.I/O吞吐量小,形成了瓶颈效应.    3.没有创建计算列导致查询不优化 ...

  2. Convert和Parse对null值处理的区别

    类型的转换在日常的变成中是经常被用到的,我们最常用的类型转换的方法就是Convert和Parse, 下面来说一下这两者null值处理的区别. int i1 = Convert.ToInt32(null ...

  3. ASP.NET MVC 5 入门教程 (1) 新建项目

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html 下一节:ASP.NET ...

  4. 安装包制作工具 SetupFactory使用2 API清单

    2014-11-19 SetupFactory中可以通过其API控制很复杂的业务需求. 下图中展示了其内置的API种类与具体分类函数.   序号 API名称 API说明 1 Application.E ...

  5. Java并发编程-CopyOnWriteArrayList

    CopyOnWriteArrayList原理 首先每次写操作,都将数组copy一份,并赋值给arrays 读操作读不加锁 写操作加锁 ReentrantLock 因为每次写都要copy数组,这是一项繁 ...

  6. 一份完整的nginx配置

    #user nobody;worker_processes 24;worker_rlimit_nofile 262144;worker_cpu_affinity 0000000000000000000 ...

  7. session和cookie的前后的操作

    1. // sign outexports.signout = function (req, res, next) { req.session.destroy(); res.clearCookie(c ...

  8. 一头扎进EasyUI

    惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第1讲 .加载库文件和样式 <link rel="stylesheet" ...

  9. WRONGTYPE Operation against a key holding the wrong kind of value

    今天改动代码,一运行就跑错了,错误原因: 因为redis中已经存在了相同的key, 而且key对应的值类型并不是Set,而是SortSet(改动前):再调用smembers时,抛出此错误. 解决方法: ...

  10. Could not load resource factory class [Root exception is java.lang.ClassNotFoundException: org.apache.tomcat.dbcp.dbcp.BasicDataSourceFactory]

    WARNING: Failed to register in JMX: javax.naming.NamingException: Could not load resource factory cl ...