There is no getter for property named 'sub_title' in class Goods

There is no getter for property named 'sub_title' in class Goods

https://img1.sycdn.imooc.com//climg/622b05c408e4758824890671.jpg


package com.imooc.mybatis.entity;
public class Goods {
    private Integer goodsId;           //商品编号
    private String title;              //标题
    private String subTitle;           //子标题
    private Float originalCost;        //原始价格
    private Float currentPrice;        //当前价格
    private Float discount;            //
    private Integer isFreeDelivery;    //是否包邮,1-包邮 0-不包邮
    private Integer categoryId;        //分类编号
    public Integer getGoodsId() {
        return goodsId;
    }
    public void setGoodsId(Integer goodsId) {
        this.goodsId = goodsId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getSubTitle() {
        return subTitle;
    }
    public void setSubTitle(String subTitle) {
        this.subTitle = subTitle;
    }
    public Float getOriginalCost() {
        return originalCost;
    }
    public void setOriginalCost(Float originalCost) {
        this.originalCost = originalCost;
    }
    public Float getCurrentPrice() {
        return currentPrice;
    }
    public void setCurrentPrice(Float currentPrice) {
        this.currentPrice = currentPrice;
    }
    public Float getDiscount() {
        return discount;
    }
    public void setDiscount(Float discount) {
        this.discount = discount;
    }
    public Integer getIsFreeDelivery() {
        return isFreeDelivery;
    }
    public void setIsFreeDelivery(Integer isFreeDelivery) {
        this.isFreeDelivery = isFreeDelivery;
    }
    public Integer getCategoryId() {
        return categoryId;
    }
    public void setCategoryId(Integer categoryId) {
        this.categoryId = categoryId;
    }
}


<?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="goods">
<!--namespace有点类似于Java中的package-->
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
<!-- id相当于select语句的别名  -->
        select * from t_goods order by goods_id desc limit 10
    </select>
    <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
        select * from babytun.t_goods where goods_id=#{value}
    </select>
    <insert id="insert" parameterType="com.imooc.mybatis.entity.Goods">
        INSERT INTO t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
        VALUES (#{title},#{sub_title},#{original_cost},#{current_price},#{discount},#{is_free_delivery},#{category_id})
    </insert>
</mapper>
package com.imooc.mybatis;
import com.imooc.mybatis.entity.Goods;
import com.imooc.mybatis.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;
import java.io.IOException;
import java.io.Reader;
import java.sql.Connection;
import java.util.List;
public class MyBatisTestor {

     @Test
     public void insertTest()throws Exception{
         SqlSession session = null;
         try {
             session = MyBatisUtils.openSession();
             Goods goods = new Goods();
             goods.setTitle("test");
             goods.setSubTitle("1234");
             goods.setOriginalCost(2000f);
             goods.setCurrentPrice(1000f);
             goods.setDiscount(0.5f);
             goods.setIsFreeDelivery(1);
             goods.setCategoryId(43);
             int num = session.insert("goods.insert",goods);
             System.out.println(num);
             session.commit();
             System.out.println(goods.getGoodsId());
         }catch (Exception e){
             if(session!=null){
                 session.rollback();
             }
             throw e;
         }finally {
             MyBatisUtils.closeSession(session);
         }
     }
}
package com.imooc.mybatis.utils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.IOException;
import java.io.Reader;
//MyBatisUtils工具类,创建全局唯一的sqlSessionFactory对象
public class MyBatisUtils {
    //利用static属于类不属于对象,且全局唯一
    private static SqlSessionFactory sqlSessionFactory;
    //利用静态块在初始化类时
    static {
        Reader reader;
        try {
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            //如果xml文件名写错了,会出现IOException:Counld not find resouce mybatis1-config.xml
            //也会出现NoClassDefFoundError:Could not initialize class com.imooc.mybatis.utils.MyBatisUtils
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
        } catch (IOException e) {
            e.printStackTrace();
            //初始化错误时,通过抛出异常ExceptionInInitializerError通知调用者
            throw new ExceptionInInitializerError(e);
        }
    }
    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
    public static void closeSession(SqlSession session){
        if(session != null) {
            session.close();
        }
    }
}
<?xml version="1.0" encoding="UTF-8" ?>
<!--文件名可以任意,文件类型一定要是xml文件-->
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!--mybatis.org官网-入门里copy dtd -->
<configuration>
    <settings>
        <!--可以把数据库中的字段如goods_id ==>goodsId -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
    <environments default="dev">
    <!--default属性设置默认使用的数据库-->
        <environment id="dev">
        <!--采用JDBC对数据库对事务commit/rollback-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
            <!--使用连接池对数据进行管理-->
                <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/school?useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="abc123456"/>
            </dataSource>
        </environment>
        <environment id="prd">
            <!--采用JDBC对数据库对事务commit/rollback-->
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://192.168.1.155:3306/babytun?useUnicode=true&amp;characterEncoding=utf-8"/>
                <property name="username" value="root"/>
                <property name="password" value="abc123456"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="mappers/goods.xml"/>
<!--<mapper resource="mappers/student.xml"/>-->
    </mappers>
</configuration>
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.imooc</groupId>
    <artifactId>mybatis</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories>
        <repository>
            <id>aliyun</id>
            <name>aliyun</name>
            <url>https://maven.aliyun.com/repository/public</url>
        </repository>
    </repositories>
    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.9</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>
    </dependencies>
</project>
<?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="goods">
<!--namespace有点类似于Java中的package-->
    <select id="selectAll" resultType="com.imooc.mybatis.entity.Goods">
<!-- id相当于select语句的别名  -->
        select * from t_goods order by goods_id desc limit 10
    </select>
    <select id="selectById" parameterType="Integer" resultType="com.imooc.mybatis.entity.Goods">
        select * from babytun.t_goods where goods_id=#{value}
    </select>
    <insert id="insert" parameterType="com.imooc.mybatis.entity.Goods">
        INSERT INTO t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)
        VALUES (#{title},#{sub_title},#{original_cost},#{current_price},#{discount},#{is_free_delivery},#{category_id})
    </insert>
</mapper>


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小脸 2022-03-11 16:33:50

同学你好,老师在另一个问答中回复你了,记得去看

http://class.imooc.com/course/qadetail/319322

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师