使用mapper代理方式对数据进行增、删、改、查--使用了接口--进行多个参数的传递。
注意事项:
(1)xml文件(mapper)的名称必须与接口的名称相同;
(2)select、update、insert中的id的名称必须与接口中方法的名称相同;
(3)xml(mapper)中namespace必须是接口的全路径+接口名。
实体类EMP
public class EMP {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate;
private int sal;
private int comm;
private int deptno;
public EMP(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.mgr = mgr;
this.hiredate = hiredate;
this.sal = sal;
this.comm = comm;
this.deptno = deptno;
}
public EMP(int empno, String ename, String job, Date hiredate) {
this.empno = empno;
this.ename = ename;
this.job = job;
this.hiredate = hiredate;
}
public EMP(int empno) {
this.empno = empno;
}
public EMP() {
}
public int getEmpno() {
return empno;
}
public void setEmpno(int empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public int getMgr() {
return mgr;
}
public void setMgr(int mgr) {
this.mgr = mgr;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getComm() {
return comm;
}
public void setComm(int comm) {
this.comm = comm;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
@Override
public String toString() {
return "EMP{" +
"empno=" + empno +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mgr=" + mgr +
", hiredate=" + new SimpleDateFormat("yyyy-MM-dd").format(hiredate) +
", sal=" + sal +
", comm=" + comm +
", deptno=" + deptno +
'}';
}
}
查询数据库信息
接口EMPMapper
public interface EMPMapper {
public List<EMP> selectAll();
}
EMPMapper.xml(与接口同名)
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.gakki.MyaBatis.EMPMapper">//必须是接口路径+接口名
<select id="selectAll" resultType="emp"> //id与接口中方法同名
select * from emp;
</select>
</mapper>
测试类:
public class Test3 {
public static void main(String[] args) throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
EMPMapper empMapper=session.getMapper(EMPMapper.class);
List<EMP> list= empMapper.selectAll();
for (EMP emp:list){
System.out.println(emp);
}
session.close();
}
}
向数据库插入数据
EMPMapper接口
public interface EMPMapper {
public List<EMP> selectAll(); //查询返回的是集合
public int insertEmp(EMP emp); //插入不用返回值,此处插入成功返回正数
}
EMPMapper.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="cn.gakki.MyaBatis.EMPMapper">
<insert id="insertEmp" parameterType="emp"> //参数类型
insert into emp (empno,ename,job,hiredate) values(#{empno},#{ename},#{job},#{hiredate})
</insert>
</mapper>
测试类
public class Test3 {
public static void main(String[] args) throws IOException, ParseException {
InputStream is = Resources.getResourceAsStream("mybatis.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
SqlSession session = factory.openSession();
EMPMapper empMapper=session.getMapper(EMPMapper.class);
EMP emp=new EMP(9999,"Mary","clerk",new SimpleDateFormat("yyyy-MM-dd").parse("2019-9-4"));
int i = empMapper.insertEmp(emp);
System.out.println(i);
session.commit();
session.close();
}
}
删除数据库信息
EMPMapper接口
EMPMapper.xml
更改数据库信息
EMPMapper接口
测试类