簡體   English   中英

MyBatis Java-查詢未返回數據?

[英]MyBatis Java - query not returning data?

我試圖在使用MyBatis的應用程序上運行一些JUnit測試。 但是,查詢按預期返回數據時遇到一些問題。 我嘗試使用多個表,但都不返回數據。

示例測試用例:

public class PLUMapperTest {

    private SqlSessionFactory sessionFactory;
    private static final String RES_PATH = "mybatis-config.xml";

    @Before
    public void setup() throws IOException {
        var inputStream = Resources.getResourceAsStream(RES_PATH);
        this.sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

    }

    @Test
    public void testAllAvailable() {
        try (var session = this.sessionFactory.openSession()) {
            var mapper = session.getMapper(PLUMapper.class);
            var activePLUs = mapper.selectAllAvailable();
            assertThat(activePLUs.size()).isGreaterThan(0);
            for (var plu : activePLUs) {
                assertThat(plu.getPluNum()).isNotBlank();
                assertThat(plu.getCategoryUID()).isGreaterThan(0);
                assertThat(plu.getShortName()).isNotBlank();
                assertThat(plu.getTypeUID()).isGreaterThan(0);
                assertThat(plu.getUnavailable()).isFalse();
            }

        }
    }

}

測試案例失敗,因為“ activePLU”映射沒有數據。 與數據庫的連接成功,並且我可以(通過psql)驗證數據確實在數據庫中。

我的映射器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.midamcorp.data.mapper.PLUMapper">

    <select id="selectAllAvailable" resultType="PLU">
    SELECT * FROM plu WHERE unavailable = FALSE
    </select>

    <select id="selectActiveByTypeUID" parameterType="int" resultType="PLU">
    SELECT * FROM plu WHERE type_uid = #{id} AND unavailable = FALSE
    </select>

    <select id="selectActiveByCategoryUID" parameterType="int" resultType="PLU">
    SELECT * FROM plu WHERE category_uid = #{id} AND unavailable = FALSE
    </select>

</mapper>

...以及相應的界面:

public interface PLUMapper {

    public List<PLU> selectAllAvailable();

    public List<PLU> selectActiveByTypeUID(int id);

    public List<PLU> selectActiveByCategoryUID(int id);
}

我試圖在語句中刪除WHERE子句,以防出現問題,但沒有成功。 我還嘗試在界面上使用注釋(@Select(“ ..”))。

MyBatis配置文件(為簡便起見,省略了一些行):

<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">


<configuration>
    <properties resource="db-config.properties">
    </properties>
    <typeAliases>

        <typeAlias alias="PLU" type="com.midamcorp.data.model.PLU" />

    </typeAliases>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC" />
            <dataSource type="POOLED">
                <property name="driver" value="${driver}" />
                <property name="url" value="${url}" />
                <property name="username" value="${username}" />
                <property name="password" value="${password}" />
            </dataSource>
        </environment>
    </environments>
    <mappers>

        <mapper resource="PLUMapper.xml" />

    </mappers>
</configuration>

我的PLU課程(更少的獲取器和設置器)

package com.midamcorp.data.model;

public class PLU {

    private String pluNum;
    private int typeUID;
    private int categoryUID;
    private String shortName;
    private double price;
    private boolean unavailable;

}

我以前使用過MyBatis,但從未遇到過此問題。 任何建議表示贊賞。

編輯:

有關表格的架構:

CREATE TABLE public.plu (
    plunum varchar NOT NULL,
    type_uid int4 NOT NULL,
    category_uid int4 NOT NULL,
    short_name varchar NOT NULL,
    price numeric(5,2) NOT NULL,
    unavailable bool NOT NULL DEFAULT false,
    CONSTRAINT plu_pkey PRIMARY KEY (plunum),
    CONSTRAINT plu_category_uid_fkey FOREIGN KEY (category_uid) REFERENCES plu_categories(plu_categories_uid),
    CONSTRAINT plu_type_uid_fkey FOREIGN KEY (type_uid) REFERENCES plu_types(plu_type_uid)
);

請注意,查看查詢后,我更新了PLU類中的“ plunum”,“ categoryUID”和“ typeUID”屬性。

編輯:更新的映射文件:

<resultMap id="pluMap"  type="PLU">
 <result property="pluNum" column="plunum"/>
  <result property="typeUID" column="type_uid"/>
    <result property="categoryUID" column="category_uid"/>
   <result property="shortName" column="short_name"/>
    <result property="price" column="price"/>
        <result property="unavailable" column="unavailable"/>
</resultMap>

<select id="selectAvailablePLUs" resultMap="pluMap">
    SELECT * FROM plu WHERE unavailable = FALSE
    </select>

    <select id="selectPLUsByType" parameterType="int" resultType="PLU">
    SELECT * FROM plu WHERE type_uid = #{id} AND unavailable = FALSE
    </select>

    <select id="selectPLUsByCat" parameterType="int" resultType="PLU">
    SELECT * FROM plu WHERE category_uid = #{id} AND unavailable = FALSE
    </select>

</mapper>
Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
PooledDataSource forcefully closed/removed all connections.
Opening JDBC Connection
Created connection 58613637.
Setting autocommit to false on JDBC Connection [org.postgresql.jdbc.PgConnection@37e5f85]
==>  Preparing: SELECT plunum, type_uid, category_uid, short_name, price, unavailable FROM public.plu WHERE unavailable = FALSE 
==> Parameters: 
<==    Columns: plunum, type_uid, category_uid, short_name, price, unavailable
<==        Row: 50000199, 200, 99, DRIVE THRU #1, 0.00, f
<==        Row: 200, 17, 81, Crew Meal 200, 0.00, f
<==        Row: 30000000, 5, 90, CASH, 0.00, f
<==        Row: 30000002, 17, 81, GUEST SATISFACTION, 1.00, f
<==        Row: 20000001, 12, 89, TOTAL   SALES, 0.00, f
<==        Row: 20000002, 11, 86, SUBTOTAL DINE IN, 0.00, f
<==        Row: 20000003, 11, 86, SUBTOTAL TAKE OUT, 0.00, f
<==        Row: 20000004, 11, 86, SUBTOTAL DRIVE THRU, 0.00, f
```

模式上的字段之間的映射不會映射到PLU模型類上的映射。

例如,在表上,您具有字段type_uid,但在PLU類上,它稱為typeUid。 除非您提供幫助,否則MyBatis根本不知道如何進行映射。 您可以將Java類上的字段名稱更改為與表上的字段相同,也可以使用MyBatis TypeAlias(更好的解決方案),有關如何使用TypeAlias的信息,請參見此處http:// www .mybatis.org / mybatis-3 / sqlmap-xml.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM