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. K8S部署遇到的问题处理汇总

    第一个: node节点注册提示:failed to get config map: Unauthorized 代码如下: [root@node1 ~]# kubeadm join --token ll ...

  2. python安装BeautifulSoup4

    今天学习使用python爬虫,书上说让安装一个BeautifulSoup的插件,网上找了,说是下载压缩文件,直接解压然后,通过两行代码就实现了,我下载了两个,还是无法使用,后来就使用在在Pycharm ...

  3. https原理总结

    博客搬家: https原理总结 最近在公司项目的服务器上做一些内部接口,要求使用https,于是花时间研究了一波.我们熟知的http在传输时未对数据进行加密,在传输一些敏感信息时存在着不小的安全隐患. ...

  4. [terminal]终端仿真程序

    char * szCommAry[COMM_NUM]={ //屏幕属性命令,23 "\x1b[12h",//禁止本端回显,键盘数据仅送给主机 "\x1b[12l" ...

  5. Codeforces_821

    A.直接判断每一个数. #include<bits/stdc++.h> using namespace std; ][]; int main() { ios::sync_with_stdi ...

  6. HDU_5045_状态压缩dp

    http://acm.hdu.edu.cn/showproblem.php?pid=5045 i从1到m依次更新,dp[i][j]表示更新到i题时,j表示每个人的答题状态,分别用0和1表示(因为每个人 ...

  7. windows系统快速安装pytorch的详细教程

    pip和conda的区别 之前一直使用conda和pip ,有时候经常会两者混用.但是今天才发现二者装的东西不是在一个地方的,所以发现有的东西自己装了,但是在运行环境的时候发现包老是识别不了,一直都特 ...

  8. learn about sqlserver partitition and partition table 1

    Dear all, Let get into business, the partitions on sql server is very different with that on oracle. ...

  9. CAS 分析

    CAS是什么 (1) CAS(Compare and Swap) 比较并交换, 比较并交换是在多线程并发时用到的一种技术 (2) CAS是原子操作, 保证并发安全性, 而不是保证并发同步. (3) C ...

  10. OsgEearh 中的 FeatureEditor的实现原理

    先来看看FeatureEditor的用法: const osgEarth::SpatialReference* mapSRS = mapNode->getMapSRS(); osgEarth:: ...