Mybatis 的问题,我按照原本的格式写一个操作为什么原先的能成功,而我的不管怎样都会失败呢?

2018-10-30 14:03:27 +08:00
 ukipoi

这个项目 Dao 是单独一个项目,然后打成 jar 包添加到 maven。在后台项目里从 maven 引入的。 我想在 Dao 里加一个操作,但是就很简单的查询也会失败。 模仿的样例:
AppUserMapper.java

public interface AppUserMapper {
    //......
    AppUser selectByPrimaryKey(String id);
    //......
}

AppUserMapper.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="XXX.dao.AppUserMapper">
  <resultMap id="BaseResultMap" type="XXX.entity.AppUser">
    <!--实体类映射-->
  </resultMap>

  <sql id="Base_Column_List">
    <!--查询的值-->
  </sql>
  <!--省略-->
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from app_user
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <!--省略-->
</mapper>

自己写的:
GoodsOff.java

package XXX.entity;

import java.io.Serializable;

public class GoodsOff implements Serializable {
    private int id;
    private String goodsId;
    private float off;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getGoodsId() {
        return goodsId;
    }

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

    public float getOff() {
        return off;
    }

    public void setOff(float off) {
        this.off = off;
    }
}

GoodsOffMapper.java

package XXX.dao;

import XXX.entity.GoodsOff;

public interface GoodsOffMapper {
    GoodsOff selectByPrimaryKey(String goodsId);
}

GoodsOffMapper.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="XXX.dao.GoodsOffMapper">
  <resultMap id="BaseResultMap" type="XXX.entity.GoodsOff">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="goods_id" jdbcType="VARCHAR" property="goodsId" />
    <result column="off" jdbcType="FLOAT" property="off" />
  </resultMap>
  <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap">
    select
    *
    from hbmm_goodsoff
    where goods_id = #{goodsId,jdbcType=VARCHAR}
  </select>
</mapper>

我数据库里的表:

REATE TABLE `hbmm_goodsoff` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `goods_id` varchar(64) CHARACTER SET utf8mb4 DEFAULT NULL,
  `off` float DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

2079 次点击
所在节点    Java
7 条回复
xiaoxinshiwo
2018-10-30 14:10:18 +08:00
报错信息贴一下啊大胸弟
Starry
2018-10-30 14:10:38 +08:00
是不是包名路径不一样,自动扫描扫不到你的 DAO 和 XML 文件
ukipoi
2018-10-30 14:24:36 +08:00
@xiaoxinshiwo
不好意思,错误信息是下面这样的</br>
```error
十月 30, 2018 2:22:44 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [mvc] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bar.hbmm_goodsoff' doesn't exist
### The error may exist in class path resource [com/youzhuo/bar/mapping/mybatis/GoodsOffMapper.xml]
### The error may involve com.youzhuo.bar.dao.mybatis.GoodsOffMapper.selectByPrimaryKey-Inline
### The error occurred while setting parameters
### SQL: select * from hbmm_goodsoff where goods_id = ?
### Cause: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bar.hbmm_goodsoff' doesn't exist
; bad SQL grammar []; nested exception is com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bar.hbmm_goodsoff' doesn't exist] with root cause
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'bar.hbmm_goodsoff' doesn't exist
```
gejun123456
2018-10-30 14:32:08 +08:00
不是表不存在么,数据库连接问题? 连到你设置的数据库去看看吧
ukipoi
2018-10-30 14:40:53 +08:00
@gejun123456
真的是眼瞎了,全局搜索了 jdbc.url 才发现原先的项目有 3 个地方配置了数据库连接,我只改了 1 个。
谢谢回复
519718366
2018-10-30 14:44:02 +08:00
bar 库下面有这个 hbmm_goodsoff 表?要么你连到其他库去了,要么就是名字打错了什么的?
xiaoxinshiwo
2018-10-30 15:13:49 +08:00
@ukipoi #5 所以发贴记得贴异常啊

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/502551

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX