本章學習Mysql基本映射--INSERT相關知識點。insert的用法比select要簡單不少。mysql
本系列文章是基於Mybatis 3.4.6 版本,數據庫使用的是Mysql 5.7。git
insert標籤經常使用屬性:github
此是插入數據最簡單的用法,在上一章的基礎上,須要編寫mapper配置文件和mapper接口。sql
代碼以下:數據庫
<!-- insert標籤最小配置 --> <insert id="insert"> INSERT INTO sys_user (user_account, user_password, created_date) VALUES (#{userAccount}, #{userPassword}, #{createdDate}) </insert>
接口代碼以下:緩存
// 返回值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
代碼以下: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>
接口代碼以下:學習
// 返回值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字段,再執行數據庫插入操做,此狀況的實現以下:
代碼以下:
<!-- --> <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>
接口代碼以下:
// 返回值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,共同窗習、共同進步,謝謝!