1.传递pojo对象
Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。
2.传递pojo包装对象
开发中通过pojo传递查询条件,查询条件是综合的查询条件,不仅包括用户查询条件还包括其他的查询条件,这时可以使用包装对象传递输入参数
Pojo类中包含pojo`
public class QueryVo {
private User user;
private List<Integer> ids;
public List<Integer> getIds() {
return ids;
}
public void setIds(List<Integer> ids) {
this.ids = ids;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
sql语句
SELECT * FROM user WHERE id=#{user.id};
Mapper文件
<select id="getUserByQueryVo" parameterType="queryvo" resultType="user">
SELECT * FROM user WHERE id=#{user.id};
</select>
接口
User getUserByQueryVo(QueryVo queryVo);
测试方法
@Test
public void testgetUserByQueryVo(){
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo=new QueryVo();
User user=new User();
user.setId(10);
queryVo.setUser(user);
User result = mapper.getUserByQueryVo(queryVo);
System.out.println(result);
sqlSession.close();
}
3.resultType(输出类型)
Mapper.xml文件
< select id="getUserCount" parameterType="user" resultType="int">
SELECT count(*) FROM user;
</select>
接口
Integer getUserCount();
测试
@Test
public void tesrgetUserCount(){
SqlSession sqlSession = sqlSessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
int userCount = mapper.getUserCount();
System.out.println(userCount);
sqlSession.close();
}
4.resultMap
resultType可以指定pojo将查询映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。
如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系,resultMap实质上还需要将查询结果映射到pojo对象中。
resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询
<!-- resultMap -->
<!-- resultMap定义
type:返回映射的pojo,可以使用别名 -->
<resultMap type="orders" id="order_list_result_map">
<!-- id主键的映射,property时候pojo中主键的属性,column:返回结果主键的列-->
<id property="id" column="id"/>
<!-- 普通列使用result映射 -->
<result property="userId" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</resultMap>
<select id="getOrderListResultMap" resultMap="order_list_result_map">
select id,user_id,number,createtime,note from oders
</select>
:此属性表示查询结果集的唯一标识,非常重要。如果是多个字段为复合唯一约束则定义多个< id />。
Property:表示User类的属性。
Column:表示sql查询出来的字段名。
Column和property放在一块儿表示将sql查询出来的字段映射到指定的pojo类属性上。
:普通结果,即pojo的属性。
5.动态sql
if
<select id="findUserList" parameterType="user" resultType="user">
select * from user
where 1=1
<if test="id!=null">
and id=#{id}
</if>
<if test="username !=null and username !='' ">
and username like '%${username}%'
</if>
</select>
where
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="username !=null and username !='' ">
and username like '%${username}%'
</if>
</where>
</select>
foreach
向sql传递数组或list,mybatis使用foreach解析
<!-- 动态sql foreach测试 -->
<select id="findUserByIds" parameterType="queryvo" resultType="user">
select * from user
<where>
<foreach collection="ids" item="id" open="and id in(" close=")" separator=",">
#{id}
</foreach>
</where>
</select>
6.Sql片段
Sql中可将重复的Sql提取出来,使用时用include引用即可,最终达到sql重复的目的
<!-- 传递pojo综合查询用户信息 -->
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</where>
</select>
将where条件抽取出来
<sql id="query_user_where">
<if test="id!=null and id!=''">
and id=#{id}
</if>
<if test="username!=null and username!=''">
and username like '%${username}%'
</if>
</sql>
使用include引用
<select id="findUserList" parameterType="user" resultType="user">
select * from user
<where>
<include refid="query_user_where"/>
</where>
</select>
注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
<include refid="namespace.sql片段”/>
7.一对一resultMap
<resultMap type="orders" id="Order_user_resultmap">
<id property="id" column="id"/>
<result property="userId" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
<!-- 配置一对一关联映射 -->
<!-- property:对应orders中user的属性 -->
<association property="user" javaType="com.itheima.mybatis.po.User">
<id property="id" column="user_id"/>
<result property="username" column="username"/>
<result property="address" column="address"/>
</association>
</resultMap>
<select id="getOrderUserResultMap" resultMap="Order_user_resultmap">
SELECT
o.id,
o.user_id,
o.number,
o.note,
o.createtime,
u.username,
u.address
FROM
orders o
LEFT JOIN `user` u ON o.user_id = u.id
</select>
一对多
<!-- 一对多关联映射 -->
<resultMap type="user" id="user_order_resultmap">
<id property="id" column="id"/>
<result property="username" column="username"/>
<result property="birthday" column="birthday"/>
<result property="sex" column="sex"/>
<result property="address" column="address"/>
<!-- 配置一对多关联映射 -->
<!-- property:对于user对象中的集合属性 -->
<!-- ofType:集合中每个元素的数据类型 -->
<collection property="orders" ofType="orders">
<!-- id对应order的主键属性 -->
<id property="id" column="oid"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</collection>
</resultMap>
<select id="getUserWithOrders" resultMap="user_order_resultmap">
SELECT
u.id,
u.username,
u.birthday,
u.sex,
u.address,
o.id oid,
o.number,
o.createtime,
o.note
FROM
`user` u
LEFT JOIN orders o ON u.id = o.user_id
</select>
8.Mybatis整合spring
(1).整合思路
- SqlSessionFactory对象应该放到spring容器中作为单例存在。
- 传统dao的开发方式,应该从spring容器中获得sqlsession对象。
- Mapper代理形式中,应该从spring容器中直接获得mapper的代理对象。
- 数据库的连接以及数据库连接池事务管理都应该交给Spring容器来完成。
(2)所需的所有jar包
mybatis整合spring的jar包](https://download.csdn.net/my)
(3)mybatis配置的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>
<!-- 配置pojo别名 -->
<typeAliases>
<!-- <typeAlias type="com.baidu.po.User" alias="user"/> -->
<!-- 扫描包的形式创建别名,别名就是类名,不区分大小写 -->
<package name="com.baidu.mybatis.po"/>
</typeAliases>
<!-- 加载mapper文件 -->
<mappers>
<!-- resource基于classpath查找 -->
<mapper resource="sqlmap/user.xml"/>
<!-- <mapper resource="mapper/mapper.xml"/> -->
</mappers>
</configuration>
(4)Spring的配置applicationCont.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">
<!-- 配置数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
</beans>
传统的dao开发
<?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">
<!-- 配置数据库连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载配置文件 -->
<property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"></property>
</bean>
<!-- 传统dao的配置方法 -->
<bean id="userDaoImpl" class="com.baidu.mybatis.dao.UserDaoImpl">
<!-- 注入sqlSessionFactory -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
</beans>
Mapper代理形式开发dao
<!-- mapper代理形式dao的配置 -->
<!-- 第一种方式,配置代理对象 -->
<!-- <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
设置代理的mapper接口
<property name="mapperInterface" value="com.itheima.mybatis.mapper.UserMapper"></property>
注入sqlSessionFactory
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean> -->
<!-- 配置包扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 配置要扫描的包 -->
<property name="basePackage" value="com.itheima.mybatis.mapper"></property>
</bean>