Maven使用注解配置SQL映射器(@Results,@ResultMap)

工程目录:
在这里插入图片描述
一、@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<Employee> 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<Employee> selectEmpByEmpId(int empId);
}

mapper.UserMapper.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="domain.UserMapper">
</mapper>

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<Employee> 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<Employee> selectEmpByEmpId(int empId);
}

mapper.UserMapper.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="mapper.UserMapper">
	<resultMap type="domain.Employee" id="empResultMap">
		<id property="empId" column="empId" />
		<result property="name" column="name" />
		<result property="deptId" column="deptId" />
	</resultMap>
</mapper>

查询结果:
在这里插入图片描述
三、@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<Employee> 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<Employee> selectEmpByEmpId(int empId);
}

mapper.UserMapper.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="mapper.UserMapper">
	<resultMap type="domain.Employee" id="empAndDeptResultMap">
		<id property="empId" column="empId" />
		<result property="name" column="name" />
		<result property="deptId" column="deptId" />
		<association property="depts" resultMap="deptResultMap"></association>
	</resultMap>
	<resultMap type="domain.Department" id="deptResultMap">
		<id property="deptId" column="deptId" />
		<result property="deptName" column="deptName" />
	</resultMap>
</mapper>

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<Employee> 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<Employee> getEmps() {
		return emps;
	}
	public void setEmps(List<Employee> 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

<?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="mapper.UserMapper">
	<resultMap type="domain.Employee" id="empResultMap">
		<id property="empId" column="empId" />
		<result property="name" column="name" />
		<result property="deptId" column="deptId" />
	</resultMap>
	<resultMap type="domain.Department" id="deptAndEmpResultMap">
		<id property="deptId" column="deptId" />
		<result property="deptName" column="deptName" />
		<collection property="emps" resultMap="empResultMap"></collection>
	</resultMap>
</mapper>

employee表:
在这里插入图片描述
department表:
在这里插入图片描述
查询结果:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值