Connected to the target VM, address: '127.0.0.1:52612', transport: 'socket'

Connected to the target VM, address: '127.0.0.1:52612', transport: 'socket'


我的mysql数据库端口号明明是3306 为什么这里报的异常地址是target VM, address: '127.0.0.1:52612', transport: 'socket' 这是什么意思?

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

<?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>
<?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>

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;
    }
}
    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
  insertTest()Exception{
         SqlSession session = ;
         {
             session = MyBatisUtils.();
             Goods goods = Goods();
             goods.setTitle();
             goods.setSubTitle();
             goods.setOriginalCost();
             goods.setCurrentPrice();
             goods.setDiscount();
             goods.setIsFreeDelivery();
             goods.setCategoryId();

             num = session.insert(,goods);
             System..println(num);
             session.commit();
             System..println(goods.getGoodsId());
         }(Exception e){
             (session!=){
                 session.rollback();
             }
             e;
         }{
             MyBatisUtils.(session);
         }
     }
}
<?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>

正在回答

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

2回答

同学你好

1、values中值要与实体类字段匹配才可以,由于开启了驼峰命名,实体类中不存在下划线的,修改代码如下所示: 
https://img1.sycdn.imooc.com//climg/622b081b089f951806500052.jpg

 2、同学也要学会根据异常报错信息去查找问题哟~在实际工作中,这是非常重要的~

祝学习愉快~

  • 晓舟 提问者 #1

    之前做作业新建了school的数据表 但是这里为什么会出现 Cause: java.sql.SQLSyntaxErrorException: Table 'school.t_goods' doesn't exist### 

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

    org.apache.ibatis.exceptions.PersistenceException: 

    ### Error updating database.  Cause: java.sql.SQLSyntaxErrorException: Table 'school.t_goods' doesn't exist

    ### The error may exist in mappers/goods.xml

    ### The error may involve goods.insert-Inline

    ### The error occurred while setting parameters

    ### SQL: INSERT INTO t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)         VALUES (?,?,?,?,?,?,?)

     Cause: java.sql.SQLSyntaxErrorException: Table 'school.t_goods' doesn't exist###


    at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:196)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:181)

    at com.imooc.mybatis.MyBatisTestor.insertTest(MyBatisTestor.java:93)

    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)

    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

    at java.lang.reflect.Method.invoke(Method.java:498)

    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59)

    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)

    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56)

    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)

    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)

    at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100)

    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103)

    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63)

    at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)

    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)

    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)

    at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)

    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)

    at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)

    at org.junit.runners.ParentRunner.run(ParentRunner.java:413)

    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)

    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)

    at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)

    at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)

    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)

    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)

    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)

    Caused by: java.sql.SQLSyntaxErrorException: Table 'school.t_goods' doesn't exist

    at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120)

    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)

    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)

    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)

    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)

    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)

    at org.apache.ibatis.executor.SimpleExecutor.doUpdate(SimpleExecutor.java:50)

    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)

    at org.apache.ibatis.executor.CachingExecutor.update(CachingExecutor.java:76)

    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:194)

    ... 29 more



    Process finished with exit code -1


    2022-03-11 18:00:54
  • 好帮手慕小脸 回复 提问者 晓舟 #2

    同学你好,

    1、school数据库中没有t_goods的表,所以就会出现这个报错,你可以自己检查一下

    2、根据同学的截图反馈,可以看出babytun数据库中是存在该表的

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

    所以同学应该将配置文件的如下位置处修改为:

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

    注:同学反馈的几次报错信息都是可以根据异常去定位问题的哟,同学要学会自己解决问题,若实在不能解决可以再问老师

    祝学习愉快~


    2022-03-11 18:09:58
  • 晓舟 提问者 回复 好帮手慕小脸 #3

    太菜了,我能看得懂是不存在这个表的意思。但是去数据库里看了 明明这个表是存在的。这个解析器也没法直接指出 是在xml里的配置错了,晕了。

    2022-03-11 18:43:50
好帮手慕小脸 2022-03-11 14:38:00

同学你好,

1、要查看如下这里的报错异常才可以哟~与同学指出来的那部分内容没有关系

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

2、根据报错信息定位问题出现的具体问题,从而追踪到xml文件中,发现同学的goods.xml文件缺少一个#{},使得两个字段连在一起了

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

3、建议同学下次将代码java代码与pom.xml文件区分开来,上述贴出的内容有乱码问题~不便于老师测试

祝学习愉快~

  • 提问者 晓舟 #1
    <?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>
    • Still throw exception...

    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;
        }
    }
    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 {
    //    public void testMyBatiosUtils() throws Exception {
    //        SqlSession sqlSession = null;
    //        try {
    //            sqlSession = MyBatisUtils.openSession();
    //            Connection conn = sqlSession.getConnection();
    //            System.out.println(conn);
    //
    //        }catch (Exception e){
    //            throw e;
    //        }finally {
    //            MyBatisUtils.closeSession(sqlSession);
    //        }
    //
    //
    //    }
    //@Test
    //    public void testSqlSessionFactory() throws IOException {
    //        //利用Reader加载classpath下的mybatis-config.xml核心配置文件
    //        Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
    //        //初始化SqlSession对象,同时解析mybatis-config.xml文件
    //        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
    //        //对mybatis进行了初始化操作
    //        System.out.println("SessionFactory Loading successful");
    //        SqlSession sqlSession = null;
    //        try {
    //            //创建SqlSession对象,SqlSession是JDBC扩展类,用于与数据库交互
    //            sqlSession = sqlSessionFactory.openSession();
    //            //创建数据库连接(测试用)这个语句非必须,正常开发时mybatis会自动帮我们完成
    //            Connection conn = sqlSession.getConnection();
    //            System.out.println(conn);
    //        }catch (Exception e){
    //            e.printStackTrace();
    //        }finally {
    //            if(sqlSession != null){
    //                sqlSession.close();
    //            }
    //        }
    //    }
    //    @Test
    // public void testSelectAll(){
    //     SqlSession session=null;
    //     session = MyBatisUtils.openSession();
    //     List<Goods> list= session.selectList("goods.selectAll");
    //     for(Goods goods:list){
    //         System.out.println(goods.getTitle());
    //     }
    // }
    // public void testSelectById() throws Exception {
    //     SqlSession session = null;
    //     try {
    //         session = MyBatisUtils.openSession();
    //         Goods goods = session.selectOne("goods.selectById", 1603);
    //         System.out.println(goods.getTitle());
    //     }catch (Exception e){
    //         throw e;
    //     }finally {
    //         MyBatisUtils.closeSession(session);
    //     }
    //     }
         @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);
             }
         }
    }


    There is no getter for property named 'sub_title' in 'class com.imooc.mybatis.entity.Goods'

    "C:\Program Files\Java\jdk1.8.0_162\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar=53603:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\lib\idea_rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit5-rt.jar;C:\Program Files\JetBrains\IntelliJ IDEA 2021.3.2\plugins\junit\lib\junit-rt.jar;C:\Program 

    org.apache.ibatis.exceptions.PersistenceException: 

    ### Error updating database.  Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'sub_title' in 'class com.imooc.mybatis.entity.Goods'

    ### The error may exist in mappers/goods.xml

    ### The error may involve goods.insert-Inline

    ### The error occurred while setting parameters

    ### SQL: INSERT INTO t_goods(title,sub_title,original_cost,current_price,discount,is_free_delivery,category_id)         VALUES (?,?,?,?,?,?,?)

    ### Cause: org.apache.ibatis.reflection.ReflectionException: There is no getter for property named 'sub_title' in 'class com.imooc.mybatis.entity.Goods'




    Process finished with exit code -1


    2022-03-11 15:33:39
问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

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

帮助反馈 APP下载

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

公众号

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

在线咨询

领取优惠

免费试听

领取大纲

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