首页 > 编程语言 > 解决MyBatis中为类配置别名,列名与属性名不对应的问题
2020
12-02

解决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自??OOLED连接池(数据源) -->
 <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中为类配置别名,列名与属性名不对应的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持自学编程网。

编程技巧