JavaWeb_(Mybatis框架)关联查询_六
系列博文:
JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门
JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二 传送门
JavaWeb_(Mybatis框架)Mapper动态代理开发_三 传送门
JavaWeb_(Mybatis框架)主配置文件介绍_四 传送门
JavaWeb_(Mybatis框架)输入和输出参数_五 传送门
JavaWeb_(Mybatis框架)关联查询_六传送门 传送门
JavaWeb_(Mybatis框架)动态sql_七传送门 传送门
数据库中存在两张表user表和country表

User表和Country表属于一对一:一个用户属于一个国家
Country表和User表属于一对多:一个国家有多个用户

一、一对一的查询
通过user为基准,查询user表中所有数据

SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
Gary.sql
UserVO.class中包含Country对象,通过用户去查询它属于哪一个国家
package com.Gary.bean; //包装类
public class UserVo extends User{ //包装类 private Country country; public Country getCountry() {
return country;
} public void setCountry(Country country) {
this.country = country;
} @Override
public String toString() {
return "UserVo [country=" + country + ", toString()=" + super.toString() + ", getU_id()=" + getU_id()
+ ", getU_password()=" + getU_password() + "]";
} }
UserVo.class
在UserMapper.xml中编写SQL语句,查询所有用户包装类
<id> 和 <result>区别:
id 和 result 元素都将一个列的值映射到一个简单数据类型(String, int, double, Date 等)的属性或字段。
这两者之间的唯一不同是,id 元素表示的结果将是对象的标识属性,这会在比较对象实例时用到。 这样可以提高整体的性能,尤其是进行缓存和嵌套结果映射(也就是连接映射)的时候。
<!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select>
<?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="com.Gary.mapper.UserMapper"> <select id="selectUserById" parameterType="Integer" resultType="user">
select * from user where u_id = #{id}
</select> <!-- #{}占位符 尽量使用#{}来解决问题 -->
<!-- ${}字符串拼接 容易sql注入 (or 1 = 1) --> <!-- ${value}中间的字符串一定需要使用value -->
<select id="selectUserByName" parameterType="String" resultType="com.Gary.bean.User">
<!-- select * from user where u_username like '%${value}%' -->
select * from user where u_username like "%"#{name}"%"
</select> <!-- 添加用户 参数为全包名 -->
<insert id="insertUser" parameterType="com.Gary.bean.User">
insert into user values(null,#{u_username},#{u_password},#{u_sex},#{u_createTime},#{u_cid})
</insert> <!-- 根据id修改username字段的语句 -->
<update id="updateUser" parameterType="com.Gary.bean.User">
update user set u_username = #{u_username} where u_id = #{u_id}
</update> <!-- 根据id删除用户 -->
<delete id="deleteUserById" parameterType="Integer">
delete from user where u_id = #{id}
</delete> <!-- 根据UserVo中的User对象的u_id去查询查询用户 -->
<select id="selectUserByVoId" parameterType="UserVo" resultType="user">
select * from user where u_id = #{user.u_id}
</select> <!-- 查询用户的总条数 -->
<select id="selectUserCount" resultType="Integer">
select count(*) from user
</select> <!-- 查询所有用户包装类 -->
<resultMap type="UserVo" id="uservolist">
<!-- 必须把想要查询数据库的语句都写上 -->
<id property="u_id" column="u_id"/>
<id property="u_username" column="u_username"/>
<id property="u_sex" column="u_sex"/>
<association property="country" javaType="Country">
<!-- 必须把想要查询数据库的语句都写上 -->
<result property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
</association>
</resultMap> <select id="selectAllUserVo" resultMap="uservolist">
SELECT * FROM USER u LEFT JOIN country c ON u.u_cid = c.c_id
</select> </mapper>
UserMapper.xml

package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
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 com.Gary.bean.Country;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest6 { @Test
public void Test6() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); UserMapper mapper = session.getMapper(UserMapper.class); List<UserVo> list = mapper.selectAllUserVo(); for(UserVo userVo:list) {
System.out.println(userVo);
} } }
MapperTest6.java
二、一对多的查询
通过country表查询用户(用户id = 国家id)

SELECT c.c_id ,c.c_countryname , c.c_capital , u.u_id , u.u_username FROM country c LEFT JOIN USER u ON u.u_id = c.c_id
Gary.sql
CountryVo.class中包含User对象集合
package com.Gary.bean;
import java.util.List;
public class CountryVo extends Country{
//需要维护一个User集合
private List<User> userList;
public List<User> getUserList() {
return userList;
}
public void setUserList(List<User> userList) {
this.userList = userList;
}
@Override
public String toString() {
return super.toString() + "userList = " + userList;
}
}
CountryVo.java
查询国家中存在的用户存在的SQL语句
<!-- 查询所有CountryVo -->
<resultMap type="CountryVo" id="countryVo">
<id property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
<result property="c_capital" column="c_capital"/>
<!-- 一对多关系 -->
<collection property = "userList" ofType="User">
<id property="u_id" column="u_id"/>
<result property="u_username" column="u_username"/>
</collection>
</resultMap>
<select id="selectAllCountryVo" resultMap="countryVo">
SELECT c.c_id ,c.c_countryname , c.c_capital , u.u_id , u.u_username FROM country c LEFT JOIN USER u ON u.u_id = c.c_id
</select>
<?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="com.Gary.mapper.CountryMapper"> <resultMap type="Country" id="country">
<result property="id" column="c_id"/>
</resultMap> <!-- 查询所有 -->
<select id="selectAll" resultMap="country">
select * from country
</select> <!-- 查询所有CountryVo -->
<resultMap type="CountryVo" id="countryVo">
<id property="id" column="c_id"/>
<result property="c_countryname" column="c_countryname"/>
<result property="c_capital" column="c_capital"/>
<!-- 一对多关系 -->
<collection property = "userList" ofType="User">
<id property="u_id" column="u_id"/>
<result property="u_username" column="u_username"/>
</collection>
</resultMap>
<select id="selectAllCountryVo" resultMap="countryVo">
SELECT c.c_id ,c.c_countryname , c.c_capital , u.u_id , u.u_username FROM country c LEFT JOIN USER u ON u.u_id = c.c_id
</select> </mapper>
CountryMapper.xml

package com.Gary.test; import java.io.IOException;
import java.io.InputStream;
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 com.Gary.bean.Country;
import com.Gary.bean.CountryVo;
import com.Gary.bean.User;
import com.Gary.bean.UserVo;
import com.Gary.mapper.CountryMapper;
import com.Gary.mapper.UserMapper; public class MapperTest7 { @Test
public void Test7() throws IOException {
//读取配置文件
String resource = "sqlMapConfig.xml"; InputStream in = Resources.getResourceAsStream(resource); //创建sqlSessionFactory
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in); //生产一个sqlSession
SqlSession session = ssf.openSession(); CountryMapper mapper = session.getMapper(CountryMapper.class);
List<CountryVo> list = mapper.selectAllCountryVo(); for(CountryVo countryVo : list){
System.out.println(countryVo);
} } }
MapperTest7.java
JavaWeb_(Mybatis框架)关联查询_六的更多相关文章
- JavaWeb_(Mybatis框架)输入和输出参数_五
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)主配置文件介绍_四
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)Mapper动态代理开发_三
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- JavaWeb_(Mybatis框架)动态sql_七
系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...
- mybatis_09关联查询_一对一
复杂查询时,单表对应的po类已不能满足输出结果集的映射. 所以有些时候就需要关联查询_一对一:通过条件查询结果每个字段都唯一 一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合 方 ...
- Mybatis之关联查询
一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...
- SpringBoot+Mybatis实现关联查询
SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...
随机推荐
- 微信小微商户申请入驻 .NET C#实现微信小微商户进件API
微信小微商户申请入驻 .NET C#实现微信小微商户进件API官方小微商户专属接口文档 微信支付SDK 微信支付官方SDK与DEMO下载 图片上传 图片上传接口API文档 证书下载 证书下载接口API ...
- 使用element-ui中的el-upload组件时携带其他参数
解决方法:// template <el-upload action="/api/oss/file/add" :headers="headers" // ...
- FlowPortal BPM多汇报线的设置及使用
1.在组织结构中设置多汇报线 2.流程中使用汇报线 3.流程节点上使用汇报线 流程节点默认启用流程中指定的汇报线,若流程中的某个节点需要启用特殊的汇报线,可通过设置节点业务属性实现.
- iOS常用宏定义大全
宏定义与常量的区别 宏:只是在预处理器里进行文本替换,不做任何类型检查,宏能定义代码,const不能,多个宏编译时间相对较长,影响开发效率,调试过慢,const只会编译一次,缩短编译时间. 所以在使用 ...
- python 将GIF拆分成图片方法
import os from PIL import Image, ImageSequence def parseGIF(gifname): # 将gif解析为图片 # 读取GIF im = Image ...
- Ubuntu系统---终端下用g++进行c++项目
Ubuntu系统---终端下用g++进行c++项目 目录 一.编译工具(g++/gcc)和编辑工具(vim/gedit)二.C语言 的编译与运行三.C++语言 的编译与运行四.gcc/g++的详细过程 ...
- Anton and Chess(模拟+思维)
http://codeforces.com/group/1EzrFFyOc0/contest/734/problem/D 题意:就是给你一个很大的棋盘,给你一个白棋的位置还有n个黑棋的位置,问你黑棋能 ...
- 15、TDM模型
论文:https://arxiv.org/pdf/1801.02294.pdf 解析: 学习基于树的推荐系统深度模型 Learning Tree-based DeepModel for Recomme ...
- zznu-oj-2117 : 我已经在路上了(求函数的原函数的字符串)--【暴力模拟题,花式模拟题,String大法好】
2117 : 我已经在路上了 时间限制:1 Sec 内存限制:256 MiB提交:39 答案正确:8 提交 状态 编辑 讨论区 题目描述 spring是不折不扣的学霸,那可是机房考研中的头号选手,不吹 ...
- 图片转换成十六进制TXT文件
最近学习了图片的转换,就学习了一下图片从二进制转换成十六进制,十六进制TXT文件转换成图片形式. using System; using System.Collections.Generic; usi ...