在实际业务中,经常能碰到多表关联查询
下面的Demo,讲举例join查询在MyBatis中的实现
User 类:
package com.zy.domain; import java.io.Serializable; import java.util.List; /** * @author gacl * users表所对应的实体类 */ public class User implements Serializable{ //实体类的属性和表的字段名称一一对应 private int id; private String name; private int age; private List<Role> role; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public List<Role> getRole() { return role; } public void setRole(List<Role> role) { this.role = role; } @Override public String toString() { return "User [id=" + id + ", name=" + name + ", age=" + age + "]"; } }
Role 类:
package com.zy.domain; import java.io.Serializable; public class Role implements Serializable{ private int rid; private int userId; private String roleName; public int getRid() { return rid; } public void setRid(int rid) { this.rid = rid; } public int getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } }
Mapping配置文件:
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的 例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) --> <mapper namespace="com.zy.mapping.userMapper"> <!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型 resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 --> <resultMap id="queryForListMap" type="com.zy.domain.User"> <id column="id" property="id" jdbcType="INTEGER"/> <result column="name" property="name" jdbcType="VARCHAR"/> <result column="age" property="age" jdbcType="INTEGER"/> <collection property="role" javaType="java.util.List" ofType="com.zy.domain.Role"> <id column="rid" property="rid" jdbcType="INTEGER" /> <result column="userId" property="userId" jdbcType="VARCHAR" /> <result column="roleName" property="roleName" jdbcType="VARCHAR" /> </collection> </resultMap> <select id="queryForList" resultMap="queryForListMap"> SELECT u.id, u.name, u.age, r.rid, r.userId, r.roleName FROM users u LEFT JOIN role r ON u.id = r.userid </select> </mapper>
Test类:
package com.zy.test; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.List; import com.zy.domain.User; import org.apache.ibatis.io.Resources; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class Test1 { public static void main(String[] args) throws IOException { //mybatis的配置文件 String resource = "config.xml"; //使用类加载器加载mybatis的配置文件(它也加载关联的映射文件) InputStream is = Test1.class.getClassLoader().getResourceAsStream(resource); //构建sqlSession的工厂 SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is); //使用MyBatis提供的Resources类加载mybatis的配置文件(它也加载关联的映射文件) //Reader reader = Resources.getResourceAsReader(resource); //构建sqlSession的工厂 //SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader); //创建能执行映射文件中sql的sqlSession SqlSession session = sessionFactory.openSession(); /** * 映射sql的标识字符串, * me.gacl.mapping.userMapper是userMapper.xml文件中mapper标签的namespace属性的值, * getUser是select标签的id属性值,通过select标签的id属性值就可以找到要执行的SQL */ /*String statement = "com.zy.mapping.userMapper.getUser";//映射sql的标识字符串 //执行查询返回一个唯一user对象的sql User user = session.selectOne(statement, 1);*/ /*String statement = "com.zy.mapping.userMapper.addUser"; User user = new User(); user.setName("张三"); user.setAge(20); int user2 = session.insert(statement, user); session.commit(); session.close(); System.out.println(user2);*/ //updateUser(session); //deleteUser(session); queryList(session); } public static void queryList(SqlSession session){ String statement = "com.zy.mapping.userMapper.queryForList"; List<Object> resultList = session.selectList(statement); System.out.println(resultList); } }