二.Mybatis 增删改查

Student.java
package com.pojo;

import java.util.Date;

public class Student {
    int stuid;
    String stuName;
    int stuAge;
    Date stuDate;
    String stuSex;
    String stuProfess;
    String status;
    String professName;
    
    
    public int getStuid() {
        return stuid;
    }
    public void setStuid(int stuid) {
        this.stuid = stuid;
    }
    public String getStuName() {
        return stuName;
    }
    public void setStuName(String stuName) {
        this.stuName = stuName;
    }
    public int getStuAge() {
        return stuAge;
    }
    public void setStuAge(int stuAge) {
        this.stuAge = stuAge;
    }
    public Date getStuDate() {
        return stuDate;
    }
    public void setStuDate(Date stuDate) {
        this.stuDate = stuDate;
    }
    public String getStuSex() {
        return stuSex;
    }
    public void setStuSex(String stuSex) {
        this.stuSex = stuSex;
    }
    public String getStuProfess() {
        return stuProfess;
    }
    public void setStuProfess(String stuProfess) {
        this.stuProfess = stuProfess;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
    public String getProfessName() {
        return professName;
    }
    public void setProfessName(String professName) {
        this.professName = professName;
    }
    
}
  IStudentDAO.java
      
    使用合理描述参数和SQL语句返回值的接口(比如IStudentDAO.class),这样更简单,更安全的代码,便于和其他优秀框架结合
package com.dao;

import com.pojo.Student;

public interface IStudentDAO {
    Student getStudentById(int stuid);
    
    void addStudent(Student stu);
    
    void delStudent(int id);

    void updateStudent(int id);
}

  Student.xml

    这里的 id="addStudent" 要和IStudentDAO.java接口中的方法名一致

    resultType="student"为实体类在Configuration.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.dao.IStudentDAO">
    <select id="getStudentById" parameterType="int" resultType="student">
        select a.*,b.professname from student a,profess b where a.stuprofess=b.proid and stuid=#{id}    
    </select>
    <insert id="addStudent" parameterType="com.pojo.Student" keyProperty="stuid">
        insert into student values(#{stuid},#{stuName},#{stuAge},#{stuDate},#{stuSex,jdbcType=CHAR},#{stuProfess})
    </insert>
    <delete id="delStudent" parameterType="int">
        delete from student where stuid=#{id}
    </delete>
    <update id="updateStudent" parameterType="int">
        update student set stuName=#{stuName} where stuid=#{id}
    </update>
</mapper>

  测试代码 

    Test.java
package com.pojo;

import java.io.IOException;
import java.io.Reader;
import java.util.Date;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import com.dao.IStudentDAO;

public class Test {
    public static void main(String[] args) throws IOException {
        //读取核心的配置文件
        Reader reader = Resources.getResourceAsReader("Configuration.xml");
        //创建会话工厂
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);
        //数据库会话
        SqlSession session = factory.openSession();
        //查询一个对象  第一个参数字符串是由Student.xml 中的namespace+id组成
//        Student stu = session.selectOne("com.dao.StudentMapper.getStudentById",5);
        
        //通过接口的方式
        IStudentDAO stuDAO = session.getMapper(IStudentDAO.class);
//        Student stu = stuDAO.getStudentById(5);
//        
//        System.out.println(stu.getStuName());
//        System.out.println(stu.getStuDate());
//        System.out.println(stu.getProfessName());
        
        //测试新增
        Student stu = new Student();
        stu.setStuid(16);
        stu.setStuName("测试新增");
        stu.setStuDate(new Date());
//        stu.setStuSex("1");
//        stu.setStuProfess("1");
//        stuDAO.addStudent(stu);
        
        //测试删除
        stuDAO.delStudent(16);
        session.commit();
        session.close();
    }
}

 

posted @ 2019-04-30 11:40  五柳先生柳三变  阅读(173)  评论(0编辑  收藏  举报