解决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中为类配置别名,列名与属性名不对应的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。