MyBatis的select语句返回空值(MyBatis select statement retu

2019-08-04 07:11发布

我想运行一个简单的MyBatis例如,从“火车”选择表中的所有行。

问题是,查询执行,但它返回一个列表具有正确数量的元素,但与空值填充。 直接与JDBC的PreparedStatement拼命地跑同样的查询工作正常。

也许这是一个配置问题,但我想不出我做错了什么。

下面是代码。 提前致谢。

Train.java

package org.example.mybatis.domain;

public class Train implements Serializable
{
private int id;
private String type;

    // getters and setters
}

TrainMapper.java

package org.example.mybatis.persistence;

public interface TrainMapper {

List<Train> getAllTrains();
}

TrainSelector.java

package org.example.mybatis.test;

public class TrainSelector implements TrainMapper {

    private static String resource = "mybatis-config.xml";
    private static SqlSessionFactory factory = null;

    private SqlSessionFactory getSqlSessionFactory()
    {
        if (factory == null)
        {
            try {
                InputStream inputStream = Resources.getResourceAsStream(resource);
                factory = new SqlSessionFactoryBuilder().build(inputStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
         }
         return factory;
    }

    @Override
    public List<Train> getAllTrains()
    {
        List<Train> trains = null;

        SqlSession session = getSqlSessionFactory().openSession();
        try {
            TrainMapper mapper = session.getMapper(TrainMapper.class);
            trains = mapper.getAllTrains();
        } finally {
            session.close();
        }   
        return trains;
    }

    public static void main(String[] args) {
        List<Train> trains = null;

        TrainSelector trainSelector = new TrainSelector();
        trains = trainSelector.getAllTrains();

        System.out.println(trains);
    }

}

MyBatis的-config.xml中

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
   PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>
  <properties resource="database.properties" />

  <typeAliases>
    <typeAlias alias="Train" type="org.example.mybatis.domain.Train" />
    <!--package name="org.example.mybatis.domain" />-->
  </typeAliases>

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

  <mappers>
    <mapper resource="org/example/mybatis/persistence/TrainMapper.xml" />
  </mappers>
</configuration>

TrainMapper.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="org.example.mybatis.persistence.TrainMapper">

  <cache />

  <select id="getAllTrains" parameterType="list" resultType="Train">
    SELECT * 
    FROM trains
  </select>
</mapper>

JdbcStatementExample.java

package org.example.mybatis.test;

public class JdbcStatementExample {

    private static void selectAllTrains() throws SQLException
    {
        String sql = "SELECT * FROM trains";
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://localhost/testing";
        String user = "test";
        String password = "test";

        try {
            conn = DriverManager.getConnection(url, user, password);
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();

            while (rs.next()) {
                String id = rs.getString("train_id");
                String type = rs.getString("train_type");
                System.out.println("id: " + id);
                System.out.println("type: " + type);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            if (ps != null) {
                ps.close();
            }
            if (conn != null) {
                conn.close();
            }
        }
    }

    public static void main(String[] args)
    {
        try {
            selectAllTrains();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Answer 1:

在结果集中的列的名称是从火车对象的属性的名称不同。 你需要一个明确的结果映射,让MyBatis的知道哪些列被映射到哪个属性。

<resultMap id="trainMap" type="Train>
        <id property="id" column="train_id" javaType="java.lang.Integer" jdbcType="INTEGER"/>
        <result property="type" column="train_type" javaType="java.lang.String" jdbcType="VARCHAR"/>
</resultMap>

让您的选择元素成

<select id="getAllTrains" parameterType="list" resultType="trainMap">
    SELECT * FROM trains
</select>


Answer 2:

另一种选择是使用列名的别名。

列名会是你的数据库的和别名将被设置为配合您的火车对象属性:

<select id="getAllTrains" parameterType="list" resultType="trainMap">
  SELECT 
  train_id as id,
  train_type as type
  FROM trains
</select>


文章来源: MyBatis select statement returns null values
标签: jdbc mybatis