欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

mybatis一對多兩種mapper寫法實例

 更新時間:2020年12月04日 11:13:17   作者:愿你活成你喜歡的模樣  
這篇文章主要介紹了mybatis一對多兩種mapper寫法實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

mybatis一對多兩種mapper寫法

第一種

<resultMap type="com.example.demo.model.TuserModel" id="extendMapper">
 <id column="id" property="id" />
 <result column="user_name" property="userName" />
 <result column="nick_name" property="nickName" />
 <result column="avatar" property="avatar" />
 <result column="email" property="email" />
 <result column="signature" property="signature" />
 <result column="create_time" property="createTime" />
 <result column="update_time" property="updateTime" />
 <result column="del_flag" property="delFlag" />
 <collection property="tpluginModels" ofType="com.example.demo.model.TpluginModel"
 column="id">
 <id column="pid" property="id" />
 <result column="user_id" property="userId" />
 <result column="name" property="name" />
 <result column="icon" property="icon" />
 <result column="vsersion" property="vsersion" />
 <result column="tags" property="tags" />
 <result column="description" property="description" />
 <result column="bcreate_time" property="createTime" />
 <result column="bupdate_time" property="updateTime" />
 <result column="del_flag" property="delFlag" />
 </collection>
 </resultMap>

sql語句用聯(lián)表查詢

 u.*,p.id
 as
 pid,p.user_id,p.name,p.icon,p.vsersion,p.tags,p.description,p.create_time
 as bcreate_time,p.update_time as bupdate_time,p.del_flag from t_user u
 LEFT
 JOIN t_plugin p ON u.id=p.user_id and u.del_flag=0 and
 p.del_flag=0 WHERE
 u.user_name LIKE CONCAT('%',#{name},'%') OR
 u.nick_name LIKE
 CONCAT('%',#{name},'%')

第二種

<resultMap type="com.example.demo.model.TuserModel" id="extendMapper">
 <id column="id" property="id" />
 <result column="user_name" property="userName" />
 <result column="nick_name" property="nickName" />
 <result column="avatar" property="avatar" />
 <result column="email" property="email" />
 <result column="signature" property="signature" />
 <result column="create_time" property="createTime" />
 <result column="update_time" property="updateTime" />
 <result column="del_flag" property="delFlag" />
 
 <collection property="tpluginModels" column="id" ofType="com.example.demo.model.TpluginModel" 
 select="pluginByUid" /> //column='id' 為關(guān)聯(lián)查詢所需條件
 </resultMap>

sql語句使用兩個sql語句返回結(jié)果

 <select id="selectTuserBynameOrNick" resultMap="extendMapper"> SELECT 
 * FROM t_user WHERE del_flag = 0 AND ( user_name LIKE CONCAT( '%', #{name},'%') 
 OR nick_name LIKE CONCAT( '%', #{name},'%')) </select> 
//下個sql語句依賴上個
<select id="pluginByUid" resultType="com.example.demo.model.TpluginModel"> SELECT id,user_id as
 userId,name,icon,vsersion,tags,description,
 create_time as createTime ,update_time as updateTime ,del_flag as delFlag
 FROM t_plugin WHERE del_flag = 0 AND user_id = #{id}
 </select>

補充知識:Mybatis 一個dao 對應(yīng)多個Mapper.xml

由于項目中的mybatis的mapper是用mybatis generator自動生成的,但是生成的mapper滿足不了我的業(yè)務(wù),需要自己擴展,所以就研究了下、

添加接口

創(chuàng)建mapper.xml

修改配置

1.添加接口

在原dao中加個接口

/** ---------------自定義Mapper--------------- **/

List<PcacheCluster> select(ClusterInstanceBO clusterInstanceBO);

2. 創(chuàng)建mapper.xml

PcacheClusterMapperExtend.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.oppo.paas.pcache.manager.mapper.PcacheTemplateMapper">

 <select id="select" parameterType="com.oppo.paas.pcache.manager.entity.PcacheTemplate" resultMap="BaseResultMap">
 select
 <include refid="Base_Column_List" />
 from t_pcache_template 

 <where>
 <if test="templateId != null and templateId != ''">
  and template_id = #{templateId}
 </if>
 <if test="templateName != null and templateName != ''">
  and template_name = #{templateName}
 </if>
 <if test="templateType != null and templateType != ''">
  and template_type = #{templateType}
 </if>
 <if test="createUser != null and createUser != ''">
  and create_user = #{createUser}
 </if>
 <if test="createTime != null ">
  and create_time = #{createTime,jdbcType=TIMESTAMP}
 </if>
 </where>
 order by create_time desc
 </select>
</mapper>

3. 修改配置

項目目錄:

添加mapper掃描路徑

 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource"/>
 <!-- 自動掃描mapping.xml文件 -->
 <property name="mapperLocations" >
  <array>
  <value>classpath:mybatis/mappers/*Mapper.xml</value>
  <!-- 擴展mapper.xml -->
  <value>classpath:mybatis/mappers/extend/*MapperExtend.xml</value>
  </array>

 </property>
 <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property>
 <property name="plugins">
  <array>
  <bean class="com.github.pagehelper.PageInterceptor">
   <!-- 這里的幾個配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
   <property name="properties">
   <value>
    helperDialect=mysql
    reasonable=true
    supportMethodsArguments=true
    params=count=countSql
    autoRuntimeDialect=true
   </value>
   </property>
  </bean>
  </array>
 </property>
 </bean>

mybatis generator 已經(jīng)過時了哦,太麻煩,耦合性高,建議使用通用Mapper,完美繼承spring,springboot

學(xué)習(xí)地址:https://gitee.com/free/Mapper/wikis/Home

以上這篇mybatis一對多兩種mapper寫法實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • java替換url的域名和端口方法

    java替換url的域名和端口方法

    下面小編就為大家?guī)硪黄猨ava替換url的域名和端口方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-02-02
  • Java基礎(chǔ)之方法重寫和多態(tài)示例

    Java基礎(chǔ)之方法重寫和多態(tài)示例

    這篇文章主要介紹了Java基礎(chǔ)之方法重寫和多態(tài),結(jié)合實例形式分析了java方法重寫和多態(tài)的相關(guān)原理與使用技巧,需要的朋友可以參考下
    2019-08-08
  • SSM框架整合之junit測試的方法

    SSM框架整合之junit測試的方法

    本篇文章主要介紹了SSM框架整合之junit測試的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • activiti實現(xiàn)員工請假流程解析

    activiti實現(xiàn)員工請假流程解析

    這篇文章主要介紹了activiti實現(xiàn)員工請假流程解析,本文通過實例代碼圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Mybatis-Plus主鍵生成策略的方法

    Mybatis-Plus主鍵生成策略的方法

    本文主要介紹了Mybatis-Plus主鍵生成策略的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • java基于UDP實現(xiàn)在線聊天功能

    java基于UDP實現(xiàn)在線聊天功能

    這篇文章主要為大家詳細介紹了java基于UDP實現(xiàn)在線聊天功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-09-09
  • Java中ByteBuffer的allocate方法 和allocateDirect方法的區(qū)別和選用原則解析

    Java中ByteBuffer的allocate方法 和allocateDirect方法的區(qū)別和選用原則解析

    在Java中,ByteBuffer是java.nio包中的一個類,用于處理字節(jié)數(shù)據(jù),ByteBuffer提供了兩種方式來分配內(nèi)存:allocate和allocateDirect,這篇文章主要介紹了Java中ByteBuffer的allocate方法 和allocateDirect方法的區(qū)別和選用原則 ,需要的朋友可以參考下
    2023-12-12
  • 詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入

    詳解MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入

    這篇文章主要介紹了MyBatis直接執(zhí)行SQL查詢及數(shù)據(jù)批量插入的相關(guān)知識,需要的朋友一起學(xué)習(xí)吧
    2016-01-01
  • 手把手教你寫Maven的archetype項目腳手架

    手把手教你寫Maven的archetype項目腳手架

    本文主要介紹了Maven的archetype項目腳手架,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • MyBatis如何實現(xiàn)多表查詢(多對一、一對多)

    MyBatis如何實現(xiàn)多表查詢(多對一、一對多)

    這篇文章主要給大家介紹了關(guān)于MyBatis如何實現(xiàn)多表查詢(多對一、一對多)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05

最新評論