mybatis_day02
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的更多相关文章
- Spring 和 MyBatis 环境整合
本案例主要是讲述Spring 和 MyBatis 的环境整合 , 对页面功能的实现并没有做的很完整 先附上本案例的结构 1 . 创建项目并导入相关jar包 commons-collections4 ...
随机推荐
- 关于mysql8启动后又停止(windows10系统),忘记密码以及密码过期等坑解决办法总结!
一 我遇到的问题 1 mysql连接不了,mysql服务启动后又马上关闭 2 忘记密码或者重装服务后提示安装的随机密码过期 一个一个来,先看第一个: 1 出现这个情况很大原因是mysql安装目录有多余 ...
- Codeforces Round #618 (Div. 2)
题库链接 https://codeforces.ml/contest/1300 A. Non-zero 一个数组,每次操作可以给某个数加1,让这个数组的积和和不为0的最小操作数 显然如果有0的话,必须 ...
- C++内存管理笔记(一)
C++内存分配的四个层面 : 四个层面的比较: 内存分配与释放的测试: ); //512 bytes free(p1); complex<int>* p2 = new complex& ...
- js原型和原型链的简单理解
构造函数创建对象: function Person() { } var person = new Person(); person.name = 'Tian'; console.log(person. ...
- 2.OpenStack 网络简介(neutron)
OpenStack 网络简介(neutron) 概述和组件 OpenStack 网络允许您创建和管理网络对象, 如网络.子网和端口, 其他 OpenStack 服务可以使用.插件可以实现, 以适应不同 ...
- python 使用记录
#print输出后不换行 #python2.x 中末尾加逗号,表示不换行,print 'contents', #python3.x 中默认print('contents',end='\n'),想要输出 ...
- 用pyinstaller打包时的图标问题
前言 因为昨天重新研究了下python的打包方法,今天一番准备把之前写的一个pdf合并软件重新整理一下,打包出来. 但在打包的过程中仍然遇到了一些问题,半年前一番做打包的时候也遇到了一些问题,现在来看 ...
- kendo ui - core
通过CDN 引入kendo-ui-core git地址:http://www.telerik.com/kendo-ui<link href="http://kendo.cdn.tele ...
- Go语言实现:【剑指offer】序列化二叉树
该题目来源于牛客网<剑指offer>专题. 请实现两个函数,分别用来序列化和反序列化二叉树. 二叉树的序列化是指:把一棵二叉树按照某种遍历方式的结果以某种格式保存为字符串,从而使得内存中建 ...
- 文件传输协议-FTP
一.FTP概述 FTP(File Transfer Protocol 文件传输协议)C/S结构的应用层协议.由服务端和客户端两个部分共同实现文件传输功能 FTP服务器普遍部署于内网中,具有容易部署.方 ...