温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

MyBatis中出现列名与属性名不匹配如何解决

发布时间:2020-12-01 15:32:01 来源:亿速云 阅读:518 作者:Leah 栏目:开发技术

这期内容当中小编将会给大家带来有关MyBatis中出现列名与属性名不匹配如何解决,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

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的标签,就是让咱们来完成返回结果的关系对应的,使用方式如下:

MyBatis中出现列名与属性名不匹配如何解决

注意:主键设置需要单独配置 如: <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 - 实体类的属性名和数据库列名不一致时的两种解决办法!

问题:两者不一致时 , 查询结果无法封装到实体!(也就无法查询出来)

MyBatis中出现列名与属性名不匹配如何解决

① 查询的sql语句中使用别名进行查询.

但要注意: 字段名的别名 要和 实体类的属性名一致!

MyBatis中出现列名与属性名不匹配如何解决

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中出现列名与属性名不匹配如何解决了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI