解決MyBatis中為類配置別名,列名與屬性名不對應的問題
在傳參與接收返回結果的時候,咱們一直是使用的全限定名。但是MyBatis自己在使用很多型別的時候(如Integer,Boolean)卻可以直接使用別名。那麼,咱們自己的寫的類能不能使用別名呢?可以。需要配置。
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"> <!-- 完成一個mybatis-config.xml的檔案 -> 作用:配置連線資料庫的所有需要的環境 必須連線到所有要使用的對映檔案(ProductMapper.xml) --> <!--configuration 根目錄 --> <configuration> <!-- 引入(關聯)db.properties檔案 --> <properties resource="db.properties"></properties> <!-- 配置別名:在MyBatis中為一個類取別名 配置別名是為了在物件對映檔案中接收引數型別和返回引數型別時使用--> <typeAliases> <!-- 設定這個包下面的所有類的別名 <package name="cn.itsource.domain"/> --> <!-- 設定單個類的別名 alias:取的別名 type:這個別名所對應的Java類 別名使用的時候與大小寫無關 --> <typeAlias alias="Product" type="cn.itsource.domain.Product"/> </typeAliases> <!-- 環境們:很多環境 default:表示預設使用哪一個環境--> <environments default="development"> <!-- 單個環境:一個環境 id:表示這個環境的名稱--> <environment id="development"> <!-- transactionManager:事務管理器 (使用的JDBC事務管理器)--> <transactionManager type="JDBC"></transactionManager> <!-- MyBatis自帶POOLED連線池(資料來源) --> <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)必須與所有的物件對映檔案(ProductMapper.xml)關聯!!!! --> <mappers> <mapper resource="cn/itsource/domain/ProductMapper.xml" /> </mappers> </configuration>
上面配置了別名,那麼物件與對映檔案中就可以直接使用別名,而不需要使用全限定名稱
<?xml version="1.0" encoding="UTF-8"?> <!-- 完成一個物件關係對映檔案 -> 作用:一個物件的所有SQL都應該寫在這個對映檔案中 這個檔案一般和我們的domain寫在同一個包裡面,取名為 -> domain的名稱+Mapper.xml --> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!-- namespace:名稱空間(每個Mapper必須有名稱空間) --> <mapper namespace="cn.itsource.domain.ProductMapper"> <!-- select:它裡面寫查詢語句 id:查詢語句的唯一標識(名稱不能重複) 如何在 java程式碼中找到sql語句? 名稱空間+id 例子:cn.itsource.domain.ProductMapper.select parameterType:傳入的引數型別。 除了MyBatis支援的型別,其它的型別都通通使用全限定名 resultType:返回的每一條資料的結果型別(結果型別寫許可權定名稱 ) 查詢功能使用 --> <select id="selectOne" parameterType="Long" resultType="Product"> select * from product where id = #{id} </select> <!-- resultType:表示返回的資料型別 --> <select id="selectAll" resultType="Product"> select * from Product </select> <!--parameterType :表示傳入引數(Product)。 useGeneratedKeys:是否需要獲取主鍵 keyColumn:主鍵在資料庫中的名稱(不寫的話預設名稱和keyProperty一致) keyProperty:物件中的屬性(代表主鍵的那個屬性) --> <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:接收引數沒有寫許可權頂名稱,使用的別名(簡寫) --> <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>
列名與屬性名不對應的解決方案(截圖不完整)
做對映檔案的時候,只做了表與物件之間的聯絡。並沒有做列與欄位之間的聯絡。那麼它們之間是怎麼聯絡上的呢?
由於之前咱們的列名與屬性名是一樣的,因此框架進行了自動的識別。
那麼,如果咱們的列名與屬性名不一致了(對應不上),這時候應該怎麼辦呢?這時候需要把哪些列名與屬性名對應上。
在MyBatis中,提供了一個resultMap的標籤,就是讓咱們來完成返回結果的關係對應的,使用方式如下:
注意:主鍵設定需要單獨配置 如: <id column="id" property="id" />
<!-- 返回的資料對映 type:代表是要對映的物件 id:代表唯一(過會我們要拿到它) --> <resultMap type="cn.itsource.domain.Product" id="productMap"> <!-- column:對應的列名 property:對應的屬性名 --> <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>
補充知識:MyBatis - 實體類的屬性名和資料庫列名不一致時的兩種解決辦法!
問題:兩者不一致時,查詢結果無法封裝到實體!(也就無法查詢出來)
① 查詢的sql語句中使用別名進行查詢.
但要注意: 欄位名的別名 要和 實體類的屬性名一致!
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>
注: 如果使用別名,每一個sql語句都需要加別名 (很麻煩)
故: 一般都使用第二種.
② 使用resultMap ★
UserMapper.xml
<mapper namespace="com.jxj.dao.UserDao"> <resultMap id="userResultMap" type="User"> <!-- 主鍵欄位 property: 實體類屬性名. column: 庫中表的列名 javaType: 資料型別. --> <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的屬性值一樣.
測試類: UserMapper.java
@Test public void queryAll() throws IOException { // 1.建立工廠類. InputStream in = Resources.getResourceAsStream("mybatis-config.xml"); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in); // 2.建立sql物件. SqlSession sqlSession = sqlSessionFactory.openSession(); // 3.建立介面的實現類物件. UserMapper mapper = sqlSession.getMapper(UserMapper.class); // 4.呼叫介面中的方法 (代理) List<User> users = mapper.queryAll(); for (User user : users) { System.out.println(user); } sqlSession.close(); in.close(); }
以上這篇解決MyBatis中為類配置別名,列名與屬性名不對應的問題就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。