我是靠谱客的博主 专注鞋子,最近开发中收集的这篇文章主要介绍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表:
在这里插入图片描述
查询结果:
在这里插入图片描述

最后

以上就是专注鞋子为你收集整理的Maven使用注解配置SQL映射器(@Results,@ResultMap)的全部内容,希望文章能够帮你解决Maven使用注解配置SQL映射器(@Results,@ResultMap)所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部