色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

mybatis一對多兩種mapper寫法實例

瀏覽:106日期:2023-10-20 17:33:10

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語句用聯表查詢

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’ 為關聯查詢所需條件 </resultMap>

sql語句使用兩個sql語句返回結果

<select 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 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 對應多個Mapper.xml

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

添加接口

創建mapper.xml

修改配置

1.添加接口

在原dao中加個接口

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

List<PcacheCluster> select(ClusterInstanceBO clusterInstanceBO);

2. 創建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 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. 修改配置

項目目錄:

mybatis一對多兩種mapper寫法實例

添加mapper掃描路徑

<bean 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 已經過時了哦,太麻煩,耦合性高,建議使用通用Mapper,完美繼承spring,springboot

學習地址:https://gitee.com/free/Mapper/wikis/Home

以上這篇mybatis一對多兩種mapper寫法實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Mybatis 數據庫
相關文章:
主站蜘蛛池模板: 美国一级做a一级视频 | 国产精选经典三级小泽玛利亚 | 欧美整片在线 | 九九视频精品在线 | 欧美高清免费一级在线 | 国模午夜写真福利视频在线 | 国产精品欧美一区二区在线看 | 国产成年人视频 | 久久国产精品久久国产片 | 五月色婷婷综合开心网亚 | 国产高清视频在线观看 | 欧美日韩一区在线观看 | 久久免费国产精品一区二区 | 成人做爰在线视频 | 中文字幕日韩精品有码视频 | 九九精品视频在线播放8 | 亚洲天堂久久 | a级毛片免费看 | 高清一本之道加勒比在线 | 九九精品激情在线视频 | 成人亚洲综合 | 亚洲精品国产精品国自产网站 | 黄色影视频 | 99精品免费视频 | 久久精品国产99国产精品亚洲 | 亚洲精品人成网在线播放影院 | 玖草在线播放 | 男人女人做刺激视频免费 | 亚洲精品国产精品国自产观看 | 国产精品特黄毛片 | 特级毛片永久久免费观看 | 欧美一级毛片高清视频 | 精品欧美一区二区三区精品久久 | 国产三级欧美 | 国产欧美日韩在线一区二区不卡 | 露脸国产野战最新在线视频 | 亚洲视频在线观看视频 | 午夜一级做a爰片久久毛片 午夜伊人网 | 免费看欧美一级片 | 三级黄色网址 | 中文字幕在线看片成人 |