一、Mybatis的多表关系
- 一对一关系
<!-- 一个订单属于一个用户,在订单类orders中有一个用户user的对象,
但是当我们使用多表联查时,虽然会同时查出订单与用户的信息,但是使用resultType标签时,
属性名与字段名是一一对应的,因此只有订单的属性会被赋值,因为找不到与user对象对应的字段,
所以用户对象不会被赋值,因此这是要使用resultMap标签
resultMap标签作用是可以自定义地将数据库字段赋值给相应的属性,引号中的值是自己定义的id值
-->
<select id="findOrdersById" parameterType="int" resultMap="orderResult">
SELECT o.*,u.username,u.birthday,u.sex,u.address FROM orders o LEFT JOIN USER u ON o.user_id = u.id WHERE o.id = #{id}
</select>
<!-- 这里就开始自定义字段与属性的映射关系,type为主表对应java对象类型,id值自定义 -->
<resultMap type="orders" id="orderResult">
<!-- id标签为唯一标识一个对象 column为数据库字段名,property为属性名 -->
<id column="id" property="id"/>
<!-- 除了id,其他字段使用result标签 -->
<result column="user_id" property="userId"/>
<result column="number" property="number"/>
<result column="createtime" property="createtime"/>
<result column="note" property="note"/>
<!-- 因为是一对一关系,这里使用association标签表示查出的数据为单条数据
property是指订单类中定义的用户对象的属性名 javaType为该对象的类型,这里用别名
-->
<association property="user" javaType="user">
<!-- id标签为唯一标识一个对象 column为数据库字段名,property为属性名
这里开始的是为订单类中的用户对象中的属性进行赋值
注意字段名要正确对应属性名
-->
<id column="user_id" property="id"/>
<result column="address" property="address"/>
<result column="birthday" property="birthday"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
</association>
</resultMap>
- 一对多关系
<select id="findUserById1" parameterType="int" resultMap="userResult">
SELECT u.*,o.id oid,o.number,o.createtime,o.note FROM USER u LEFT JOIN orders o ON u.id = o.user_id WHERE u.id = #{id}
</select>
<resultMap type="user" id="userResult">
<id column="id" property="id"/>
<result column="address" property="address"/>
<result column="birthday" property="birthday"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<!-- 一个用户可以有多个订单,所以是多对一关系,这里就使用collection标签表示查出的是多条数据
注意这里使用ofType来表示查出的每条数据对应的对象类型
-->
<collection property="orders" ofType="orders">
<id column="oid" property="id"/>
<result column="number" property="number"/>
<result column="note" property="note"/>
<result column="createtime" property="createtime"/>
</collection>
</resultMap>
二、整合spring
- 整合需要的jar
1、spring的jar包
2、Mybatis的jar包
3、Spring+mybatis的整合包。
4、Mysql的数据库驱动jar包。
5、数据库连接池的jar包。
- 整合的步骤
1、创建一个java工程。
2、导入jar包。(上面提到的jar包)
3、mybatis的配置文件sqlmapConfig.xml
4、编写Spring的配置文件:
(1)数据库连接及连接池
(2)事务管理
(3)sqlsessionFactory对象,配置到spring容器中
(4)mapeer代理对象或者是dao实现类配置到spring容器中
5、编写dao或者mapper文件
- spring的配置文件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">
<!-- splMapConfig.xml中的数据库连接池的配置可以直接转移到spring的配置中 -->
<!-- 加载配置文件 -->
<context:property-placeholder location="classpath:jdbc.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>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory 使用mybatis和spring整合包中的,这样mybatis中的配置文件的environments就都不需要了 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 别名的配置也可以在这里配置 -->
<property name="typeAliasesPackage" value="com.wzm.domain"></property>
<!-- 加载mybatis的全局配置文件
这里可以加载mybatis的文件,但是如果文件中的配置都移到spring的话就没必要加载了
-->
<!-- <property name="configLocation" value="classpath:SqlMapConfig.xml" /> -->
</bean>
<!-- 这里是使用传统开发方式,直接创建接口实现类,将实现类交给spring管理 -->
<bean id="userDao" class="com.wzm.dao.impl.UserDaoImpl">
<!-- 依赖注入 -->
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<!--
这个是动态代理开发方式,直接让spring管理动态代理生成的对象,这个缺点就是每一接口都需要一一配置,比较麻烦
<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="com.wzm.mapper.UserMapper"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean> -->
<!-- 这个也是动态代理方式,但是这里是直接扫描包下所有接口,为所有接口生成代理对象,比较方便
spring会自动为代理对象注入sqlSessionFactory,生成的代理对象的bean的id值默认为接口名
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.wzm.mapper"></property>
</bean>
</beans>
- 测试案例
public class SpringMybatisTest {
public static void main(String[] args) {
//代理开发方式
//加载spring配置文件
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
//获取代理对象
UserMapper userMapper = (UserMapper) context.getBean("userMapper");
//执行操作
User user = userMapper.findUserById(1);
System.out.println(user);
}
}