mybatis返回自增主鍵的id,動態拼接查詢語句,mysql建立新使用者並授權相關表

來源:互聯網
上載者:User

標籤:char   bin   update   each   12c   add   entity   time   exist   

1,mybatis中操作注意點

1,update語句與insert語句傳回值是語句影響資料庫的行數,而不是成功執行就返回1,

Dog dag = new Dog("a");
//插入一個對象執行成功返回1Integer affectNo = daoImpl.insert(dog);List dogs = new ArrayList<Dog>();dogs.add(new Dog("b"));dogs.add(new Dog("c"))
//更新兩個對象的資訊,執行成功後,返回2Integer 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建立新使用者並授權相關表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.