大数据WEB阶段Mybatis(二)
生活随笔
收集整理的這篇文章主要介紹了
大数据WEB阶段Mybatis(二)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Mybatis(二)
零、目錄
- Mybatis接口形式
- Mybatis整合Spring
- Mybatis的緩存機制
- 手動封裝結果集
- 一對一表操作
- 一對多表操作
- 多對多表操作
- SpringMVC 、 Spring 、 Mybatis三大框架整合
一、 Mybatis接口形式
示例:
映射文件 <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
<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核心配置文件中配置二級緩存的總開關
<settings><setting name="cacheEnabled" value="true"/> </settings>在需要的mapper中添加二級緩存
<cache/>mapper對應的實體類需要實現序列化接口
public class User implements Serializable{}四、手動封裝結果集
示例:
<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>五、 一對一表操作
六、 一對多表操作
七、 多對多表操作
如果添加一個新的Teacher實體 針對這個實體進行數據查詢的時候需要做的事兒:
在核心配置文件中 添加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 sLEFT JOIN t_s tsONs.`id`=ts.`sid`) sLEFT JOIN teacher tONs.tid=t.`id`</select>八、 三大框架整合
配置文件改動
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阶段Mybatis(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大数据WEB阶段Mybatis(一)
- 下一篇: 大数据WEB阶段Maven安装配置与使用