映射語句之INSERT語句


1.一個 INSERT SQL 語句可以在<insert>元素在映射器 XML 配置文件中配置

例子:
  1. <insert id="insertStudentWithId" parameterType="Student">
  2.         INSERT INTO Student(id,name,sex,birthday,height,weight,score,address,email,hobby) values
  3.         (#{id},#{name},#{sex},#{birthday},#{height},#{weight},#{score},#{address},#{email},#{hobby})
  4.  </insert>
如果使用名空間(namspace)和語句id來調用的話,那么使用的是SqlSession對象的insert()方法,其返回值是執行INSER語句后所影響的行數。
注: 在執行insert語句之后,SqlSession對象必須執行commit進行顯式提交,否則數據庫中的數據不會刷新
 
2.自動生成主鍵
在上述的 INSERT 語句中,我們為可以自動生成(auto-generated)主鍵的列 id插入值。 我們可以使用
useGeneratedKeys keyProperty 屬性讓數據庫生成 auto_increment 列的值,並將生成的值設置到其中一個
輸入對象屬性內:

例子:
xml code
  1. <insertid="insertStudentWithoutId"parameterType="Student"useGeneratedKeys="true"keyProperty="id"  >
  2.         INSERT INTO Student (name,sex,birthday,height,weight,score,address,email,hobby) values
  3.         (#{name},#{sex},#{birthday},#{height},#{weight},#{score},#{address},#{email},#{hobby})
  4. </insert>
java code
  1. @Test
  2.     publicvoid testInsertWithoutId()
  3.     {
  4.         SqlSession sqlSession =MyBatisSqlSessionFactory.openSession();
  5.         try{
  6.             StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
  7.             Student student =newStudent();
  8.             student.setName("小2");
  9.             student.setAddress("江蘇南通");
  10.             student.setBirthday(newDate());
  11.             student.setEmail("xiao2@live.com");
  12.             student.setHeight(177);
  13.             student.setHobby("打籃球");
  14.             student.setScore(99);
  15.             student.setSex("男");
  16.             student.setWeight(120);
  17.             studentMapper.insertStudentWithoutId(student);
  18.             sqlSession.commit();
  19.             System.out.println(student);
  20.         }finally{
  21.             sqlSession.close();
  22.         }
  23.     }
注: 有些數據庫如 Oracle 並不支持 AUTO_INCREMENT 列, 其使用序列( SEQUENCE)來生成主鍵值
方法一:
假設我們有一個名為 STUD_ID_SEQ 的序列來生成 SUTD_ID 主鍵值。使用如下代碼來生成主鍵:
Xml Code
  1. <insertid="insertStudent"parameterType="Student">
  2.     <selectKeykeyProperty="id"resultType="int"order="BEFORE">
  3.         SELECT ELEARNING.STUD_ID_SEQ.NEXTVAL FROM DUAL
  4.     </selectKey>
  5.     INSERT INTO STUDENTS(id,name,email, address)
  6.     VALUES(#{id},#{name},#{email},#{address})
  7. </insert>
這里我們使用了<selectKey>子元素來生成主鍵值,並將值保存到 Student 對象的 id 屬性上。 屬性
order=“before”表示 MyBatis 將取得序列的下一個值作為主鍵值,並且在執行 INSERT SQL 語句之前將值設置到
id 屬性上
 
方法二:
我們也可以在獲取序列的下一個值時,使用觸發器(trigger)來設置主鍵值,並且在執行 INSERT SQL 語句之
前將值設置到主鍵列上。 如果你采取這樣的方式,則對應的 INSERT 映射語句如下所示:
Xml Code
  1. <insert id="insertStudent" parameterType="Student">
  2.        INSERT INTO STUDENTS(name,email, address)   VALUES(#{name},#{email},#{address})
  3.        <selectKey keyProperty="studId" resultType="int" order="AFTER">
  4.               SELECT ELEARNING.STUD_ID_SEQ.CURRVAL FROM DUAL
  5.         </selectKey>
  6. </insert>
 
3.Sql Server顯式插入主鍵
Sql Server中可以設置主鍵自增,但是一旦設置為主鍵自增,我們不可以默認顯式的插入主鍵值
開啟顯式插入主鍵值得sql語句:
SET IDENTITY_INSERT tableName  ON (OFF為關閉的方法)
注:有時候我們並不需要手動去關閉顯式插入主鍵值,因為在每一個新的數據庫連接中SQL Server會自動調整為默認不開啟
Xml Code
  1. <update id="setIdentityInsert" parameterType="java.lang.String">
  2.         SET IDENTITY_INSERT Student ${_parameter}
  3. </update>
注: MyBatis中傳入String類型參數的時候,SQL語句中的參數名必須為_parameter
 
 
 






免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM