版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Yuan52007298/article/details/81429431
先把自定义的sql语句亮出来,其中有添加数据并返回主键的sql写法,主要就在于sql语句的书写配置,使用标签<selectKey>z并指定结果类型,映射实体属性,和先后顺序。使用方法就是添加操作完成,mybatis获取自增主键值,并setId()
结果如下,添加之前id为null,添加之后id被赋值。对应测试方法为 public void testInsertReturnId()
mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="student">
<insert id="insertStduent" parameterType="com.Demo.Entity.Student">
insert into student(name,age,score) values(#{name},#{age},#{score})
</insert>
<!-- 使用别名 -->
<insert id="insertStduentReturnId" parameterType="Student">
insert into student(name,age,score) values(#{name},#{age},#{score})
<!-- mysql的自增主键在insert语句之后,所以AFTER。ORACLE数据库的逐渐自增在inert语句之前,所以BEFORE -->
<selectKey resultType="int" keyProperty="id" order="AFTER">
select @@identity
</selectKey>
</insert>
<delete id="deleteStudent">
<!-- #{xxx}是占位符,名称随意,参数自动填充 -->
delete from Student where id = #{xxx}
</delete>
<update id="updateStudent">
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>
<!-- select结果数据集需要封装到实体类中,需要设置属性resultType -->
<select id="selectAllStudent" resultType="Student">
select id,name,age,score from student
</select>
<select id="selectStudentById" resultType="Student">
select id,name,age,score from student where id=#{xxx}
</select>
</mapper>
接口,方法,Utils 和 实体类(Util和实体类见https://blog.csdn.net/Yuan52007298/article/details/81413042)
package com.Demo.Dao;
import java.util.List;
import com.Demo.Entity.Student;
public interface StudentDao {
void insertStudent(Student student);
void insertStudentReturnId(Student student);
void deleteStudent(Integer id);
void updateStudent(Student student);
List<Student> selectAllStudent();
Student selectStudentById(Integer id);
}
package com.Demo.Dao;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.Demo.Entity.Student;
import com.Demo.Utils.MyBatisUtil;
public class StudentDaoImpl implements StudentDao {
@Override
public void insertStudent(Student student) {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
sqlSession.insert("student.insertStduent", student);
sqlSession.commit();
sqlSession.close();
}
@Override
public void insertStudentReturnId(Student student) {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
sqlSession.insert("student.insertStduentReturnId", student);
sqlSession.commit();
sqlSession.close();
}
@Override
public void deleteStudent(Integer id) {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
sqlSession.delete("student.deleteStudent", id);
sqlSession.commit();
sqlSession.close();
}
@Override
public void updateStudent(Student student) {
SqlSession sqlSession = MyBatisUtil.getSqlSession();
sqlSession.update("student.updateStudent", student);
sqlSession.commit();
sqlSession.close();
}
@Override
public List<Student> selectAllStudent() {
List<Student> studentList = new ArrayList<>();
SqlSession sqlSession = MyBatisUtil.getSqlSession();
studentList = sqlSession.selectList("student.selectAllStudent");
sqlSession.commit();
sqlSession.close();
return studentList;
}
@Override
public Student selectStudentById(Integer id) {
Student stu = new Student();
SqlSession sqlSession = MyBatisUtil.getSqlSession();
stu = sqlSession.selectOne("selectStudentById", id);
sqlSession.commit();
sqlSession.close();
return stu;
}
}