一 Mybatis学习随笔(mybatis学不会)

一、Mybatis的特性

  • MyBatis支持定制化SQL、存储过程以及高级映射
  • Mybatis避免了几乎所有的JDBC代码和手动设置参数以及结果集解析操作
  • Mybatis可以使用简单的XML或注解实现配置和原始映射,将接口和Java的POJO(普通的Java对象)映射成数据库中的记录
  • Mybatis是一个半自动的ORM(对象关系映射)框架
二、Mybatis的优势*轻量级,性能出色
*SQL和Java编码分开,功能边界清晰 。Java代码专注业务逻辑,SQL语句专注数据
*能够与Spring很好的集成
三、Mybatis的基本用法1.添加Mybatis所需依赖1.Mybatis核心包
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency>2.MySQL驱动
<dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.3</version></dependency>3.junit单元测试
<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency>4.log4j日志
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>2.配置Mybatis的全局配置文件mybatis-config.xml1.settings标签内
  • mapUnderscoreToCamelCase:驼峰式命名,为true表示
    开启自动映射驼峰式命名规则
    <setting name="mapUnderscoreToCamelCase" value="https://tazarkount.com/read/true"/>
  • aggressiveLazyLoading:积极的懒加载,使用在Mybatis的association关联对象(对一)和collection关联集合对象(对多)的延迟加载 。Mybatis版本大于3.4.1默认关闭
  • lazyLoadingEnabled:开始时,设置为True
<setting name="aggressiveLazyLoading" value="https://tazarkount.com/read/false"/><setting name="lazyLoadingEnabled" value="https://tazarkount.com/read/true"/>2.properties标签:关联外部属性文件
<properties resource="jdbc.properties"/>
3.mappers标签:注册Mapper配置文件
  • mapper标签:具体注册一个Mapper配置文件
<mapper resource="mappers/EmployeeMapper.xml"/>
  • package标签:指定Mapper配置文件所在包,全部注册
    注意:如果工程是Maven工程,那么Mapper配置文件还是要放在resources目录下,且目录结构要和Mapper接口的目录结构保持一致
<package name="com.atguigu.mybatis.mapper"/>
  • environments标签:配置Mybatis使用的环境,通常会转移到Spring中配置
<environments default="development"><!-- environment表示配置Mybatis的一个具体的环境 --><environment id="development"><!-- Mybatis的内置的事务管理器 --><transactionManager type="JDBC"/><!-- 配置数据源 --><dataSource type="POOLED"><!-- 建立数据库连接的具体信息 --><property name="driver" value="https://tazarkount.com/read/${wechat.dev.driver}"/><property name="url" value="https://tazarkount.com/read/${wechat.dev.url}"/><property name="username" value="https://tazarkount.com/read/${wechat.dev.username}"/><property name="password" value="https://tazarkount.com/read/${wechat.dev.password}"/></dataSource></environment></environments>3.配置Mybatis的映射文件Mapper.xml一、基本增删改查操作1.select标签
  • resultType属性:指定封装查询结果的实体类类型
    1.简单数据类型
    <select id="selectCount" resultType="int">SELECT count(*) from t_emp</select>2.Map类型
    <select id="selectEmpNameAndMaxSalary" resultType="map">select emp_name,max(emp_salary) from t_emp</select>3.实体类类型
    • 返回单个实体类
    <select id="selectEmpById" resultType="com.atguigu.mybatis.entity.Emp">select emp_id ,emp_name,emp_salary from t_emp where emp_id=#{empId}</select>
    • 返回实体类的集合:mybatis再将查询结果封装到实体类对象之后,会自动把多个实体类对象放在List集合返回
    <select id="selectAll" resultType="com.atguigu.mybatis.entity.Emp">select * from t_emp;</select>
  • resultMap属性
    1.映射实体类自身属性
    <!--专门声明一个resultMap设定column到property之间的对应关系--><resultMap id="empMapResultMap" type="com.atguigu.mybatis.entity.Emp"><id column="emp_id" property="empId"/><result column="emp_name" property="empName"/><result column="emp_salary" property="empSalary"/></resultMap><!--Emp selectWithResultMap(long empId)--><select id="selectWithResultMap" resultMap="empMapResultMap">select emp_id ,emp_name,emp_salaryfrom t_emp where emp_id = #{empId}</select>2.映射关联属性
    • 对一:association标签和javaType属性
    <resultMap id="selectOrderWithCustomerResultMap" type="com.atguigu.mybatis.entity.Order"><id column="order_id" property="orderId"/><result column="order_name" property="orderName"/><result column="customer_id" property="customerId"/><!-- 使用association标签配置“对一”关联关系 --><association property="customer" javaType="com.atguigu.mybatis.entity.Customer"><result column="customer_id" property="customerId"/><result column="customer_name" property="customerName"/></association></resultMap><!--Order selectOrderWithCustomer(long orderId);--><!--关键词:association javaType--><select id="selectOrderWithCustomer" resultMap="selectOrderWithCustomerResultMap">select order_id,order_name,c.customer_id,customer_namefrom t_order o left join t_customer con o.customer_id = c.customer_idwhere order_id = #{orderId}</select>
    • 对多:collection标签和ofType属性
    <resultMap id="selectCustomerWithOrderListResultMap" type="com.atguigu.mybatis.entity.Customer"><id column="customer_id" property="customerId"/><result column="customer_name" property="customerName"/><collection property="orderList" ofType="com.atguigu.mybatis.entity.Order"><result column="order_id" property="orderId"/><result column="order_name" property="orderName"/><result column="customer_id" property="customerId"/></collection></resultMap><!--Customer selectCustomerWithOrderList(long customerId);--><!--关键词:collection ofType--><select id="selectCustomerWithOrderList" resultMap="selectCustomerWithOrderListResultMap">select c.customer_id,customer_name,order_id,order_namefrom t_customer c left join t_order o ON c.customer_id = o.customer_idwhere c.customer_id = #{customerId}</select>
2.insert标签
  • useGeneratedKey属性:设置为true时表示使用数据库自增主键
  • keyProperty属性:指定实体类中接收自增主键的属性
<insert id="getGeneratedKeys" useGeneratedKeys="true" keyProperty="empId">insert into t_emp(emp_name, emp_salary) values (#{empName},#{empSalary})</insert>3.update标签
<update id="updateEmp">update t_emp set emp_name=#{empName},emp_salary=#{empSalary} where emp_id=#{empId}</update>4.delete标签
<delete id="deleteEmp">delete from t_emp where emp_id=#{empId}</delete>二、给SQL传参
  • #{}:将来变成?占位符
  • ${}:将来拼接字符串
三、动态SQL动态SQL存在的意义是为了解决拼接SQL语句字符串时的痛点问题 。
  • where标签:会自动去掉标签体内前面、后面多余的and/or
  • if标签
<select id="selectEmpByCondition" resultType="com.atguigu.mybatis.entity.Emp">select * from t_emp<where><if test="empName != null">or emp_name = #{empName}</if><if test="empSalary &gt; 800.00">or emp_salary > #{empSalary}</if></where></select>
  • set标签
  • trim标签
    • prefix属性:指定要动态添加的前缀
    • suffix属性:指定要动态添加的后缀
    • prefixOverrides属性:指定要动态去掉的前缀,使用"|"分隔有可能的多个值
    • suffixOverrrides属性:指定要动态驱动的后缀,使用"|"分隔有可能的多个值
<select id="selectEmpByConditionByTrim" resultType="com.atguigu.mybatis.entity.Emp">SELECT emp_id,emp_name,emp_salary from t_emp<trim prefix="where" suffixOverrides="and|or"><if test="empName != null">emp_name = #{empName} or</if><if test="empSalary &lt; 1000">emp_salary &lt;#{empSalary}</if></trim></select>
  • sql标签
  • foreach标签
    • collection属性:指定要遍历的集合
      • 使用@Param注解,按照注解指定的集合名来引用
      • 没使用@Param注解,则使用默认名collection或list
    • item属性:指定遍历出来每个元素的名字
    • open属性:指定整个循环把字符串拼好后,字符串整体的前面要添加的字符串
    • close属性
    • separator属性:循环体之间的间隔符号
    • index属性:遍历过程中的索引
  • 注意:在拼接多条SQL语句的时候,需要让连接数据库的地址后面附加一个参数:allowMultiQueries=true
<--批量插入多条数据--><insert id="insertEmpByBatch"> insert into t_emp(emp_name,emp_salary)<foreach collection="empList" index="index" item="emp" open="values" separator=",">(#{emp.empName},#{emp.empSalary})</foreach> </insert><--批量更新多条数据--><update id="updateEmpByBatch"><foreach collection="empList" separator=";" item="emp" >update t_emp set emp_name = #{emp.empName},emp_salary = #{emp.empSalary} where emp_id = #{emp.empId}</foreach></update></mapper>
  • choose/when/otherwise标签
  • include标签:引用已抽取的SQL片段,于sql标签搭配使用
四、开启二级缓存功能
  • 使用cache标签:type属性:指定具体的缓存产品,例如EHCache
【一 Mybatis学习随笔(mybatis学不会)】<cache type="org.mybatis.caches.ehcache.EhcacheCache"/>