您的位置:首页 > 大数据

大数据WEB阶段Mybatis(二)

2017-12-28 22:22 253 查看

Mybatis(二)

零、目录

Mybatis接口形式

Mybatis整合Spring

Mybatis的缓存机制

手动封装结果集

一对一表操作

一对多表操作

多对多表操作

SpringMVC 、 Spring 、 Mybatis三大框架整合

一、 Mybatis接口形式

为表User创建映射接口

此时要注意

接口的全名称要与映射文件中的namespace一致。

接口中的方法名与映射文件中sql的id一致

示例:

映射文件
<mapper namespace="com.tj.mapper.UserMapper">
<select id="findAll" resultType="com.tj.pojo.User" >
select * from user ;
</select>

</mapper>

映射接口
package com.tj.mapper;

import java.util.List;

import com.tj.pojo.User;
public interface UserMapper {
/**
* 查询所有用户
* */
public List<User> findAll();
}


二、mybatis整合Spring

导入在Mybatis所有jar包和Spring的所有jar包的基础上 , 加上mybatis-spring-1.2.0.jar



配置文件

在mybatis配置文件中删除所有的配置 , 仅剩根节点即可

Spring在原有的基础上添加两个bean

会话工厂

数据源

核心配置文件

映射文件配置

映射接口扫描器

<?xml version="1.0" encoding="UTF-8"?>

<beans

xmlns="http://www.springframework.org/schema/beans"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:util="http://www.springframework.org/schema/util"

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-3.2.xsd  http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

">

<!-- 配置mybatis -->

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">

<!-- 配置数据源 -->

<property name="dataSource" ref = "datasSource"></property>

<!-- 注入mybatis核心配置文件 -->

<property name="configLocation" value="classpath:/sqlMapConfig.xml"></property>

<!-- 注入映射文件 -->

<property name="mapperLocations" value="classpath:/com/tj/pojo/*.xml"></property>

</bean>

<!-- 配置映射接口扫描器 -->

<bean class = "org.mybatis.spring.mapper.MapperScannerConfigurer">

<property name="basePackage" value="com.tj.mapper"></property>

</bean>

<!-- 读取外部配置文件 -->

<context:property-placeholder location="classpath:/jdbc.properties" />

<!-- 配置数据源 -->

<bean id = "datasSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" >

<property name="user" value="${user}"></property>

<property name="password" value="${password}"></property>

<property name="jdbcUrl" value="${jdbcUrl}"></property>

<property name="driverClass" value="${driverClass}"></property>

</bean>

<!-- 开启事务注解模式 -->

<tx:annotation-driven transaction-manager="transactionManager"/>

<!-- 开启注解模式 -->

<context:annotation-config />

<!-- 开启包扫描 -->

<context:component-scan base-package="com.tj"></context:component-scan><!-- 扫描com.tj子包下的所有类 -->

<!-- 配置aop -->

<aop:aspectj-autoproxy/>

</beans>


三、mybatis缓存机制

mybatis中缓存机制分为一级缓存和二级缓存

一级缓存 , 默认共享同一个session中的数据

二级缓存共享同一个sessionFactory中的数据

一级缓存默认是开启的 , 但是二级缓存默认是关闭的

开启二级缓存

在mybatis核心配置文件中配置二级缓存的总开关

<settings>
<setting name="cacheEnabled" value="true"/>
</settings>


在需要的mapper中添加二级缓存

<cache/>


mapper对应的实体类需要实现序列化接口

public class User implements Serializable{

}


注意:

无论时一级缓存还是二级缓存 , 当数据做出修改后 , 缓存的数据就会被清除, 下次查询回去数据库中取值

二级缓存的数据只有当session会话被关闭时才会把数据缓存起来 。

四、手动封装结果集

如果实体类中的属性名和数据库中的字段名不一致时 , 结果集自动封装就会失败 , 需要手动封装

注意:结果集自动化封装实际上是以setxxx方法的xxx为准的

示例:

<select id="findAll" resultMap="userRM" >
select * from user ;
</select>

<resultMap type="com.tj.pojo.User" id="userRM" autoMapping="true"><!-- 如果字段名与属性名一致则自动注入 -->
<!-- 主键必须写 -->
<id column="id" property="id"/>

<!-- 自定义类型需要手动封装结果集 -->
<association property="userinfo" javaType="com.tj.pojo.UserInfo">
<!-- 在上面配置了字段名与属性名一致时自动注入 , 但是在自定义类型中不生效 -->
<id column="uid" property="uid"/>
<result column="phone" property="phone"/>
<result column="parentid" property="parentid"/>
</association>
</resultMap>


五、 一对一表操作



一对一查询中 , 主表可以将非主键自动封装(但是要求表字段与实体类的属性相同) , 主键必须手动注入,且从表不能手动注入

六、 一对多表操作

实体类中属性为List类型



2.

七、 多对多表操作

查询老师教的所有学生

查询一个学生的所有老师

如果添加一个新的Teacher实体 针对这个实体进行数据查询的时候需要做的事儿:

创建TeacherMapper接口

创建TeacherMapper.xml映射文件 在映射文件里 把nameSpace改成 cn.tedu.mapper.TeacherMapper

在TeacherMapper.xml映射文件中写sql语句 在接口类中创建和sql语句id一致的方法名

在核心配置文件中 添加teacherMapper.xml的引入

<resultMap type="cn.tedu.pojo.Student" id="studentRM">
<id column="sid" property="id"/>
<result column="sname" property="name"/>
<collection property="teachers" ofType="cn.tedu.pojo.Teacher">
<id column="tid" property="id"/>
<result column="tname" property="name"/>
</collection>
</resultMap>
<select id="findAllStudents" resultMap="studentRM">
SELECT s.id sid,s.name sname,t.`id` tid,t.`name` tname FROM
(SELECT * FROM
student s
LEFT JOIN
t_s ts
ON
s.`id`=ts.`sid`) s
LEFT JOIN
teacher t
ON
s.tid=t.`id`
</select>


八、 三大框架整合



一共四个配置文件: web.xml 、applicationContext.xml 、applicationContext-mvc.xml 、 sqlMapConfig.xml

配置文件改动

web.xml中不仅引入SpringMVC的核心配置文件 , 还要引入Spring的核心配置文件

mybatis核心配置文件留下根节点 , 其余删除 , 在需要时可以在其中配置缓存 、 别名等

Spring 核心配置文件中添加两个bean

<!-- 配置sql会话工厂 -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 配置数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 引入Mybatis的核心配置文件 -->
<property name="configLocation"
value="classpath:/sqlMapConfig.xml"></property>
<!-- 引入所有的Mapper配置文件 -->
<property name="mapperLocations"
value="classpath:/cn/tedu/pojo/*.xml"></property>
</bean>

<!-- 配置mapper接口扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tedu.mapper"></property>
</bean>


注意:在web工程中如果访问src路径下面的内容 , 需要在文件名前加上classpath:/如:classpath:/jdbc.properties
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息