最近我们在学习result嵌套结果和嵌套查询。今天就来总结一下。
首先呢,我们要 做好准备工作。
1.在数据库中创建两个表。
2.准备好我们需要的jar包
3.准备好我们要的配置文件
4.创建好两个表的pojo包
准备好这四个jar包,前两个用于日志,第三个是mybatis的,第四个JDBC的。导入jar包后,还要确定是否加入项目中。
#key=value
mysql.driver=com.mysql.jdbc.Driver
mysql.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
mysql.username=root
mysql.password=
oracle.driver=com.mysql.jdbc.Driver
oracle.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
oracle.username=root
oracle.password=
log4j.rootLogger=debug,cons
log4j.appender.cons=org.apache.log4j.ConsoleAppender
log4j.appender.cons.layout=org.apache.log4j.PatternLayout
log4j.appender.cons.layout.ConversionPattern=%5p [%t] - %m%n
记得配置文件后面都不要加空格,不然可能会报错哦~
Dept.java
public class Dept {
private int deptno;
private String dname;
private String loc;
//有参构造函数
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
//无参构造函数
public Dept() {
super();
// TODO Auto-generated constructor stub
}
//get、set方法
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
//打印函数
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
}
}
Emp.java
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate; //java.sql.Date只包含年月日,java.util.Date包含年月日,时分秒
private int sal;
private int comm;
private int deptno;
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=" + hiredate
+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]";
}
public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno) {
super();
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() {
super();
}
}
函数大部分都是可以自己生成的哦~
到这里我们的准备工作就已经做完了,接下来我们进入主题吧~
修改Emp.java代码
public class Emp {
private int empno;
private String ename;
private String job;
private int mgr;
private Date hiredate; //java.sql.Date只包含年月日,java.util.Date包含年月日,时分秒
private int sal;
private int comm;
private int deptno;
private Dept depts;//一对一(新加的属性)
public Dept getDepts() {
return depts;
}
public void setDepts(Dept depts) {
this.depts = depts;
}
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=" + hiredate
+ ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + ", depts=" + depts + "]";
}
public Emp(int empno, String ename, String job, int mgr, Date hiredate, int sal, int comm, int deptno) {
super();
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() {
super();
}
}
package mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import pojo.Emp;
public interface EmpMapper {
//嵌套结果一对一
@Results({
@Result(column="empno",property="empno"),
@Result(column="ename",property="ename"),
@Result(column="deptno",property="depts.deptno"),
@Result(column="dname",property="depts.dname"),
@Result(column="loc",property="depts.loc"),
})
@Select("select * from emp e,dept d where e.deptno=d.deptno and empno=#{empno}")
Emp selectEmp(int empno);
}
public class demo01 {
public static void main(String[] args) throws Exception {
SqlSession session = null;
try{
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(
Resources.getResourceAsReader("mybatis.xml"));
session = build.openSession();
// 创建接口的实现类getMapper(Class c)参数是告诉我们创建哪个接口的实现类
EmpMapper mapper = session.getMapper(EmpMapper.class);
Emp emp = mapper.selectEmp(8500);
session.commit();
System.out.println(emp);
}catch(IOException e){
e.printStackTrace();
}finally {
if(session != null){
session.close();
}
}
}
}
public class Dept {
private int deptno;
private String dname;
private String loc;
private List emps;//一对多(新加的属性)
public Dept() {
super();
}
public Dept(int deptno, String dname, String loc) {
super();
this.deptno = deptno;
this.dname = dname;
this.loc = loc;
}
@Override
public String toString() {
return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", emps=" + emps + "]";
}
public List getEmps() {
return emps;
}
public void setEmps(List emps) {
this.emps = emps;
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
public interface DeptMapper {
//嵌套结果一对多
@ResultMap("mapper.DeptMapper.selectDeptMap")
@Select("select * from emp,dept where emp.deptno=dept.deptno and dept.deptno=#{deptno}")
Dept selectDept(int deptno);
}
public class demo02 {
public static void main(String[] args){
SqlSession session = null;
try{
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(
Resources.getResourceAsReader("mybatis.xml"));
session = build.openSession();
// 创建接口的实现类getMapper(Class c)参数是告诉我们创建哪个接口的实现类
DeptMapper mapper = session.getMapper(DeptMapper.class);
Dept dept = mapper.selectDept(20);
session.commit();
System.out.println(dept);
}catch(IOException e){
e.printStackTrace();
}finally {
if(session != null){
session.close();
}
}
}
}
@Select: 执行查询语句
@Results:结果映射的列表,相当于
@Result: 在列和属性或字段之间的单独结果映射,相当于resultMap 元素中的 和属性
@Many: 复杂类型的集合属性映射,相等于
@One: 单独属性值映射,相当于
两个pojo包不用做修改,和上面的一样。
public interface EmpMapper {
/**
* column 关联字段
* property 属性名称
* one=@One 表示一对一
* select:需要去执行的另一条sql路径
* @param empno
* @return
*/
//嵌套查询一对一
@Results({
@Result(column="empno",property="empno"),
@Result(column="deptno",property="depts",
one=@One(select="mapper.DeptMapper.selectDept"))
})
@Select("select * from emp where empno=#{empno}")
Emp selectEmp(int empno);
}
public interface DeptMapper {
@Select("Select * from dept Where deptno=#{deptno}")
Dept selectDept(int deptno);
}
SqlSession session = null;
try{
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(
Resources.getResourceAsReader("mybatis.xml"));
session = build.openSession();
// 创建接口的实现类getMapper(Class c)参数是告诉我们创建哪个接口的实现类
EmpMapper mapper = session.getMapper(EmpMapper.class);
Emp emp = mapper.selectEmp(8500);
session.commit();
System.out.println(emp);
}catch(IOException e){
e.printStackTrace();
}finally {
if(session != null){
session.close();
}
public interface DeptMapper {
//嵌套查询一对多
@Select("select * from dept where deptno=#{id}")
@Results({
@Result(property="deptno",column="deptno"),
@Result(property="emps",column="deptno", many=@Many(select="mapper.EmpMapper.selectEmp1"))
})
Dept selectDept1(int deptno);
}
public interface EmpMapper {
@Select("select * from emp where deptno = #{id}")
Emp selectEmp1(int deptno);
}
SqlSession session = null;
try{
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(
Resources.getResourceAsReader("mybatis.xml"));
session = build.openSession();
// 创建接口的实现类getMapper(Class c)参数是告诉我们创建哪个接口的实现类
DeptMapper mapper = session.getMapper(DeptMapper.class);
Dept dept = mapper.selectDept1(20);
session.commit();
System.out.println(dept);
}catch(IOException e){
e.printStackTrace();
}finally {
if(session != null){
session.close();
}
}