解決mybatis一對多關聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題
一對多,如果多個表字段名相同,要記住使用別名,否則多條數(shù)據(jù)只顯示一條
<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> <!-- <collection property="tpluginModels" column="id" ofType="com.example.demo.model.TpluginModel" select="pluginByUid" /> -->
下列sql
<select id="selectTuserBynameOrNick" resultMap="extendMapper"> select 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},'%') </select>
補充知識:MyBatis使用resultMap解決列名和屬性名不一致的問題
resultType可以指定將查詢結果映射為pojo,但需要pojo的屬性名和sql查詢的列名一致方可映射成功。
如果sql查詢字段名和pojo的屬性名不一致,可以通過resultMap將字段名和屬性名作一個對應關系 ,resultMap實質上還需要將查詢結果映射到pojo對象中。
需求:查詢訂單表orders的所有數(shù)據(jù)
SELECT id,user_id,number,createtime,note FROM orders,這里的數(shù)據(jù)庫表user_id與pojo的Order對象中的userId不一致
orders表:
Order對象:
OrderMapper.xml配置:
其中注釋掉了另一種使用數(shù)據(jù)庫別名解決列名和屬性名不一致的問題
<?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.sea.crm.mapper.OrderMapper"> <!-- 使用as 給列取別名解決類名和屬性名不一致的情況 --> <!--<select id="queryAll" resultType="Order"> SELECT id,user_id as userId,number,createtime,note FROM orders </select> --> <!-- 使用resultMap解決列名和屬性名不一致的情況 --> <!-- 配置一個resultMap映射列和屬性 --> <resultMap type="Order" id="orderMap"> <!-- id:設置ResultMap的id --> <!-- 定義主鍵 ,非常重要。如果是多個字段,則定義多個id --> <!-- property:主鍵在pojo中的屬性名 --> <!-- column:主鍵在數(shù)據(jù)庫中的列名 --> <id column="id" property="id" /> <!-- 映射其他普通列 --> <result column="user_id" property="userId" /> <result property="number" column="number" /> <result property="createtime" column="createtime" /> </resultMap> <!-- 方法的返回值可以使用 --> <select id="queryAll" resultMap="orderMap"> SELECT id,user_id ,number,createtime,note FROM orders </select> </mapper>
單元測試:
public class OrderMapperTest { SqlSessionFactory factory = null; private OrderMapper orderMapper = null; @Before public void testInit() { // 1. 創(chuàng)建SqlSessionFactoryBuilder對象 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); // 2. 加載SqlMapConfig.xml配置文件 // /20181013_mybatis/config/SqlMapConfig.xml InputStream in = MyBatisTest.class.getResourceAsStream("/SqlMapConfig.xml"); // 3. 創(chuàng)建SqlSessionFactory對象 factory = builder.build(in); } @Test public void testqueryAll() { SqlSession session = factory.openSession(); OrderMapper orderMapper = session.getMapper(OrderMapper.class); List<Order> orders = orderMapper.queryAll(); System.out.println(orders); } }
以上這篇解決mybatis一對多關聯(lián)查詢多條數(shù)據(jù)只顯示一條的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
在IntelliJ IDEA中多線程并發(fā)代碼的調試方法詳解
這篇文章主要介紹了在IntelliJ IDEA中多線程并發(fā)代碼的調試方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08SpringBoot部署到Linux讀取resources下的文件及遇到的坑
本文主要給大家介紹SpringBoot部署到Linux讀取resources下的文件,在平時業(yè)務開發(fā)過程中,很多朋友在獲取到文件內容亂碼或者文件讀取不到的問題,今天給大家分享小編遇到的坑及處理方案,感興趣的朋友跟隨小編一起看看吧2021-06-06Spring?Cloud?Ribbon?中的?7?種負載均衡策略的實現(xiàn)方法
Ribbon?內置了?7?種負載均衡策略:輪詢策略、權重策略、隨機策略、最小連接數(shù)策略、重試策略、可用性敏感策略、區(qū)域性敏感策略,并且用戶可以通過繼承?RoundRibbonRule?來實現(xiàn)自定義負載均衡策略,對Spring?Cloud?Ribbon負載均衡策略相關知識感興趣的朋友一起看看吧2022-03-03Java實現(xiàn)注冊登錄與郵箱發(fā)送賬號驗證激活功能
這篇文章主要介紹了Java實現(xiàn)注冊登錄與郵箱發(fā)送賬號驗證激活功能,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧2022-12-12Java中Integer的parseInt和valueOf的區(qū)別詳解
這篇文章主要介紹了Java中Integer的parseInt和valueOf的區(qū)別詳解,nteger.parseInt(s)是把字符串解析成int基本類型,Integer.valueOf(s)是把字符串解析成Integer對象類型,其實int就是Integer解包裝,Integer就是int的包裝,需要的朋友可以參考下2023-11-11