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三剑客之compose

    简介 Compose 项目是 Docker 官方的开源项目,负责实现对 Docker 容器集群的快速编排.从功能上看,跟 OpenStack 中的 Heat 十分类似.其代码目前在 https://g ...

  2. 深入了解 Java 中的异常处理 + 面试题

    # 深入了解 Java 中的异常处理 + 面试题 在程序开发中,异常处理也是我们经常使用到的模块,只是平常很少去深究异常模块的一些知识点.比如,try-catch 处理要遵循的原则是什么,finall ...

  3. 那些 JavaScript 自带的奇妙 Bug

    米娜桑,哦哈哟~ 本章讲解关于 JavaScript 奇妙的 Bug,与其说是Bug,不如说是语言本身隐藏的奥秘.接下来就看看可能会影响到我们编程的那些Bug吧. typeof null === &q ...

  4. PE可执行文件加载器

    PE文件加载器 模仿操作系统,加载pe文件到内存中 该项目主要是为了检测pe的学习程度,是否都完全理解了.当然没有完全理解 实现功能的如下: 模仿操作系统,加载pe文件到内存中,然后执行待执行的pe文 ...

  5. javaIO编码详解

    原创 输出流 有哪些构造方法可以在参数上设置编码格式 PrintStream(File file, String csn) PrintStream(String fileName, String cs ...

  6. tmobst4

    (单选题)HTML代码: <table> <tr><td>Value 1</td><td></td></tr> &l ...

  7. python环境安装及配置

    一.下载python,可选择python2.x或python 3.0 下载地址:[官网],选择系统 ---选择对应版本 注意自己电脑是32位(X86)还是64位(x86-64) 下载文件包,点击点击安 ...

  8. python列表与元祖

    python 的列表和元素: 共同点:有序的 区别:1.列表可以修改增加删除列表内容,元组不能修改 联系:元组中包含列表的元素,可以修改列表元素. 分析:1.列表:LIST1=[1,2,3,4,5,6 ...

  9. 关于求最长子串,使得最大减最小小于k的问题-以POJ4003为例

    问题 给出一个长度为\(n\)的序列\(a[i]\),有\(m\)次询问, 每次给你一个\(k\),让你求一个最长子串\([l,r]\),使得\(max_l^r\{a_i\}-min_l^r\{a_i ...

  10. spring中获取bean的方式

    获取bean的方式 1.可以通过上下文的getBean方法 2.可以通过@Autowired注入 定义controller @RestController @RequestMapping(" ...