当前位置:Gxlcms > 数据库问题 > mybatis返回自增主键的id,动态拼接查询语句,mysql创建新用户并授权相关表

mybatis返回自增主键的id,动态拼接查询语句,mysql创建新用户并授权相关表

时间:2021-07-01 10:21:17 帮助过:2人阅读

 

3, 实体类(entity)对象的属性 < 数据库的字段时 => 不会报错

  实体类(entity)对象的属性 > 数据库的字段时 => 会报错

4, 数据表对应的entity 的类,必须有无参的构造方法,不然sql语句会报错

2,插入数据时,返回自增主键id

<!-- 插入账户表情况,useGeneratedKeys="true" keyProperty="id"--> 
<insert id="insert" parameterType="map" useGeneratedKeys="true" keyProperty="id"> INSERT INTO account (`account_id`, `total_point`) VALUES (#{accountId}, #{totalPoint}) </insert>
//主键是自增策略的情况下,设置每插入一条数据,返回数据的ID
<insert id="insert" parameterType="com.2412cyy.pojo.TbContentCategory" >
   <selectKey keyProperty="id" resultType="long" order="AFTER">
       select LAST_INSERT_ID()//取到最后生成的主键
   </selectKey>
    insert into tb_content_category (id, parent_id, name, 
      status, sort_order, is_parent, 
      created, updated)
    values (#{id,jdbcType=BIGINT}, #{parentId,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}, 
      #{status,jdbcType=INTEGER}, #{sortOrder,jdbcType=INTEGER}, #{isParent,jdbcType=BIT}, 
      #{created,jdbcType=TIMESTAMP}, #{updated,jdbcType=TIMESTAMP})
  </insert>

3,动态拼接sql语句

//List参数在mybatis中的拼接方法(idList是传入的list类型参数)
<if test="idList != null"> 
   AND opr.order_id IN 
    <foreach item="item" index="index" collection="idList" open="(" separator="," close=")"> 
      #{item} 
     </foreach> 
</if>

4,exists函数的用法

<if test="isReceive == 1">
	AND NOT EXISTS (select 1 FROM order_xxx WHERE order_xx=opr.id AND is_receive=1)
</if>

5,mysql创建新用户并授权相关表

Mysql 本地的安装路径:

/usr/local/mysql/bin/mysql -u root -p 

创建新用户

create user admin identified by ‘admin‘;

授权:

grant all on db32.* to admin;

mybatis返回自增主键的id,动态拼接查询语句,mysql创建新用户并授权相关表

标签:char   bin   update   each   12c   add   entity   time   exist   

人气教程排行