标签:
1.一个 INSERT SQL 语句可以在<insert>元素在映射器 XML 配置文件中配置
<insert id="insertStudentWithId" parameterType="Student">
INSERT INTO Student(id,name,sex,birthday,height,weight,score,address,email,hobby) values
(#{id},#{name},#{sex},#{birthday},#{height},#{weight},#{score},#{address},#{email},#{hobby})
</insert>
<insertid="insertStudentWithoutId"parameterType="Student"useGeneratedKeys="true"keyProperty="id" >
INSERT INTO Student (name,sex,birthday,height,weight,score,address,email,hobby) values
(#{name},#{sex},#{birthday},#{height},#{weight},#{score},#{address},#{email},#{hobby})
</insert>
@Test
publicvoid testInsertWithoutId()
{
SqlSession sqlSession =MyBatisSqlSessionFactory.openSession();
try{
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student =newStudent();
student.setName("小2");
student.setAddress("江苏南通");
student.setBirthday(newDate());
student.setEmail("xiao2@live.com");
student.setHeight(177);
student.setHobby("打篮球");
student.setScore(99);
student.setSex("男");
student.setWeight(120);
studentMapper.insertStudentWithoutId(student);
sqlSession.commit();
System.out.println(student);
}finally{
sqlSession.close();
}
}
<insertid="insertStudent"parameterType="Student">
<selectKeykeyProperty="id"resultType="int"order="BEFORE">
SELECT ELEARNING.STUD_ID_SEQ.NEXTVAL FROM DUAL
</selectKey>
INSERT INTO STUDENTS(id,name,email, address)
VALUES(#{id},#{name},#{email},#{address})
</insert>
<insert id="insertStudent" parameterType="Student">
INSERT INTO STUDENTS(name,email, address) VALUES(#{name},#{email},#{address})
<selectKey keyProperty="studId" resultType="int" order="AFTER">
SELECT ELEARNING.STUD_ID_SEQ.CURRVAL FROM DUAL
</selectKey>
</insert>
<update id="setIdentityInsert" parameterType="java.lang.String">
SET IDENTITY_INSERT Student $(_parameter)
</update>
标签:
原文地址:http://www.cnblogs.com/xiao2/p/5728788.html