日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

大数据WEB阶段Mybatis(二)

發布時間:2024/4/30 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大数据WEB阶段Mybatis(二) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 sLEFT JOIN t_s tsONs.`id`=ts.`sid`) sLEFT JOIN teacher tONs.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
  • 總結

    以上是生活随笔為你收集整理的大数据WEB阶段Mybatis(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。