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. Docker基础内容之资源限制

    内存限制 --memory:内存限定,格式是数字加单位,单位可以为 B.K.M.G.最小为 4M. --memory-swap:交换分区大小限定 CPU限制 --cpus:表示分配给容器可用的cpu资 ...

  2. JavaScript面向对象:创建对象

    1.初级创建对象 var oCar=new Object; oCar.color='red'; oCar.door=4; oCar.map=3; oCar.showColor=function () ...

  3. java获取本地IP地址集合包括虚拟机的ip

    public static ArrayList<String> getLocalIpAddr() { ArrayList<String> ipList = new ArrayL ...

  4. Leetcode 题目整理 Sqrt && Search Insert Position

    Sqrt(x) Implement int sqrt(int x). Compute and return the square root of x. 注:这里的输入输出都是整数说明不会出现 sqrt ...

  5. ogg trail文件序列号不一致

    一.Cause 在某些情况下,对于一个已经running的OGG进程,对已同步的数据(正确的同步或者错误的同步)做修改,修改完之后,需要保持一个一致点,从一致点继续同步. 这时需要人工干涉产生一个新的 ...

  6. 线程池之 ThreadPoolExecutor

    线程池之 ThreadPoolExecutor + 面试题 线程池介绍 线程池(Thread Pool):把一个或多个线程通过统一的方式进行调度和重复使用的技术,避免了因为线程过多而带来使用上的开销. ...

  7. linux系统中的硬链接和软链接

    首先我们需要了解linux下硬链接以及软连接的基本概念.硬链接:新建的文件是已经存在的文件的一个别名,当原文件删除时,新建的文件仍然可以使用.软链接:也称为符号链接,新建的文件以“路径”的形式来表示另 ...

  8. 微信小程序框架分析小练手(三)——仿香哈菜谱小程序制作

    香哈菜谱是一款围绕美食而成的小程序,在这里可以查看各式各样的菜谱. 一.打开微信开发者工具,新建一个项目:xhcp.如下图: 二.建立如下的一些目录: 三.将底部标签导航图标.美食轮播图片.宫格导航图 ...

  9. CSS selector? [class*=“span”]怎么理解

    我在Twitter 中有看到如下selector: .show-grid [class*="span"] { background-color: #eee; text-align: ...

  10. Spring 事件:Application Event

    Spring Application Event Spring 的事件(Application Event)为 Bean 与 Bean 之间的消息通信提供了支持.当一个 Bean 处理完一个任务之后, ...