MyBatis入门介绍(二)

简介: 根据用户名查询用户信息,查询条件放到QueryVo的user属性中,入参传递user对象

1、根据用户名查询用户信息,查询条件放到QueryVo的user属性中,入参传递user对象

<!-- 使用包装类型查询用户 
使用ognl从对象中取属性值,如果是包装对象可以使用.操作符来取内容部的属性
-->
<select id="findUserByQueryVo" parameterType="queryvo" resultType="user">
  SELECT * FROM user where username like '%${user.username}%'
</select>

2、获取用户列表总数

<select id="findUserCount" resultType="int">
  select count(1) from user
</select>

resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功

如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中

resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询

定义resultMap

由于上边的mapper.xml中sql查询列和Users.java类属性不一致,需要定义resultMap:userListResultMap将sql查询列和Users.java类属性对应起来

<id />:此属性表示查询结果集的唯一标识,非常重要。如果是多个字段为复合唯一约束则定义多个<id />。

Property:表示User类的属性。

Column:表示sql查询出来的字段名。

Column和property放在一块儿表示将sql查询出来的字段映射到指定的pojo类属性上。

<result />:普通结果,即pojo的属性。


3、动态sql

3.1、if语句

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  where 1=1 
  <if test="id!=null">
  and id=#{id}
  </if>
  <if test="username!=null and username!=''">
  and username like '%${username}%'
  </if>
</select>

3.2、Where语句

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  <where>
  <if test="id!=null and id!=''">
  and id=#{id}
  </if>
  <if test="username!=null and username!=''">
  and username like '%${username}%'
  </if>
  </where>
</select>

where语句可以自动处理第一个and


3.3、foreach语句

向sql传递数组或List,mybatis使用foreach解析,如下:

传入多个id查询用户信息,用下边两个sql实现:

SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%张%'  id IN (10,89,16)

在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法

<if test="ids!=null and ids.size>0">
    <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >
      #{id}
    </foreach>
</if>

4、Sql重复片段

Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:

<select id="findUserList" parameterType="user" resultType="user">
  select * from user 
  <where>
  <include refid="query_user_where"/>
  </where>
</select>
<sql id="query_user_where">
  <if test="id!=null and id!=''">
  and id=#{id}
  </if>
  <if test="username!=null and username!=''">
  and username like '%${username}%'
  </if>
</sql>

注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:

<include refid="namespace.sql片段”/>

5、查询所有订单信息,关联查询下单用户信息

使用resultType,定义订单信息po类,此po类中包括了订单信息和用户信息:

public class OrdersCustom extends Orders {
  private String username;// 用户名称
  private String address;// 用户地址
get/set。。。。
}

OrdersCustom类继承Orders类后OrdersCustom类包括了Orders类的所有字段,只需要定义用户的信息字段即可

<!-- 查询所有订单信息 -->
<select id="findOrdersList" resultType="cn.itcast.mybatis.po.OrdersCustom">
SELECT
orders.*,
user.username,
user.address
FROM
orders, user
WHERE orders.user_id = user.id 
</select>

小结:定义专门的po类作为输出类型,其中定义了sql查询结果集所有的字段。此方法较为简单,企业中使用普遍


另外一种方法:使用resultMap,定义专门的resultMap用于映射一对一查询结果。

在Orders类中加入User属性,user属性中用于存储关联查询的用户信息,因为订单关联查询用户是一对一关系,所以这里使用单个User对象存储关联查询的用户信息。

xml文件:

<!-- 查询订单关联用户信息使用resultmap -->
<resultMap type="cn.itheima.po.Orders" id="orderUserResultMap">
  <id column="id" property="id"/>
  <result column="user_id" property="userId"/>
  <result column="number" property="number"/>
  <result column="createtime" property="createtime"/>
  <result column="note" property="note"/>
  <!-- 一对一关联映射 -->
  <!-- 
  property:Orders对象的user属性
  javaType:user属性对应 的类型
  -->
  <association property="user" javaType="cn.itcast.po.User">
  <!-- column:user表的主键对应的列  property:user对象中id属性-->
  <id column="user_id" property="id"/>
  <result column="username" property="username"/>
  <result column="address" property="address"/>
  </association>
</resultMap>
<select id="findOrdersWithUserResultMap" resultMap="orderUserResultMap">
  SELECT
  o.id,
  o.user_id,
  o.number,
  o.createtime,
  o.note,
  u.username,
  u.address
  FROM
  orders o
  JOIN `user` u ON u.id = o.user_id
</select>

这里resultMap指定orderUserResultMap。

association:表示进行关联查询单条记录

property:表示关联查询的结果存储在cn.itcast.mybatis.po.Orders的user属性中

javaType:表示关联查询的结果类型

<id property="id" column="user_id"/>:查询结果的user_id列对应关联对象的id属性,这里是<id />表示user_id是关联查询对象的唯一标识。

<result property="username" column="username"/>:查询结果的username列对应关联对象的username属性


6、一对多查询

查询所有用户信息及用户关联的订单信息

用户信息和订单信息为一对多关系,使用resultMap实现如下:

在User类中加入List orders属性

xml文件:

<resultMap type="cn.itheima.po.user" id="userOrderResultMap">
  <!-- 用户信息映射 -->
  <id property="id" column="id"/>
  <result property="username" column="username"/>
  <result property="birthday" column="birthday"/>
  <result property="sex" column="sex"/>
  <result property="address" column="address"/>
  <!-- 一对多关联映射 -->
  <collection property="orders" ofType="cn.itheima.po.Orders">
  <id property="id" column="oid"/>  
       <!--用户id已经在user对象中存在,此处可以不设置-->
  <!-- <result property="userId" column="id"/> -->
  <result property="number" column="number"/>
  <result property="createtime" column="createtime"/>
  <result property="note" column="note"/>
  </collection>
</resultMap>
<select id="getUserOrderList" resultMap="userOrderResultMap">
  SELECT
  u.*, o.id oid,
  o.number,
  o.createtime,
  o.note
  FROM
  `user` u
  LEFT JOIN orders o ON u.id = o.user_id
</select>

collection部分定义了用户关联的订单信息。表示关联查询结果集

property=“orders”:关联查询的结果集存储在User对象的上哪个属性。

ofType=“orders”:指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。

<id />及<result/>的意义同一对一查询


目录
相关文章
|
4月前
|
关系型数据库 Java 数据库连接
MyBatis-Plus简介和入门操作
【1月更文挑战第5天】 一、MyBatis-Plus简介 二、 MyBatis-Plus操作 1、准备数据库脚本 2、准备boot工程 3、导入依赖 4、配置文件和启动类 5、功能编码 6、测试和使用
107 1
|
4月前
|
SQL Java 数据库连接
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
94 0
JAVAEE框架技术之7-myBatis ORM框架入门基础CRUD
|
18天前
|
SQL Java 数据库连接
Javaweb之Mybatis入门程序的详细解析
Javaweb之Mybatis入门程序的详细解析
18 0
|
18天前
|
SQL Java 数据库连接
Javaweb之Mybatis入门的详细解析
Javaweb之Mybatis入门的详细解析
12 0
|
2月前
|
SQL JavaScript Java
mybatis-flex入门体验(一)
`shigen`,一个专注于Java、Python、Vue和Shell的博主,分享成长和认知。近期探索了`mybatis-flex`,通过官网学习了代码生成和编码体验。配置数据源和依赖后,利用示例代码生成了符合Lombok+MyBatis Plus规范的实体和Mapper。此外,展示了如何配置SQL打印,并用测试代码演示了查询、多条件查询和更新操作。`mybatis-flex`的亮点在于流畅的查询语法和连表查询功能。后续将分享更多关于连表查询的实践。一起学习,每天进步!
47 0
mybatis-flex入门体验(一)
|
2月前
|
Java 数据库连接 mybatis
|
3月前
|
XML Java 数据库连接
MyBatis入门配置
【2月更文挑战第9天】
MyBatis入门配置
|
4月前
|
Java 数据库连接 API
MyBatis入门操作
MyBatis入门操作
16 0
|
4月前
|
Java 数据库连接 测试技术
【MyBatis】操作数据库——入门
【MyBatis】操作数据库——入门
|
4月前
|
SQL Java 关系型数据库
一文彻底搞懂Mybatis系列(一)之mybatis入门
一文彻底搞懂Mybatis系列(一)之mybatis入门