增删改查

1.实体类 Student_new.java

import java.util.Date;

public class Student_new {
    private Integer sid;
    private String sname;
    private Integer snum;
    private String ssex;
    private String stel;
    private String semail;
    private Date sbirth;
    private String sdept;

    public Student_new(Integer sid, String sname, Integer snum, String ssex, String stel, String semail, Date sbirth, String sdept) {
        this.sid = sid;
        this.sname = sname;
        this.snum = snum;
        this.ssex = ssex;
        this.stel = stel;
        this.semail = semail;
        this.sbirth = sbirth;
        this.sdept = sdept;
    }

    public Integer getSid() {
        return sid;
    }

    public void setSid(Integer sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public Integer getSnum() {
        return snum;
    }

    public void setSnum(Integer snum) {
        this.snum = snum;
    }

    public String getSsex() {
        return ssex;
    }

    public void setSsex(String ssex) {
        this.ssex = ssex;
    }

    public String getStel() {
        return stel;
    }

    public void setStel(String stel) {
        this.stel = stel;
    }

    public String getSemail() {
        return semail;
    }

    public void setSemail(String semail) {
        this.semail = semail;
    }

    public Date getSbirth() {
        return sbirth;
    }

    public void setSbirth(Date sbirth) {
        this.sbirth = sbirth;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }
}

2.Mapper接口 StudentMapper.java

import java.util.List;

public interface StudentMapper {
      public List<Student_new> queryStudent();
      public Student_new getStudentById(Integer sid);
      public void addStudent(Student_new student_new);
      public void delStudent(Integer sid);
      public void updateStudent(Student_new student_new);
}

  

3.SQL映射文件 StudentMapper.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="com.xQuant.platform.app.auth.dao.StudentMapper">
    <select id="queryStudent" resultType="com.xQuant.platform.app.auth.entity.Student_new">
        SELECT * FROM STUDENT_NEW_1
    </select>
    <select id="getStudentById" resultType="com.xQuant.platform.app.auth.entity.Student_new">
        SELECT * FROM STUDENT_NEW_1 WHERE SID = #{SID};
    </select>
    <insert id="addStudent" >
        INSERT INTO STUDENT_NEW_1(SID,SNAME,SNUM,SSEX,SEMAIL,SBIRTH,SDEPT) VALUE (
        #{SID},#{SNAME},#{SNUM},#{SSEX},#{SEMAIL},#{SBIRTH},#{SDEPT})
    </insert>
    <update id="updateStudent">
        UPDATE STUDENT_NEW_1 SET SNAME=#{SNAME},SSEX=#{SEX},SEMAIL=#{SEMAIL},
        SBIRTH=#{SBIRTH},SDEPT=#{SDEPT} WHERE SID=#{SID}
    </update>
    <delete id="delStudent">
        DELETE FROM STUDENT_NEW_1 WHERE SID=#{SID}
    </delete>
</mapper>

4.工具类 MybatisUtils 获取SqlSession实例

public class MybatisUtils {
    public SqlSession getopenSession() throws Exception {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSession = new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession openSession = sqlSession.openSession();
        return openSession;
    }
}

5.service 服务类 StudentService.java 实现mapper接口方法

import java.util.List;

@Service
public class StudentService extends MybatisUtils {

    public List<Student_new> selectAllStudents() throws Exception {
        SqlSession session = getopenSession();
        try {
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            List<Student_new> students = studentMapper.queryStudent();
            return students;
        }finally {
            session.close();
        }
    }
    public Student_new getStudentById(Integer sid) throws Exception{
        SqlSession session = getopenSession();
        try {
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            return studentMapper.getStudentById(sid);
        }finally {
            session.close();
        }
    }
    public void addStudent(Student_new student_new) throws Exception{
        SqlSession session = getopenSession();
        try {
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            studentMapper.addStudent(student_new);
            session.commit();/*提交事务*/
        }finally {
            session.close();/*当前会话连接关闭 断开与数据库的连接*/
        }
    }
    public void delStudent(Integer sid) throws Exception{
        SqlSession session = getopenSession();
        try{
            StudentMapper studentMapper = session.getMapper(StudentMapper.class);
            studentMapper.delStudent(sid);
            session.commit();
        }finally {
            session.close();
        }
    }
}

  

原文地址:https://www.cnblogs.com/iM59/p/15458850.html