【MyBatis】表中字段名称与实体类中属性名称不一致
本文最后更新于 525 天前,其中的信息可能已经有所发展或是发生改变。

1.创建实体类(数据库也要对应创建)。
[sourcecode language=”java” title=”Goods.java”]
package com.bean;

public class Goods {
private Integer goodsId;
private String goodsName;
private Double goodsPrice;
private Integer goodsNum;
private Integer goodsType;

public Goods() {
}

public Goods(Integer goodsId, String goodsName, Double goodsPrice,
Integer goodsNum, Integer goodsType) {
super();
this.goodsId = goodsId;
this.goodsName = goodsName;
this.goodsPrice = goodsPrice;
this.goodsNum = goodsNum;
this.goodsType = goodsType;
}

public Integer getGoodsId() {
return goodsId;
}

public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}

public String getGoodsName() {
return goodsName;
}

public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}

public Double getGoodsPrice() {
return goodsPrice;
}

public void setGoodsPrice(Double goodsPrice) {
this.goodsPrice = goodsPrice;
}

public Integer getGoodsNum() {
return goodsNum;
}

public void setGoodsNum(Integer goodsNum) {
this.goodsNum = goodsNum;
}

public Integer getGoodsType() {
return goodsType;
}

public void setGoodsType(Integer goodsType) {
this.goodsType = goodsType;
}

@Override
public String toString() {
return "Goods [goodsId=" + goodsId + ", goodsName=" + goodsName
+ ", goodsPrice=" + goodsPrice + ", goodsNum=" + goodsNum
+ ", goodsType=" + goodsType + "]";
}

}
[/sourcecode]
2.对应的xml文件(重点修改处)。
[sourcecode language=”xml” title=”Goods.xml”]
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bean.Goods">
<select id="findAll" resultMap="resultGoods">
select * from goods
</select>
<resultMap type="com.bean.Goods" id="resultGoods">
<id property="goodsId" column="goods_id" javaType="int" />
<result property="goodsName" column="goods_name" javaType="java.lang.String" />
<result property="goodsPrice" column="goods_price" javaType="java.lang.Double" />
<result property="goodsNum" column="goods_num" javaType="int" />
<result property="goodsType" column="goods_type" javaType="int" />
</resultMap>
</mapper>
[/sourcecode]
3.在mybatis.xml注册Goods.xml。
[sourcecode language=”xml” title=”在mybatis.xml”]
<mapper resource="com/bean/Goods.xml" />
[/sourcecode]
4.测试运行。

项目地址:[bdbtn]https://ge.benzhu.xyz/?/代码/源代码/MyBatis/源代码/MB002.rar[/bdbtn]
数据库:[bdbtn]https://ge.benzhu.xyz/?/代码/源代码/MyBatis/数据库/spring2.sql[/bdbtn]

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇