2.映射器Mapper

相当于dao层,不用写实现类(mybatis底层会采用动态代理模式 会跟我生成实现)

步骤:

(1) 创建项目 配置mybatis-config.xml 和昨天一样

(2) 创建接口 ProductMapper (XXXXMapper) 里面定义一个方法findAll -->以前dao层/mapper层

(3) 在对应的ProductMapper.xml namespace 和

标签里面id (namespace+id) == (ProductMapper包路径+方法)

<select id="findAll"  resultType="product">

select * from product

</select>

3.高级查询

3.1 like查询

方式一(存在sql注入的问题):

<if test="productName != null">

and productName like '%${productName}%'

</if>

方式二:

<if test="productName != null">

and productName like concat('%',#{productName},'%')

</if>

3.2特殊符号转译

方式一:

Eq: <if test="minPrice != null and  maxPrice != null">

and salePrice > #{minPrice} and salePrice < #{maxPrice}

</if>

方式二(CDATA):

<![CDATA[ ]]>

Eq:<if test="minPrice != null and  maxPrice != null">

<![CDATA[

and salePrice > #{minPrice} and salePrice <= #{maxPrice}

]]>

</if>

4.结果映射

处理表字段和domain的对象字段不一致的情况

4.1 使用别名

<select id="findAll"  resultType="product">

select id,productName pName,salePrice,costPrice,cutoff from product

</select>

4.2 返回Map解决

<select id="findAll"  resultMap="productMap">

select id,productName,salePrice,costPrice,cutoff from product

</select>

<resultMap id="productMap" type="product">

<!-- id:主键

property:类里面的属性

column:查询列名称

-->

<id property="id" column="id"></id>

<result property="pName" column="productName"></result>

<result property="salePrice" column="salePrice"></result>

</resultMap>

5.关系处理

5.1 对一方处理

嵌套结果: 只发送一条sql

嵌套查询: 发送 1+n条sql

<?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="cn.itsource._03_manytoone.mapper.ProductMapper">

<!-- <resultMap id="productMap" type="product">

<id column="id" property="id"></id>

<result column="productName" property="pName"></result>

<result column="salePrice" property="salePrice"></result>

<result column="costPrice" property="costPrice"></result>

<result column="cutoff" property="cutoff"></result>

<!– 处理一方–>

<association property="dir" javaType="productDir">

<id column="did" property="id"></id>

<result column="dname" property="dirName"></result>

</association>

</resultMap>-->

<!-- (1)嵌套结果 发送一条sql语句-->

<!-- <select id="findAll"  resultMap="productMap">

SELECT p.id, p.productName, p.salePrice, p.costPrice, p.cutoff, dir.id did, dir.dirName  dname

FROM

product p

JOIN productDir dir ON p.dir_id = dir.id

</select>-->

<!-- 嵌套查询:发送多sql语句(1+n条sql)-->

<select id="findAll"  resultMap="productMap">

SELECT

p.id,p.productName,p.salePrice, p.costPrice,p.cutoff,p.dir_id

FROM product p

</select>

<resultMap id="productMap" type="product">

<id column="id" property="id"></id>

<result column="productName" property="pName"></result>

<result column="salePrice" property="salePrice"></result>

<result column="costPrice" property="costPrice"></result>

<result column="cutoff" property="cutoff"></result>

<!--根据上面查询dir_id 在去查询 分类对象-->

<association property="dir" column="dir_id" javaType="productDir" select="selectDir">

</association>

</resultMap>

<select id="selectDir" parameterType="long" resultType="ProductDir">

select * from productDir where id = #{dir_id}

</select>

</mapper>

5.2 对多方进行处理

嵌套结果和嵌套查询

<?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="cn.itsource._04_onetomany.mapper.ProductDirMapper">

<!-- 嵌套结果-->

<!-- <select id="findAll" resultMap="productDirMap">

select dir.id ,dir.dirName,

p.id pid,p.productName pName,p.salePrice,p.costPrice,p.cutoff

from productDir dir join product p

on dir.id = p.dir_id

</select>

<resultMap id="productDirMap" type="productDir">

<id property="id" column="id"></id>

<result property="dirName" column="dirName"></result>

<collection property="products" ofType="product">

<id property="id" column="pid"></id>

<result property="pName" column="pName"></result>

<result property="salePrice" column="salePrice"></result>

<result property="costPrice" column="costPrice"></result>

</collection>

</resultMap>-->

<!-- 嵌套查询-->

<select id="findAll" resultMap="productDirMap">

select dir.id ,dir.dirName from productDir dir

</select>

<resultMap id="productDirMap" type="productDir">

<id property="id" column="id"></id>

<result property="dirName" column="dirName"></result>

<collection property="products" column="id" ofType="product" select="selectProducts">

</collection>

</resultMap>

<select id="selectProducts" parameterType="long" resultType="product">

select id,productName pName,salePrice,costPrice,cutoff

from product where dir_id = #{dir_id}

</select>

</mapper>

6.SSM整合

6.1 整合步骤

(1)创建项目--web项目 (maven/普通web)

(2)导入三个框架的jar包

(3)配置文件

applicationContext.xml --配置spring+mybatis

<!--sqlSessionFactory -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<property name="dataSource" ref="dataSource"></property>

<property name="mapperLocations" value="classpath:cn/itsource/ssm/mapper/*Mapper.xml"></property>

<property name="typeAliasesPackage">

<value>

cn.itsource.ssm.domain

</value>

</property>

</bean>

<!-- 把产生mapper 交给 spring-->

<bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="cn.itsource.ssm.mapper"></property>

</bean>

mybatis_day02的更多相关文章

  1. Spring 和 MyBatis 环境整合

    本案例主要是讲述Spring  和  MyBatis 的环境整合 , 对页面功能的实现并没有做的很完整 先附上本案例的结构 1 . 创建项目并导入相关jar包 commons-collections4 ...

随机推荐

  1. POI导入导出excel(附工具类)

    关于POI导出excel的功能我在前面的文章已经写过了,POI导出excel的三种方式 , 导出表格数据到excel并下载(HSSFWorkbook版) ,本篇文章主要是将导入导出功能进一步地封装,在 ...

  2. Maven的scope属性作用域范围

    在POM 4中,<dependency>中还引入了<scope>,它主要管理依赖的部署.目前<scope>可以使用5个值: 1. compile,缺省值,适用于所有 ...

  3. vscode 调试 react 项目

    主要分为以下三个步骤 安装 debug for chrome 配置 launch.json 文件 配置内容如下 { "version": "0.2.0", &q ...

  4. ORACLE ITL事务槽

    讲到ITL(事务槽)必定先说数据块,那么什么是数据块呢?先给大家上一个数据块结构图 数据块分别由块头.ITL(事务槽).表信息区.行信息区.块空闲区.行数据区组成,其中ITL用来记录在数据块发生的所有 ...

  5. 二、UDP

    1 端口号 在计算机网络和电磁信号理论中,对共享同一通信信道的多个信号进行区分是个常见的问题.多路复用(multiplexing)就是允许多个会话共享同一介质或机制的一种解决方案. 使用不同的频率来区 ...

  6. JavaScript 与 Java 有什么不同?

    JavaScript 编程语言是由 Netscape,Inc. 开发的,它并不是 Java 平台的一部分. JavaScript 不会创建小应用程序或独立应用程序.在最常见的形式中,JavaScrip ...

  7. 如何在windows10家庭版上搭建docker

    如何在windows10家庭版上搭建docker 建议先跳到最后一条,看完之后再决定是否安装. 0X00搭建原因 首先搭docker的想法是我在复现漏洞时候,发现大佬们的复现环境都是基于docker的 ...

  8. MongoDB 4.2新特性:分布式事务、字段级加密、通配符索引、物化视图

    MongoDB 4.2已经发布,我们来看看它增加了哪些新特性?分布式事务?数据库加密?通配符索引? 在2019年MongoDB World大会上,CTO Eliot Horowitz介绍了MongoD ...

  9. Source Code Structure - Python 源码目录结构

    Source Code Structure - Python 源码目录结构 Include 目录包含了 Python 提供的所有头文件, 如果用户需要用 C 或 C++ 编写自定义模块扩展 Pytho ...

  10. Pyinstaller 打包程序为可执行文件exe

    Pyiinstaller打包 pyinstaller是python的一个第三方模块,使用它可以将pythnon程序打包为可执行文件,实现打包后的程序在没有python环境的机器上也可以运行.pyins ...