解決MyBatis中為類(lèi)配置別名,列名與屬性名不對(duì)應(yīng)的問(wèn)題
在傳參與接收返回結(jié)果的時(shí)候,咱們一直是使用的全限定名。但是MyBatis自己在使用很多類(lèi)型的時(shí)候(如Integer,Boolean)卻可以直接使用別名。那么,咱們自己的寫(xiě)的類(lèi)能不能使用別名呢?可以。需要配置。
mybatis配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--
完成一個(gè)mybatis-config.xml的文件
-> 作用:配置連接數(shù)據(jù)庫(kù)的所有需要的環(huán)境
必須連接到所有要使用的映射文件(ProductMapper.xml)
-->
<!--configuration 根目錄 -->
<configuration>
<!-- 引入(關(guān)聯(lián))db.properties文件 -->
<properties resource="db.properties"></properties>
<!-- 配置別名:在MyBatis中為一個(gè)類(lèi)取別名 配置別名是為了在對(duì)象映射文件中接收參數(shù)類(lèi)型和返回參數(shù)類(lèi)型時(shí)使用-->
<typeAliases>
<!--
設(shè)置這個(gè)包下面的所有類(lèi)的別名
<package name="cn.itsource.domain"/>
-->
<!--
設(shè)置單個(gè)類(lèi)的別名 alias:取的別名 type:這個(gè)別名所對(duì)應(yīng)的Java類(lèi) 別名使用的時(shí)候與大小寫(xiě)無(wú)關(guān)
-->
<typeAlias alias="Product" type="cn.itsource.domain.Product"/>
</typeAliases>
<!-- 環(huán)境們:很多環(huán)境 default:表示默認(rèn)使用哪一個(gè)環(huán)境-->
<environments default="development">
<!-- 單個(gè)環(huán)境:一個(gè)環(huán)境 id:表示這個(gè)環(huán)境的名稱(chēng)-->
<environment id="development">
<!-- transactionManager:事務(wù)管理器 (使用的JDBC事務(wù)管理器)-->
<transactionManager type="JDBC"></transactionManager>
<!-- MyBatis自帶POOLED連接池(數(shù)據(jù)源) -->
<dataSource type="POOLED">
<property name="driver" value="${db_driverClassname}" />
<property name="url" value="${db_url}" />
<property name="username" value="${db_username}" />
<property name="password" value="${db_password}" />
</dataSource>
</environment>
</environments>
<!-- resource:表示 核心配置文件(mybatis-config.xml)必須與所有的對(duì)象映射文件(ProductMapper.xml)關(guān)聯(lián)?。。?! -->
<mappers>
<mapper resource="cn/itsource/domain/ProductMapper.xml" />
</mappers>
</configuration>
上面配置了別名,那么對(duì)象與映射文件中就可以直接使用別名,而不需要使用全限定名稱(chēng)
<?xml version="1.0" encoding="UTF-8"?>
<!-- 完成一個(gè)對(duì)象關(guān)系映射文件 ->
作用:一個(gè)對(duì)象的所有SQL都應(yīng)該寫(xiě)在這個(gè)映射文件中 這個(gè)文件一般和我們的domain寫(xiě)在同一個(gè)包里面,取名為
-> domain的名稱(chēng)+Mapper.xml -->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace:命名空間(每個(gè)Mapper必須有命名空間) -->
<mapper namespace="cn.itsource.domain.ProductMapper">
<!--
select:它里面寫(xiě)查詢(xún)語(yǔ)句
id:查詢(xún)語(yǔ)句的唯一標(biāo)識(shí)(名稱(chēng)不能重復(fù))
如何在 java代碼中找到sql語(yǔ)句? 命名空間+id
例子:cn.itsource.domain.ProductMapper.select
parameterType:傳入的參數(shù)類(lèi)型。 除了MyBatis支持的類(lèi)型,其它的類(lèi)型都通通使用全限定名
resultType:返回的每一條數(shù)據(jù)的結(jié)果類(lèi)型(結(jié)果類(lèi)型寫(xiě)權(quán)限定名稱(chēng) ) 查詢(xún)功能使用
-->
<select id="selectOne" parameterType="Long" resultType="Product">
select * from product where id = #{id}
</select>
<!-- resultType:表示返回的數(shù)據(jù)類(lèi)型 -->
<select id="selectAll" resultType="Product">
select * from Product
</select>
<!--parameterType :表示傳入?yún)?shù)(Product)。
useGeneratedKeys:是否需要獲取主鍵
keyColumn:主鍵在數(shù)據(jù)庫(kù)中的名稱(chēng)(不寫(xiě)的話(huà)默認(rèn)名稱(chēng)和keyProperty一致)
keyProperty:對(duì)象中的屬性(代表主鍵的那個(gè)屬性)
-->
<insert id="save" parameterType="Product"
useGeneratedKeys="true" keyColumn="id" keyProperty="id">
insert into product (productName,dir_id,salePrice,supplier,brand,cutoff,costPrice)
values(#{productName},#{dir_id},#{salePrice},#{supplier},#{brand},#{cutoff},#{costPrice})
</insert>
<!-- parameterType:接收參數(shù)沒(méi)有寫(xiě)權(quán)限頂名稱(chēng),使用的別名(簡(jiǎn)寫(xiě)) -->
<delete id="delete" parameterType="long">
delete from product where id = #{id}
</delete>
<update id="update" parameterType="Product">
update product set productName=#{productName},dir_id=#{dir_id},
salePrice=#{salePrice},supplier=#{supplier},brand=#{brand},cutoff=#{cutoff},costPrice=#{costPrice}
where id = #{id}
</update>
</mapper>
列名與屬性名不對(duì)應(yīng)的解決方案(截圖不完整)
做映射文件的時(shí)候,只做了表與對(duì)象之間的聯(lián)系。并沒(méi)有做列與字段之間的聯(lián)系。那么它們之間是怎么聯(lián)系上的呢?
由于之前咱們的列名與屬性名是一樣的,因此框架進(jìn)行了自動(dòng)的識(shí)別。
那么,如果咱們的列名與屬性名不一致了(對(duì)應(yīng)不上),這時(shí)候應(yīng)該怎么辦呢?這時(shí)候需要把哪些列名與屬性名對(duì)應(yīng)上。
在MyBatis中,提供了一個(gè)resultMap的標(biāo)簽,就是讓咱們來(lái)完成返回結(jié)果的關(guān)系對(duì)應(yīng)的,使用方式如下:

注意:主鍵設(shè)置需要單獨(dú)配置 如: <id column="id" property="id" />
<!--
返回的數(shù)據(jù)映射
type:代表是要映射的對(duì)象
id:代表唯一(過(guò)會(huì)我們要拿到它)
-->
<resultMap type="cn.itsource.domain.Product" id="productMap">
<!--
column:對(duì)應(yīng)的列名
property:對(duì)應(yīng)的屬性名
-->
<id column="id" property="id" />
<result column="productName" property="name" />
</resultMap>
<select id="queryOne" parameterType="long" resultMap="productMap">
select * from product where id = #{id}
</select>
補(bǔ)充知識(shí):MyBatis - 實(shí)體類(lèi)的屬性名和數(shù)據(jù)庫(kù)列名不一致時(shí)的兩種解決辦法!
問(wèn)題:兩者不一致時(shí) , 查詢(xún)結(jié)果無(wú)法封裝到實(shí)體!(也就無(wú)法查詢(xún)出來(lái))

① 查詢(xún)的sql語(yǔ)句中使用別名進(jìn)行查詢(xún).
但要注意: 字段名的別名 要和 實(shí)體類(lèi)的屬性名一致!

UserMapper.xml
<!-- namespace:接口的全路徑名. --> <mapper namespace="com.xxx.dao.UserMapper"> <!-- 使用別名 --> <select id="queryAll" resultType="com.xxx.domain.User"> select id as userId, username as userName, address as userAddress, sex as userSex, birthday as userBirthday from user; </select> </mapper>
注: 如果使用別名 , 每一個(gè)sql語(yǔ)句都需要加別名 (很麻煩)
故: 一般都使用第二種.
② 使用resultMap ★
UserMapper.xml
<mapper namespace="com.jxj.dao.UserDao"> <resultMap id="userResultMap" type="User"> <!-- 主鍵字段 property: 實(shí)體類(lèi)屬性名. column: 庫(kù)中表的列名 javaType: 數(shù)據(jù)類(lèi)型. --> <id property="userId" column="id" javaType="int"></id> <!-- 非主鍵字段 --> <result property="userSex" column="sex" javaType="string"></result> <result property="userAddress" column="address" javaType="string"></result> <result property="userBirthday" column="birthday" javaType="date"></result> <result property="username" column="username" javaType="string"></result> </resultMap> <select id="queryAll" resultMap="userResultMap"> select * from user </select>
注: select中resultMap的屬性值 要和 resultMap中id的屬性值一樣.
測(cè)試類(lèi): UserMapper.java
@Test
public void queryAll() throws IOException {
// 1.創(chuàng)建工廠(chǎng)類(lèi).
InputStream in = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
// 2.創(chuàng)建sql對(duì)象.
SqlSession sqlSession = sqlSessionFactory.openSession();
// 3.創(chuàng)建接口的實(shí)現(xiàn)類(lèi)對(duì)象.
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// 4.調(diào)用接口中的方法 (代理)
List<User> users = mapper.queryAll();
for (User user : users) {
System.out.println(user);
}
sqlSession.close();
in.close();
}
以上這篇解決MyBatis中為類(lèi)配置別名,列名與屬性名不對(duì)應(yīng)的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼
Feign是一個(gè)開(kāi)源的Java HTTP客戶(hù)端,可以幫助我們?cè)赟pringBoot應(yīng)用中快速構(gòu)建和使用HTTP客戶(hù)端,方便實(shí)現(xiàn)服務(wù)間的通信,本文就來(lái)介紹一下SpringBoot使用Feign進(jìn)行服務(wù)間通信的實(shí)現(xiàn)示例代碼,感興趣的可以了解一下2024-01-01
多線(xiàn)程-lock與lockInterruptibly的區(qū)別及說(shuō)明
文章主要討論了Java中ReentrantLock的lock和lockInterruptibly方法的區(qū)別,以及AQS中的雙向鏈表設(shè)計(jì),lock方法不響應(yīng)中斷,而lockInterruptibly方法會(huì)響應(yīng)中斷,AQS的雙向鏈表設(shè)計(jì)使得線(xiàn)程管理更加高效和靈活,適用于高并發(fā)場(chǎng)景2025-02-02
java實(shí)現(xiàn)支付寶支付接口的調(diào)用
本文主要介紹了java實(shí)現(xiàn)支付寶支付接口的調(diào)用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-07-07
Java實(shí)現(xiàn)商城訂單超時(shí)取消功能
大多數(shù)的B2C商城項(xiàng)目都會(huì)有限時(shí)活動(dòng),當(dāng)用戶(hù)下單后都會(huì)有支付超時(shí)時(shí)間,當(dāng)訂單超時(shí)后訂單的狀態(tài)就會(huì)自動(dòng)變成已取消 ,這個(gè)功能的實(shí)現(xiàn)有很多種方法,本文的實(shí)現(xiàn)方法適合大多數(shù)比較小的商城使用。具體實(shí)現(xiàn)方式可以跟隨小編一起看看吧2019-12-12

