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>

最新文章

  1. 用ffmpeg合并音频文件
  2. dll 日志文件 放在同一个目录。
  3. CI控制器中设置在其它方法中可用的变量
  4. ubuntu关闭自动更新、打开 ubuntu 的 apport 崩溃检测报告功能
  5. Codeforces 1C Ancient Berland Circus
  6. 自定的 HttpModule 为什么总是执行两次
  7. PowerPoint Office Mix 插件
  8. mysql show variables系统变量详解
  9. MachineKey
  10. spark 启动job的流程分析
  11. smarty(原理概述)
  12. Mac下配置Nginx负载均衡
  13. HTML入门11
  14. [LeetCode] Sliding Puzzle 滑动拼图
  15. js 给定时间,如&#39;2013-08-30&#39;,换算和今天的天数差
  16. rails 杂记 - erb 中的 link_to 的 ActiveRecord 与 render 中的 partial
  17. 第四节《Git检出》
  18. QScrollBar &amp; QSlider &amp; QDial
  19. MYSQL MyISAM与InnoDB对比
  20. 100-days: eight

热门文章

  1. Jpofiler
  2. 深入JVM类加载器机制,值得你收藏
  3. OpenCV3入门(二)Mat操作
  4. c++中重载运算符
  5. 第四次作业:使用Packet Tracer理解RIP路由协议及ICMP协议
  6. 十二、sed文本处理
  7. 11、ACL
  8. PaddleBook的部署安全性问题
  9. Java 添加、读取、删除Excel文档属性
  10. 鸭子类型 - Duck Typing