【软件开发架构平台】CH8 Mybatis进阶

Spring Boot整合Mybatis
  • 将Configuration.xml的内容整合到application.properties中统一配置

  • 使用Spring IoC 容器管理SqlSessionFactiory 和SqlSession对象

  • 使用面向接口的编程+Mapper配置文件方式实现DAO层类

  • 支持注解编程

Configuration

Configuration的配置内容

  • environments:环境配置

  • mappers:映射器

  • typeAliases:类型别名

    • mybatis.type-aliases-package=org.csu.demo.domain
      
  • settings:设置

    • 延迟加载:默认值为false。应用场景为关联查询,当开启时,所有关联对象都会延迟加载(当真正使用时才会查询数据库),目的在于提高程序执行效率

    • mybatis.configuration.lazy-loading-enabled = true
      
  • typeHandler:类型处理器

    • 用于解决数据库中的数据类型和Java语言中的数据类型不匹配问题

    • Mybatis从结果集中取出一个值时,都会用类型处理器将获取到的值以合适的方式转换为Java类型

    • mybatis.type-handlers-package = org.csu.demo.persistence.util
      
Mapper

CRUD元素常用属性

  • parameterType:用于指定传入SQL语句的参数类型
  • resultType:用于指定SQL语句执行完成后的返回值类型
  • sql参数:使用“#{}”语法在SQL语句中预留参数

参数类型指定

  • 如果出现参数类型(Java)和数据库字段类型不匹配的情况,就需要参数类型指定
#{property,javaType=int,jdbcType=NUMERIC}

结果映射ResultMap

可实现“对简单的查询零配置(80%),对复杂的查询只需描述语句关系(20%)”的设计目标

结果映射常用的两个属性是resultType和resultMap

  • resultType用于设置将结果集应色号到已有类型上,可以是普通类型、集合类型、自定义bean等
  • resultMap用于设置复杂映射,指返回的结果集和已有的类型不完全匹配的情况下,二者不能同时使用
<!--当返回结果有多行时,可以使用hashmap解决类型问题-->
<!--resultType="hashmap"-->
<!--resultType:可一一对应,resultMap:无法一一对应-->
<select id="addAccount" parameterType="String">
        INSERT INTO SIGNON (USERNAME,PASSWORD)
	  		VALUES(#{username}, #{password})
</select>
动态SQL语句

由于实际项目的业务逻辑的复杂性,豁达配置需要动态的拼接SQL,如模糊搜索、条件查询等

Mybatis中有if,choose(when,otherwise),trim(where,set),foreach四个用于动态SQL的标签元素

if

<select id="findActiveBlogWithTitleLike" resultType="Blog">
	SELECT* FROM BLOG WHERE state = 'ACTIVE'
	<if test="title!=null">
		AND title like #{title}
	</if>
</select>

choose(when,otherwise)——复杂条件判断

<select id="findActiveBlogWithTitleLike" resultType="Blog">
	SELECT* FROM BLOG WHERE state = 'ACTIVE'
	<choose>
		<when test="title!=null">
			AND title like #{title}
		</when>
		<when test="author!=null and author.name!=null">
			...
		</when>
		<otherwise>
			...
		</otherwise>
	</choose>
</select>

trim(where,set)——防止特殊情况SQL错误

<select id="findActiveBlogWithTitleLike" resultType="Blog">
	SELECT* FROM BLOG WHERE
	<if test="title!=null">
		AND title like #{title}
	</if>
	<if test="state!=null">
		...
	</if>
</select>
<select id="findActiveBlogWithTitleLike" resultType="Blog">
	SELECT* FROM BLOG
	<where>
		<if test="title!=null">
			AND title like #{title}
		</if>
		<if test="state!=null">
			...
		</if>
	</where>
</select>

foreach——复杂查询

<select id="selectPostIn" resultType="domain.blog.Post">
	SELECT * FROM POST P WHERE ID in
	<foreach item="item" index="index" collection="list" open="(" seperator=" " close=")">
		#{item}
	</foreach>
</select>
Mybatis其他使用

MyBatis 三剑客
Mybatis-generator:自动代码生成工具。自动生成Javabean和Mapper映射器文件

<sqlMapGenerator targetPackage="mappers" targetProject="./src/main/resources">
	<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>

Mybatis plugin:Mapper接口到xml映射器的自动导航
Mybatis PageHelper:查询数据量大的数据库表时,可以减少数据库查询压力,降低客户端的数据加载量

Mybatis plus:包含三剑客的功能,无侵入、损耗小,直接面向对象操作。

public void addAccount(String username,String password){
        accountMapper.addAccount(username,password);
    }

public List<Item> getItemListByProduct(String productId){

        return itemMapper.getItemListByProduct(productId);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值