用mybatis连接数据库,查询到的结果列中deptid老显示为0,是啥原因,求大神们给看看!
测试代码如下:
package Test;
import java.io.IOException;
import java.util.List;
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 org.junit.Test;
import DAO.EmpDAO;
import Entity.Dept;
import Entity.Emp;
public class EmpTest {
@Test
public void findAll() throws IOException{
String resource="config.xml";
SqlSessionFactory sf=new SqlSessionFactoryBuilder() .build(Resources.getResourceAsReader(resource));
SqlSession session=sf.openSession();
EmpDAO empDAO=session.getMapper(EmpDAO.class);
List<Emp> list=empDAO.findAll();
for(Emp emp:list){
System.out.println(emp.getId()+","+emp.getEname()+","+emp.getSal()+","+emp.getDeptid()+","+emp.getDept().getDname()+","+emp.getDept().getLoc() );
}
}
}
实体类Emp
package Entity;
public class Emp {
private int id;
private String ename;
private int sal;
private int deptid;
private Dept dept;
public Dept getDept() {
return dept;
}
public Emp() {
}
public Emp(int id, String ename, int sal, int deptid) {
super();
this.id = id;
this.ename = ename;
this.sal = sal;
this.deptid = deptid;
}
public void setDept(Dept dept) {
this.dept = dept;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public int getSal() {
return sal;
}
public void setSal(int sal) {
this.sal = sal;
}
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
@Override
public String toString() {
return "Emp [deptid=" + deptid + ", ename=" + ename + ", id=" + id
+ ", sal=" + sal + "]";
}
}
实体类dept
package Entity;
public class Dept {
private int deptid;
private String dname;
private String loc;
public Dept() {
}
public Dept(int deptid, String dname, String loc) {
super();
this.deptid = deptid;
this.dname = dname;
this.loc = loc;
}
public int getDeptid() {
return deptid;
}
public void setDeptid(int deptid) {
this.deptid = deptid;
}
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 [deptid=" + deptid + ", dname=" + dname + ", loc=" + loc
+ "]";
}
}
DAO类如下
package DAO;
import java.util.List;
import Entity.Emp;
public interface EmpDAO {
List findAll();
}
config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
alias="Emp" />
alias="Dept" />
<environments default="development">
<environment id="development">
<transactionManager type="jdbc" />
<dataSource type="POOLED">
<property name="driver"
value="oracle.jdbc.OracleDriver" />
<property name="url"
value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" />
<property name="username" value="system" />
<property name="password" value="jsw123" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="DAO/Emp.xml" />
</mappers>
Emp.xml
select
e.id id,
e.ename ename,
e.sal sal,
e.dept_id deptid,
d.dname dname,
d.loc loc
from Emp e join Dept d on e.dept_id=d.id
数据库为
EMP(id,ename,sal,dept_id)
dept(id,dname,loc)