配置
<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);
}