Mybatis基本映射--INSERT

概述

本章學習Mysql基本映射--INSERT相關知識點。insert的用法比select要簡單不少。mysql

本系列文章是基於Mybatis 3.4.6 版本,數據庫使用的是Mysql 5.7。git

INSERT標籤

insert標籤經常使用屬性:github

  • id:能夠理解爲Mybatis執行語句的名稱,與Mapper接口一一對應,此屬性爲必須屬性,且在命名空間(mapper標籤的namespace)中惟一。
  • parameterType:該屬性的含義就是其字面意思,即傳入語句的參數類型,是類的全限定類名,非必須。
  • flushCache:表示執行該語句將清空一級、二級緩存,默認爲true。
  • timeout:超時時間,即程序提交sql語句到數據庫等待的時間,超過此設置時間將拋出超時異常,默認設置是不超時,也就是說程序會一直等待直到有結果返回,單位爲妙。
  • useGeneratedKeys:該屬性是獲取數據庫內部生產的主鍵,默認爲false。
  • keyProperty:賦值主鍵的屬性名,即把數據庫內部生產的主鍵賦值給該屬性。
  • keyColumn:賦值主鍵的字段名,即把數據庫內部生產的主鍵賦值給該字段。

不返回主鍵

此是插入數據最簡單的用法,在上一章的基礎上,須要編寫mapper配置文件和mapper接口。sql

  • mapper配置文件

代碼以下:數據庫

<!-- insert標籤最小配置 -->
<insert id="insert">
    INSERT  INTO sys_user (user_account, user_password, created_date)
    VALUES (#{userAccount}, #{userPassword}, #{createdDate})
</insert>
  • mapper接口

接口代碼以下:緩存

// 返回值int爲插入數據的行數
public int insert(SysUser sysUser) throws Exception ;
  • 測試用例

測試用戶代碼以下:mybatis

@Test
public void testInsert(){
    // 得到sqlSession
    SqlSession sqlSession = getSqlSession();
    try{
        // 獲取Mapper接口
        SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        SysUser user = new SysUser();
        user.setUserAccount("admin");
        user.setUserPassword("123");
        user.setCreatedDate(new Date());
        // 插入的條數
        int count = sysUserMapper.insert(user);
        // 提交事務,數據存入數據庫
        sqlSession.commit();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        sqlSession.close();
    }
}

返回自增主鍵

根據上面對INSERT標籤屬性的學習知道,若是要返回數據庫自增的主鍵,可經過設置useGeneratedKeys和keyProperty以及keyCulomn(返回多個時需指定字段順序)。oracle

  • mapper配置文件

代碼以下:app

<!-- 
    配置useGeneratedKeys=true,keyProperty="屬性名"
    parameterType屬性不是必須的,再次只是掩飾下它的用法,使用時按這樣用就好了
-->
<insert id="insertAndResultAutoId" parameterType="com.github.dalianghe.model.SysUser"
        useGeneratedKeys="true" keyProperty="id">
    INSERT  INTO sys_user (user_account, user_password, created_date)
    VALUES (#{userAccount}, #{userPassword}, #{createdDate})
</insert>
  • mapper接口

接口代碼以下:學習

// 返回值int仍然爲插入影響的行數,數據庫內部自增id值被封裝到參數對象中
public int insertAndResultAutoId(SysUser sysUser) throws Exception;
  • 測試用例

測試用戶代碼以下:

@Test
public void testInsertAndResultAutoId(){
    // 得到sqlSession
    SqlSession sqlSession = getSqlSession();
    try{
        // 獲取Mapper接口
        SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        SysUser user = new SysUser();
        user.setUserAccount("admin");
        user.setUserPassword("123");
        user.setCreatedDate(new Date());
        // 插入前,id爲空
        System.out.println(user.getId());
        // 插入的條數
        int count = sysUserMapper.insertAndResultAutoId(user);
        // 插入後,id的值爲新插入數據的數據庫自增id值
        System.out.println(user.getId());
        // 提交事務,數據存入數據庫
        sqlSession.commit();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        sqlSession.close();
    }
}

返回非自增主鍵

對於一些數據庫不支持自增ID,好比Oracle數據庫使用的序列,而後賦值給id字段,再執行數據庫插入操做,此狀況的實現以下:

  • mapper配置文件

代碼以下:

<!-- 
    
-->
<insert id="insertAndSelectKey">
    <!-- 
        使用selectKey子標籤返回數據生成的主鍵
        keyColumn、keyProperty與自增主鍵中含義同樣,resultType是返回的數據類型
        order的值與數據庫有關,mysql使用AFTER,oracle使用BEFORE
    -->
    <selectKey keyColumn="id" resultType="long" keyProperty="id" order="AFTER">
        SELECT LAST_INSERT_ID()
    </selectKey>
    INSERT  INTO sys_user (user_account, user_password, created_date)
    VALUES (#{userAccount}, #{userPassword}, #{createdDate})
</insert>
  • mapper接口

接口代碼以下:

// 返回值int仍然爲插入影響的行數,數據庫內部自增id值被封裝到參數對象中
public int insertAndSelectKey(SysUser sysUser) throws Exception;
  • 測試用例

測試用戶代碼以下:

@Test
public void testInsertAndSelectKey(){
    // 得到sqlSession
    SqlSession sqlSession = getSqlSession();
    try{
        // 獲取Mapper接口
        SysUserMapper sysUserMapper = sqlSession.getMapper(SysUserMapper.class);
        SysUser user = new SysUser();
        user.setUserAccount("admin");
        user.setUserPassword("123");
        user.setCreatedDate(new Date());
        // 插入前,id爲空
        System.out.println(user.getId());
        // 插入影響的條數
        int count = sysUserMapper.insertAndSelectKey(user);
        // 插入後,id的值爲新插入數據的數據庫自增id值
        System.out.println(user.getId());
        // 提交事務,數據存入數據庫
        sqlSession.commit();
    }catch (Exception e){
        e.printStackTrace();
    }finally {
        sqlSession.close();
    }
}

批量插入

此部份內容將在動態SQL中進行學習。

總結

本節學習了mybatis的基本的insert的用法,但願對你們有幫助。

最後建立了qq羣方便你們交流,可掃描加入,同時也可加我qq:276420284,共同窗習、共同進步,謝謝!

相關文章
相關標籤/搜索