MyBatis动态代理
使用MyBatis后,持久层的接口不需要再手动写实现类,MyBatis会使用代理模式自动实现该接口.
使用方法
通过SqlSession对象拿到mapper
//通过工具类拿到SqlSession
SqlSession sqlSession = MyBatisUtil.getSqlSession();
//拿到对应接口的mapper,Mybatis已经代理了IPersonRepository,Mapper是其子类
//class com.sun.proxy.$Proxy4
IPersonRepository mapper = sqlSession.getMapper(IPersonRepository.class);
注意事项
使用mapper,mapper.xml 的namespace 必须是对应接口的全限定名
<mapper namespace="com.marui.repository.IPersonRepository"> /<!--此处的必须是被代理接口的全限定名-->
</mapper>
mapper.xml中需要转义的符号
使用高级查询时,大于(>),小于(<) 等等可能再mapper.xml中需要转义
方式一
大于(>)-------- >
小于(<)-------- <
方式二
使用CDATA区域,将需要转义的代码包裹,解析器不回去解析此段代码.
结果映射(resultMap)
当进行嵌套结果查询(联表查询join,一条sql),嵌套查询(1+n条sql),查询出来的结果时一个复杂的对象,返回的是一个resultMap,需要自己告诉Mybatis如何解析,如何封装.
多对一关系
如: 多个员工对应一个部门
嵌套结果
public class Person {
private Long id;
private String username;
private Integer age;
private Dept dept; //外键
}
mapper.xml代码
<!--嵌套结果查询,一条sql语句 (联表查询) -->
<!--property 代表的时对象(type)的字段,column代表的时查询结果的列 配置他们之间的对应关系-->
<resultMap id="person_dept_mapper" type="person" >
<id property="id" column="id" ></id>
<result property="username" column="username" /><!--普通字段的映射的javaType是可以不写的–>-->
<result property="age" column="age" />
<!--外键 -->
<!--外键 property是对象字段 javaType 是字段的类型 -->
<association property="dept" javaType="dept" >
<id property="id" column="d_id" />
<result property="name" column="d_name" />
</association>
</resultMap>
<select id="queryAll" resultMap="person_dept_mapper" >
select *,d.id d_id,d.name d_name from t_person p join dept d on p.dept_id = d.id
</select>
嵌套查询
自己需要写两条sql语句,会发送(1+n条sql)
<!--所谓的嵌套查询(一条加+ nsql语句) - 就是把一张表查询出来后,再根据主键查询相应的外键,再把结果组合在一起-->
<resultMap id="personMap" type="person" >
<id property="id" column="id"/>
<result property="username" column="username" />
<result property="age" column="age" />
<!--column代表的是第一条查询语句查出来的结果列,该列的值要传入第二条查询语句中-->
<!-- 根据员工的外键再去拿部门,通过dept_id 再去拿部门-->
<association property="dept" javaType="dept" column="dept_id" select="getDept" ></association>
</resultMap>
<select id="queryAll" resultMap="personMap" >
select * from t_person
</select>
<!-- 第二条查询语句 -->
<select id="getDept" parameterType="long" resultType="dept" >
select * from dept where id = #{id}
</select>
一对多关系
public class Dept {
private Long id;
private String name;
// 一个部门多个员工
private List<Person> people = new ArrayList<>();
//以下省略
}
嵌套结果
一对多的嵌套结果不适合用来做分页查询
<!-- 一对多的嵌套结果查询,type是要返回的结果类型-->
<resultMap id="personMapper" type="com.marui.domain.Dept" >
<id property="id" column="id" />
<result property="name" column="name" />
<!-- 一对多的集合是用的collection,javaType是集合的类型,ofType是集合的泛型类型-->
<collection property="people" javaType="arraylist" ofType="com.marui.domain.Person" >
<id property="id" column="p_id" />
<result property="username" column="p_username" />
<result property="age" column="p_age" />
</collection>
</resultMap>
<select id="queryAll" resultMap="personMapper" >
select d.*,p.id p_id,p.username p_username,p.age p_age from dept d join t_person p on d.id = p.dept_id
</select>
嵌套查询
<!--嵌套查询-->
<resultMap id="personMapper" type="dept" >
<id property="id" column="id" />
<result property="name" column="name" />
<!--column 是要嵌套查询传入的表的列名,根据部门id去拿员工(通过部门的id去匹配dept_id), -->
<collection property="people" column="id" javaType="arraylist" ofType="person" select="getPerson" >
</collection>
</resultMap>
<!-- 第一条查询语句 -->
<select id="findAll" resultMap="personMapper" >
select * from dept
</select>
<select id="getPerson" parameterType="long" resultType="person" >
select * from t_person where dept_id = #{id}
</select>
ssm框架集成
spring + springMVC + Mybatis 框架的集成
application.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
<!--扫描service层的注解-->
<context:component-scan base-package="com.marui.ssm.service.impl" />
<!--引入jdbc的配置文件-->
<context:property-placeholder location="classpath:jdbc.properties" />
<!--配置datasource连接池,使用的是jdbc的连接池-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="url" value="${jdbc.url}" />
<property name="driverClassName" value="${jdbc.driver}" />
<property name="password" value="${jdbc.password}" />
<property name="username" value="${jdbc.username}" />
</bean>
<!--配置Mybatis 的factoryBean-->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean" >
<property name="dataSource" ref="dataSource" />
<!--扫描包,mapper.xml可以直接使用类的名字-->
<property name="typeAliasesPackage" >
<value>
com.marui.ssm.domain
</value>
</property>
<!--mapper.xml 配置文件的位置-->
<property name="mapperLocations" value="classpath:mappers/*Mapper.xml" />
</bean>
<!--配置接口mapper,让spring自动注入,需要配置mapperFactoryBean-->
<!--<bean id="mapperFactoryBean" class="org.mybatis.spring.mapper.MapperFactoryBean" >
<property name="sqlSessionFactory" ref="sqlSessionFactoryBean" />
<property name="mapperInterface" >
<value>
com.marui.ssm.mapper.xxx //此方法需要手动配置mapper的接口,麻烦,直接取扫描包,把包下面的都作为mapper
</value>
</property>
</bean>-->
<!-- 一劳永逸
Mapper(映射)Scanner(扫描)Configurer(配置)
basePackage:扫描的包(会用对象的类都创建实现)
-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.marui.ssm.mapper" />
</bean>
<!--事务配置-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 -->
<tx:annotation-driven transaction-manager="transactionManager" />
</beans>
springMVC的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
">
<!--扫描controller的注解-->
<context:component-scan base-package="com.marui.ssm.web.controller" />
<!--2.静态资源(js,css,图片...)放行-->
<mvc:default-servlet-handler />
<!--3.mvc的注解支持-->
<mvc:annotation-driven />
<!--4.视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
web.xml 配置文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--准备一个监听器,启动Spring-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--告诉监听器,核心配置文件在哪里-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置核心控制器-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--读取springmvc的xml-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-mvc.xml</param-value>
</init-param>
<!--让这个Servlet随着服务器启动而启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<!-- /:更加符合咱们的RESTful风格 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 配置编码方式过滤器,注意一点:要配置在所有过滤器的前面 -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>