工程目录:
一、@Results映射
Client.java
package client;
import java.io.*;
import java.util.List;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import domain.Employee;
import domain.UserMapper;
public class Client {
public static void main(String args[]) throws IOException {
InputStream in=null;
SqlSessionFactory factory=null;
SqlSession sqlSession=null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
}
catch(IOException e) {
e.printStackTrace();
}
try {
factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
List list = userMapper.selectEmpByEmpId(1);
for(Employee emp:list)
System.out.println(emp.getDeptId()+" "+emp.getEmpId()+" "+emp.getName());
sqlSession.commit();
}
catch(Exception e) {
e.printStackTrace();
if(sqlSession!=null) {
sqlSession.rollback();
}
}
finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
}
}
Department.java
package domain;
public class Department {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
Employee.java
package domain;
public class Employee {
private int empId;
private String name;
private int deptId;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
}
domain.UserMapper.java
package domain;
import java.util.List;
import org.apache.ibatis.annotations.*;
public interface UserMapper {
@Select("select * from employee where empId=#{empId}")
@Results({
@Result(id=true,column="empId",property="empId"),
@Result(column="name",property="name"),
@Result(column="deptId",property="deptId")
})
public List selectEmpByEmpId(int empId);
}
mapper.UserMapper.xml
Employee表:
查询结果:
二、@ResultMap映射,当要重复使用这个 resultMap时,可用@ResultMap注解在各个需要的接口方法上引用它
Client.java
package client;
import java.io.*;
import java.util.List;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import domain.Employee;
import domain.EmpMapper;
public class Client {
public static void main(String args[]) throws IOException {
InputStream in=null;
SqlSessionFactory factory=null;
SqlSession sqlSession=null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
}
catch(IOException e) {
e.printStackTrace();
}
try {
factory = new SqlSessionFactoryBuilder().build(in);
factory.getConfiguration().addMapper(EmpMapper.class);
sqlSession = factory.openSession();
EmpMapper userMapper=sqlSession.getMapper(EmpMapper.class);
List list = userMapper.selectEmpByEmpId(1);
for(Employee emp:list)
System.out.println(emp.getDeptId()+" "+emp.getEmpId()+" "+emp.getName());
sqlSession.commit();
}
catch(Exception e) {
e.printStackTrace();
if(sqlSession!=null) {
sqlSession.rollback();
}
}
finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
}
}
domain.EmpMapper.java
package domain;
import java.util.List;
import org.apache.ibatis.annotations.*;
public interface EmpMapper {
@Select("select * from employee where empId=#{empId}")
@ResultMap("mapper.UserMapper.empResultMap")
public List selectEmpByEmpId(int empId);
}
mapper.UserMapper.xml
查询结果:
三、@ResultMap配置一对一关联查询
Client.java
package client;
import java.io.*;
import java.util.List;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import domain.Employee;
import domain.EmpMapper;
public class Client {
public static void main(String args[]) throws IOException {
InputStream in=null;
SqlSessionFactory factory=null;
SqlSession sqlSession=null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
}
catch(IOException e) {
e.printStackTrace();
}
try {
factory = new SqlSessionFactoryBuilder().build(in);
factory.getConfiguration().addMapper(EmpMapper.class);
sqlSession = factory.openSession();
EmpMapper userMapper=sqlSession.getMapper(EmpMapper.class);
List list = userMapper.selectEmpByEmpId(1);
for(Employee emp:list)
System.out.println(emp.getDeptId()+" "+emp.getEmpId()+" "+emp.getName()+" "+emp.getDepts().getDeptName());
sqlSession.commit();
}
catch(Exception e) {
e.printStackTrace();
if(sqlSession!=null) {
sqlSession.rollback();
}
}
finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
}
}
Employee.java
package domain;
public class Employee {
private int empId;
private String name;
private int deptId;
private Department depts;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public Department getDepts() {
return depts;
}
public void setDepts(Department depts) {
this.depts = depts;
}
}
Department.java
package domain;
public class Department {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
EmpMapper.java(interface)
package domain;
import java.util.List;
import org.apache.ibatis.annotations.*;
public interface EmpMapper {
@Select("select * from employee,department where empId=#{empId}")
@ResultMap("mapper.UserMapper.empAndDeptResultMap")
public List selectEmpByEmpId(int empId);
}
mapper.UserMapper.xml
employee表:
department表:
查询结果:
四、@ResultMap配置一对多查询
Client.java
package client;
import java.io.*;
import org.apache.ibatis.io.*;
import org.apache.ibatis.session.*;
import domain.Employee;
import domain.Department;
import domain.EmpMapper;
public class Client {
public static void main(String args[]) throws IOException {
InputStream in=null;
SqlSessionFactory factory=null;
SqlSession sqlSession=null;
try {
in = Resources.getResourceAsStream("mybatis-config.xml");
}
catch(IOException e) {
e.printStackTrace();
}
try {
factory = new SqlSessionFactoryBuilder().build(in);
factory.getConfiguration().addMapper(EmpMapper.class);
sqlSession = factory.openSession();
EmpMapper userMapper=sqlSession.getMapper(EmpMapper.class);
Department dept = userMapper.selectDeptBydeptId(1);
for(Employee emp:dept.getEmps())
System.out.println(emp.getName()+" "+dept.getDeptId()+" "+dept.getDeptName());
sqlSession.commit();
}
catch(Exception e) {
e.printStackTrace();
if(sqlSession!=null) {
sqlSession.rollback();
}
}
finally {
if(sqlSession!=null) {
sqlSession.close();
}
}
}
}
Department.java
package domain;
import java.util.List;
public class Department {
private int deptId;
private String deptName;
private List emps;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public List getEmps() {
return emps;
}
public void setEmps(List emps) {
this.emps = emps;
}
}
Employee.java
package domain;
public class Employee {
private int empId;
private String name;
private int deptId;
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
}
EmpMapper.java(interface)
package domain;
import org.apache.ibatis.annotations.*;
public interface EmpMapper {
@Select("select * from department,employee where department.deptId=employee.deptId and department.deptId=#{deptId}")
@ResultMap("mapper.UserMapper.deptAndEmpResultMap")
public Department selectDeptBydeptId(int deptId);
}
mapper.UserMapper.xml