码迷,mamicode.com
首页 > 数据库 > 详细

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

时间:2018-01-28 17:20:11      阅读:260      评论:0      收藏:0      [点我收藏+]

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

1,mybatis中操作注意点

1,update语句与insert语句返回值是语句影响数据库的行数,而不是成功执行就返回1,

Dog dag = new Dog("a");
//插入一个对象执行成功返回1 Integer affectNo = daoImpl.insert(dog); List dogs = new ArrayList<Dog>(); dogs.add(new Dog("b")); dogs.add(new Dog("c"))
//更新两个对象的信息,执行成功后,返回2 Integer affectNo =daoImpl.update(dogs);

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   

原文地址:https://www.cnblogs.com/yanyuechao/p/8371137.html

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