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 ...
随机推荐
- 深入了解 Java 中的异常处理 + 面试题
# 深入了解 Java 中的异常处理 + 面试题 在程序开发中,异常处理也是我们经常使用到的模块,只是平常很少去深究异常模块的一些知识点.比如,try-catch 处理要遵循的原则是什么,finall ...
- Oracle 11g 单实例静默安装实战记录(linux)
oracle 11g 单实例静默安装 AUTHOR:Oracle_Ran 环境规划: OS Version : Red Hat Enterprise Linux Server release 6.7 ...
- python中的变量和字符串
一.变量 1.python变量 *变量用于存储某个或某些特定的值,它与一个特定标识符相关联,该标识符称为变量名称.变量名指向存储在内存中的值.在创建变量时会在内存中开辟一个空间.基于变量的数据类型,解 ...
- 遇到的一些在ie下的兼容问题和解决方案(ie10+)
1,ie 10下实现水平垂直居中,不固定高度的话,正常的top:50%,left:50%,transform(translate(-50%,-50%)) 是不能实现的,ie下top:50%会失去效果. ...
- 练习2-14 求奇数分之一序列前N项和 (15 分)
练习2-14 求奇数分之一序列前N项和 (15 分) 本题要求编写程序,计算序列 1 + 1/3 + 1/5 + ... 的前N项之和. 输入格式: 输入在一行中给出一个正整数N. 输出格式: 在一行 ...
- Flink 1.10 正式发布!——与Blink集成完成,集成Hive,K8S
Apache Flink社区宣布Flink 1.10.0正式发布! 本次Release版本修复1.2K个问题,对Flink作业的整体性能和稳定性做了重大改进,同时增加了对K8S,Python的支持. ...
- QT5如何设置QLabel中字体的颜色
修改了wd的文章: 如何使用Qt5,设置QLabel中字体的颜色. 大致有几种做法: 一是使用setPalette()方法: 二是使用样式表: 三是可以使用QStyle: 四是可以在其中使用一些简单的 ...
- 你都这么拼了,面试官TM怎么还是无动于衷
面试,对于每个人而然并不陌生,可以说是必须经历的一个过程了,小到一场考试,大到企业面试,甚至大型选秀...... 有时自己明明很努力了,但偏偏会在面试环节出了插曲,比如,紧张就是最容易出现的了. 我相 ...
- The 2019 University of Jordan Collegiate Programming Contest
链接:https://codeforc.es/gym/102267 A. Picky Eater 直接比较 int main(){ int x ,y; scanf("%d %d" ...
- java9String类简单了解
public class jh_01_String类简单了解 { public static void main(String[] args) { /* * 函数:完成特定功能的代码块. * next ...