MyBatis学习笔记二
知识纲领:
- 输入映射和输出映射
- 动态SQL
- 关联查询
- MyBatis整合spring
- MyBatis逆向工程
一. 映射
对于映射的理解?
- Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心
输入映射(parameterType)
- 传递简单类型
- 使用#{} 或者 ${} 进行SQL拼接
- 传递pojo类型
- #{} ${} 中的值为pojo属性名称
- 传递pojo包装对象
- 什么是包装类型; pojo 中的一个属性是另外一个pojo
输出映射(resultType)
- 输出简单类型(integer等类型)
- 输出pojo对象
- 输出pojo列表
resultMap
resultType可以指定将查询结果映射为pojo,
但需要pojo的属性名和sql查询的列名一致方可映射成功。
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 ,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
resultMap的使用
<resultMap id="orderResult" type="order">
<result property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="note" column="note" />
</resultMap>
二. 动态SQL
- if标签, where标签 , include标签
<!--使用标签进条件查询-->
<select id="findBySexAndName" parameterType="user" resultType="user">
select * from user
<where>
<if test="sex != null">
and sex = #{sex}
</if>
<if test="username != null and username != ''">
and username like
'%${username}%'
</if>
</where>
</select>
- foreach标签
<!--使用foreeach来查询多个数据-->
<select id="findUserByIds" parameterType="ubaba" resultType="user">
select * from user
<where>
<foreach collection="ids" item="item" open="id in (" close=")" separator=",">
#{item}
</foreach>
</where>
</select>
三. 关联查询
1. 一对一查询
- 使用resultType(企业中常用)
- 自定义一个pojo类来存放数据
- 使用resultMap
- 使用的原理就是在pojo中嵌入其他的pojo类
2. 一对多查询
<resultMap id="userOrderResultMap" type="user">
<id column="id" property="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<collection property="orders" javaType="list" ofType="order">
<id column="id" property="id"/>
<result property="number" column="number" />
<result property="createtime" column="createtime" />
<result property="note" column="note" />
</collection>
</resultMap>
四. MyBatis整合spring
整合步骤:
- 将SqlSessionFactory放到spring容器中作为单例对象
- sqlsession应该从spring容器中获得
- mapper代理形式中, 应该从spring容器中直接获得mapper的代理对象
- 数据库的连接以及数据库连接池事务管理都交给spring容器来完成
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!-- 设置别名 -->
<typeAliases>
<!-- 2. 指定扫描包,会把包内所有的类都设置别名,别名的名称就是类名,大小写不敏感 -->
<package name="com.lyric.mybatis.pojo" />
</typeAliases>
<mappers>
<package name="com.lyric.mybatis.mapper"/>
</mappers>
</configuration>
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 数据库连接池 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="10" />
<property name="maxIdle" value="5" />
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置mybatis核心配置文件 -->
<property name="configLocation" value="classpath:SqlMapConfig.xml" />
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource" />
</bean>
<!-- Mapper代理的方式开发方式一,配置Mapper代理对象 -->
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!-- 配置Mapper接口 -->
<property name="mapperInterface" value="com.lyric.mybatis.mapper.UserMapper" />
<!-- 配置sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- Mapper代理的方式开发方式二,扫描包方式配置代理 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置Mapper接口 -->
<property name="basePackage" value="com.lyric.mybatis.mapper" />
</bean>
</beans>
五. 逆向工程
- 在配置文件中修改三个地方