天天看點

ibatis學習筆記-SQL主鍵生成方式

配置

<insert id="addStudentBySequence" parameterClass="Student">
		<selectKey resultClass="int" keyProperty="studentid">
			select @@IDENTITY as studentid
			<!-- select last_insert_id() as studentid -->
			<!-- select @@identity as inserted -->
			<!-- 這裡需要說明一下不同的資料庫主鍵的生成,對各自的資料庫有不同的方式: -->
			<!-- mssql:SELECT LAST_INSERT_ID() AS VALUE -->
			<!-- mysql:select @@IDENTITY as value -->
			<!-- oracle:SELECT STOCKIDSEQUENCE.NEXTVAL AS VALUE FROM DUAL -->
			<!-- 還有一點需要注意的是不同的資料庫生産商生成主鍵的方式不一樣,有些是預先生成 (pre-generate)主鍵的,如Oracle和PostgreSQL。 
				有些是事後生成(post-generate)主鍵的,如MySQL和SQL Server 是以如果是Oracle資料庫,則需要将selectKey寫在insert之前 -->
		</selectKey>
		insert into Student (studentid, sname, ssex, sphone, sclass, sdate)
		values 				(#studentid#,#sname#,#ssex#,#sphone#,#sclass#,#sdate#);
	</insert>
           

實作

@Override
	public List<Student> queryStudtentByName(String name) {
		List<Student> studentList = null;
		try
		{  
			studentList = sqlMapClient.queryForList("selectStudentByName",name);
		} catch (SQLException e)
		{
			e.printStackTrace();
		}
		return studentList;
	}
           

測試

//6.模糊查詢 測試List<User> queryUserByName(String name) 
		System.out.println("6.測試List<User> queryStudtentByName(String name)");
		for(Student user3:dao.queryStudtentByName("2"))
		{
		    System.out.println(user3);
		}