系列博文:  

    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框架)关联查询_六的更多相关文章

  1. JavaWeb_(Mybatis框架)输入和输出参数_五

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  2. JavaWeb_(Mybatis框架)主配置文件介绍_四

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  3. JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  4. JavaWeb_(Mybatis框架)使用Mybatis对表进行增、删、改、查操作_二

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  5. JavaWeb_(Mybatis框架)Mapper动态代理开发_三

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  6. JavaWeb_(Mybatis框架)动态sql_七

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  7. mybatis_09关联查询_一对一

    复杂查询时,单表对应的po类已不能满足输出结果集的映射. 所以有些时候就需要关联查询_一对一:通过条件查询结果每个字段都唯一 一对一:模型里面有模型 一对多:模型里面有集合 多对多:集合里面有集合 方 ...

  8. Mybatis之关联查询

    一.一对一关联 1.1.提出需求 根据班级id查询班级信息(带老师的信息) 1.2.创建表和数据 创建一张教师表和班级表,这里我们假设一个老师只负责教一个班,那么老师和班级之间的关系就是一种一对一的关 ...

  9. SpringBoot+Mybatis实现关联查询

    SpringBoot+Mybatis实现关联查询 今天学习了下Mybatis的动态查询,然后接着上次的Demo改造了下实现表的关联查询. 话不多说,开始今天的小Demo 首先接着上次的项目 https ...

随机推荐

  1. Asp.Net Core 2.0 之旅---@Html.Action

    原文:Asp.Net Core 2.0 之旅---@Html.Action 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https: ...

  2. Linux增加虚拟内存

    Docker容器启动Mysql镜像报错,提示无法分配内存,报错信息如下: 由此我们看到Swap为0,考虑适当增加swap. Linux开启swap空间有好几种方法,在这里只介绍比较常用的两种. 使用交 ...

  3. 重构与反思-<重构代码的7个阶段>有感

    https://coolshell.cn/articles/5201.html/comment-page-2#comment-1932554 过去半年基本上完整经历了这个文章的各个阶段,看完文章结合自 ...

  4. spingboot启动报驱动Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of th

    原因: springboot应用了最新的驱动com.mysql.cj.jdbc.Driver,这个驱动需要用mysql-connector-java包的6.x版本才可以, 而mysql-connect ...

  5. MySQL数据库的创建&删除&选择

    1.MySQL数据库的创建 方法1和2      方法3.使用PHP脚本 PHP中使用mysqli_query函数来创建或删除MySql数据库 mysqli_query函数:两个参数 返回值:执行成功 ...

  6. 前端理解控制反转ioc

    工作一直都是写前端,而且都是偏业务的.相关的框架代码层面的了解还是有很大的缺失.一直想多写些维护性,可读性强的代码. 这段时间对控制反转ioc,这样的设计有了一个初步的了解. 前端为弱语言,平时代码的 ...

  7. MYSQL查询练习 1

    -- 查询练习 1------------ CREATE TABLE stu ( sid ), sname ), age INT, gender ) ); , 'male'); , 'female') ...

  8. 浅谈JAVA继承关系中的构造函数

    话不多说直接上例子,我的例子中定义了两个类,TheSon和TheFather,TheSon继承了TheFather,如图: TheSon类的定义: ​ TheFather类的定义: 当我们初始化The ...

  9. 网络分类及OSI七层模型

    一.网络分类: 局域网(LAN)是指在某一区域内由多台计算机互联成的计算机组.一般是方圆几千米以内.局域网可以实现文件管理.应用软件共享.打印机共享.工作组内的日程安排.电子邮件和传真通信服务等功能. ...

  10. My97DatePicker(js日期插件) v4.8

    1.下载地址 https://files.cnblogs.com/files/yu-shang/My97DatePicker.zip 2.文档地址 http://my97.net/demo/index ...