Mybatis 框架(三)—— 映射文件的配置(单表查询映射、一对一映射、一对多映射)、Mybatis Generator 插件进行反向工程

一、映射文件的配置(2)

1、手动的结果集映射_单表查询映射

(1)单表查询映射:
在使用 mybatis 时,我们推荐实体类的属性名称和表中的字段名名称保持一致,采用这样的做法 mybatis 可以自动将结果集中的字段值映射到实体类的属性中,如果实体类的属性名称与数据库表的字段名称不一致时,我们就需要使用 resultMap 标签来实现手动的结果集映射。
例如:实体类的属性名称为 name,price 但是数据库表的字段名称是 p_name 和 p_price,这时我们就需要在映射文件中自定义映射关系。

<?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.mybatis.demo1.dao.OneToOneMapper">
    <!-- sql标签定义可重用的sql语句 -->
   <sql id="columns">p_id,p_name,p_price,p_count,p_class,p_attribute</sql>

    <!-- 单表查询映射 -->
    <resultMap type="Product" id="p_map">
        <id property="id" column="p_id"/>
        <result property="name" column="p_name"/>
        <result property="price" column="p_price"/>
        <result property="count" column="p_count"/>
        <result property="classes" column="p_class"/>
        <result property="attribute" column="p_attribute"/>
    </resultMap>
</mapper>

属性说明:

  • type:结果集映射的实体类型
  • id:映射关系的id,在查询时会使用该id完成结果集映射
  • column:列名
  • property:属性名

定义好映射关系后,在查询时,就可以使用 resultMap 属性来引用手动映射关系

<select id="select" resultMap="p_map">
	select <include refid="columns"></include> from products
</select>

2、手动的结果集映射_一对一关联查询

一对一的关联查询有两种方式,第一种是直接将多表连接查询的结果封装为键值对,要实现这个目的只需要将 resultType 定义为 map 类型即可。
第二种方式是用实体类对象来进行封装,以商品表和商品类型为例:
(1)Products 表结构:
在这里插入图片描述
(2)l_type 表结构:
在这里插入图片描述
(3)Products 实体类:

public class Products {
	private int p_id;
	private String p_name;
	private double p_price;
	private int p_count;
	private String p_class;
	private String p_attribute;
	private int p_typeid;
	private Ltype ltype;
	//省略get、set、toString方法
}

(4)Products 类中定义了一个 Ltype 的对象专门用于保存商品类型信息

public class Ltype {
	private int id;
	private String name;
	private int pid;
	//省略get、set、toString方法
}

映射文件:

<!-- 一对一映射关系 -->
<resultMap type="Product" id="p1_map">
	<id property="id" column="p_id"/>
	<result property="name" column="p_name"/>
	<result property="price" column="p_price"/>
	<result property="count" column="p_count"/>
	<result property="classes" column="p_class"/>
	<result property="attribute" column="p_attribute"/>
	<!--将查询结果封装为一个对象-->
	<association property="ltype" javaType="Ltype">
		<id property="id" column="lt_id" />
		<result property="name" column="lt_name" />
		<result property="pid" column="lt_pid" />
	</association>
</resultMap>
<select id="selectOneToOne" resultMap="p1_map">
	select p.*,t.* from Products as p inner join ltype as t on p.p_typeid = t.lt_id
</select>

首先查询时一定是使用多表联合查询,同时查询出商品详情和商品类型信息,最后在通过自定义的结果集映射来进行结果集的封装。
重要标签:

  • association:用于对实体类的关联对象进行映射
  • property:表示对象名称
  • javaType:用于说明关联的对象类型

3、手动的结果集映射_一对多关联映射查询

以商品表和商品类型表为例,一种类型对应多个商品,想要在查询商品类型的同时查询出对应类型的商品的详细信息,在定义实体类时就需要在商品类型实体类中定义一个商品信息集合,用来保存单个商品信息。
(1)商品类型实体类:

public class Ltype {
	private int id;
	private String name;
	private int pid;
	private List<Product> product;
	//省略get、set方法
	@Override
	public String toString() {
		return "\nLtype {id:" + id + ", name:" + name + ", pid:" + pid
				+ ", product:" + product + "}";
	}
}

(2)商品信息类

public class Product {
	private int id;
	private String name;
	private double price;
	private int count;
	private String classes;
	private String attribute;
	private Ltype ltype;
	//省略get、set方法
	@Override
	public String toString() {
		return "{id:" + id + ", name:" + name + ", price:" + price
				+ ", count:" + count + ", classes:" + classes + ", attribute:"
				+ attribute + "}";
	}
}

(3)映射文件:

<!-- 一对多映射关系 -->
<resultMap type="Ltype" id="p_map">
    <id property="id" column="lt_id" />
    <result property="name" column="lt_name" />
    <!--一对多映射-->
    <collection property="product" ofType="Product">
        <id property="id" column="p_id"/>
        <result property="name" column="p_name"/>
        <result property="price" column="p_price"/>
        <result property="count" column="p_count"/>
        <result property="classes" column="p_class"/>
        <result property="attribute" column="p_attribute"/>
    </collection>
</resultMap>
<select id="selectOneToMore" resultMap="p_map">
    select t.lt_id,t.lt_name,p.* from Products as p right join ltype as t on p.p_typeid = t.lt_id
</select>

和一对一关联查询一样首先要用多表联合查询查询出数据,区别是:

  • 一对一是一条主表数据 + 一条从表数据 = 一条数据
  • 一对多是一条主表数据 + 多条从表数据 = 多条数据

但是我们最终的目的是想将一的数据保存到商品类型 Ltype 对象中,而 商品信息 Product 数据保存到 Orderinfo 对象的集合中,使用 collection 标签来映射一对多结果集时,mybatis 会判断如果一条数据的主表段已经被封装为对象,那么 mybatis 就不会在封装新对象了,而是将从表段数据封装到集合中。

二、Mybatis Generator 插件进行反向工程

1、安装插件

在这里插入图片描述

2、编写xml文件进行反向工程

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
  PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
  "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
	<!--指定jdbc驱动包在硬盘的地址 -->
	<classPathEntry location="E://work.software//mysql-connector-java-5.1.44-bin.jar" />
	<context id="DB2Tables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释  -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--插件需要读取数据库的信息 进行反向工程 需要在此处提供链接数据库的信息 -->
		<jdbcConnection 
            driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/store" 
			userId="root"
			password="admin">
		</jdbcConnection>

		<javaTypeResolver>
			<!--禁止将数据库中的整数类型 在java实体类中定义为BigDecimal类型 直接定义为Integer类型 -->
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>

		<!--生成实体类的配置 -->
		<javaModelGenerator targetPackage="com.mybatis.entity" targetProject="Mybatis_Demo1">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		<!--生成映射文件的配置 -->
		<sqlMapGenerator targetPackage="com.mybatis.mapper" targetProject="Mybatis_Demo1">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>
		<!--生成数据层接口的配置 -->
		<javaClientGenerator type="XMLMAPPER" targetPackage="com.mybatis.mapper" targetProject="Mybatis_Demo1">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>
		<!--那些表需要进行反向工程 -->
		<table tableName="products" domainObjectName="Products"></table>
		<table tableName="ltype" domainObjectName="Ltype"></table>
		<table tableName="btype" domainObjectName="Btype"></table>
	</context>
</generatorConfiguration>

3、运行xml文件,生成实体类、数据层接口、映射文件

在这里插入图片描述

发布了75 篇原创文章 · 获赞 10 · 访问量 2864

猜你喜欢

转载自blog.csdn.net/baidu_27414099/article/details/104440492