mybatis 增删改查、批量插入和删除以及自动生成uuid主键和分页

Mapper接口:

public int update(Admin admin);

public Admin selectByUserName(String account);

public List<Admin> list();

public int add(Admin admin);

public int delete(String id);

public Set<String> getRoleIds(String adminId);

public void deleteRole(Map<String,Object> map);

public List<Role> getRoles(@Param("set") Set<String> roleIds);

public void addRole(List<AdminRole> addRoleList);

public void deleteRoles(@Param("adminId")String adminId, @Param("deleteList")List<String> deleteRoles);

public List<Function> list(PageVo pageVo);

 

Mapper.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="com.young.shop.dao.mapper.AdminMapper">
<!--设置domain类和数据库中表的字段一一对应,注意数据库字段和domain类中的字段名称不致,此处一定要! -->
<resultMap id="BaseResultMap" type="com.young.shop.vo.Admin">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="email" property="email" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="AdminRoleResultMap" type="com.young.shop.vo.AdminRole">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="role_id" property="roleId" jdbcType="VARCHAR" />
<result column="admin_id" property="adminId" jdbcType="VARCHAR" />
</resultMap>
<resultMap id="RoleResultMap" type="com.young.shop.vo.Role">
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="name" property="name" jdbcType="VARCHAR" />
</resultMap>
<!-- 查询单条记录 -->
<select id="selectByUserName" parameterType="String" resultMap="BaseResultMap">
SELECT * FROM t_admin WHERE username = #{username}
</select>
<!-- 查询所有记录 -->
<select id="list" resultMap="BaseResultMap">
SELECT * FROM t_admin
</select>
<!--修改记录 -->
<update id="update" parameterType="com.young.shop.vo.Admin">
update t_admin
<set>
<if test=" username != null ">
username = #{username,jdbcType=VARCHAR},
</if>
<if test=" password != null ">
password = #{password,jdbcType=VARCHAR},
</if>
<if test=" email != null ">
email = #{email,jdbcType=VARCHAR}
</if>
</set>
where id = #{id,jdbcType=VARCHAR}
</update>
<!--添加记录 -->
<insert id="add" parameterType="com.young.shop.vo.Admin">
<selectKey keyProperty="id" resultType="java.lang.String"
order="BEFORE">
select replace(uuid(),'-','') from dual
</selectKey>
insert into t_admin (id,username,password,email) values
(#{id},#{username},#{password},#{email})
</insert>
<delete id="delete">
delete from t_admin where id=#{id}
</delete>
<select id="getRoleIds" resultMap="AdminRoleResultMap">
select * from t_admin_role
where admin_id = #{adminId}
</select>
<select id="getRoles" resultMap="RoleResultMap" parameterType="java.util.Set">
select * from t_role where id in
<foreach collection="set" index="index" item="item" open="("
separator="," close=")">
#{item.roleId}
</foreach>
</select>
<!-- 批量删除 使用map-->
<delete id="deleteRole" parameterType="map">
delete from t_admin_role where admin_id = #{adminId} and role_id in
<foreach collection="deleteRoles" index="index" item="item"
open="(" separator="," close=")">
#{item.roleId}
</foreach>
</delete>
<!-- 批量插入 -->
<insert id="addRole">
insert into t_admin_role (id,role_id,admin_id) values
<foreach collection="list" index="index" item="item"
separator=",">
(#{item.id},#{item.roleId},#{item.adminId})
</foreach>
</insert>
<!-- 批量删除 使用list-->
<delete id="deleteRoles">
delete from t_admin_role where admin_id = #{adminId} and role_id in
<foreach collection="deleteList" index="index" item="item"
open="(" separator="," close=")">
#{item}
</foreach>
</delete>

<!-- 查询所有记录 -->
 <select id="list" parameterType="com.cn21.shop.vo.PageVo"
  resultMap="BaseResultMap">
  SELECT * FROM t_function
  <if test=" page != null and size != null">
   limit #{page},#{size}
  </if>
 </select>
</mapper>

 加黑部分为需注意的地方以及分页查询和自动生成uuid主键

属性描述
item循环体中的具体对象。支持属性的点路径访问,如item.age,item.info.details。 具体说明:在list和数组中是其中的对象,在map中是value。 该参数为必选。
collection

要做foreach的对象,作为入参时,List<?>对象默认用list代替作为键,数组对象有array代替作为键,Map对象没有默认的键。 当然在作为入参时可以使用@Param("keyName")来设置键,设置keyName后,list,array将会失效。 除了入参这种情况外,还有一种作为参数对象的某个字段的时候。举个例子: 如果User有属性List ids。入参是User对象,那么这个collection = "ids" 如果User有属性Ids ids;其中Ids是个对象,Ids有个属性List id;入参是User对象,那么collection = "ids.id" 上面只是举例,具体collection等于什么,就看你想对那个元素做循环。 该参数为必选。

separator元素之间的分隔符,例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。
openforeach代码的开始符号,一般是(和close=")"合用。常用在in(),values()时。该参数可选。
closeforeach代码的关闭符号,一般是)和open="("合用。常用在in(),values()时。该参数可选。
index在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。
 具体可以学习一下MyBatis的动态SQL详解

 

 

 

 

转载于:https://www.cnblogs.com/youngcrazy/p/5915331.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值