Note that there are some explanatory texts on larger screens.

plurals
  1. POMyBatis select statement returns null values
    primarykey
    data
    text
    <p>I'm trying to run a simple MyBatis example, selecting all rows from the "trains" table.</p> <p>The problem is that the query performs, but it returns a list with the correct number of elements, but populated with null values. The same query runned directly with JDBC PreparedStatement works fine.</p> <p>Perhaps it's a configuration problem, but I cannot figure out what I'm doing wrong.</p> <p>Here is the code. Thanks in advance.</p> <p><em>Train.java</em></p> <pre><code>package org.example.mybatis.domain; public class Train implements Serializable { private int id; private String type; // getters and setters } </code></pre> <p><em>TrainMapper.java</em></p> <pre><code>package org.example.mybatis.persistence; public interface TrainMapper { List&lt;Train&gt; getAllTrains(); } </code></pre> <p><em>TrainSelector.java</em></p> <pre><code>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&lt;Train&gt; getAllTrains() { List&lt;Train&gt; 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&lt;Train&gt; trains = null; TrainSelector trainSelector = new TrainSelector(); trains = trainSelector.getAllTrains(); System.out.println(trains); } } </code></pre> <p><em>mybatis-config.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"&gt; &lt;configuration&gt; &lt;properties resource="database.properties" /&gt; &lt;typeAliases&gt; &lt;typeAlias alias="Train" type="org.example.mybatis.domain.Train" /&gt; &lt;!--package name="org.example.mybatis.domain" /&gt;--&gt; &lt;/typeAliases&gt; &lt;environments default="development"&gt; &lt;environment id="development"&gt; &lt;transactionManager type="JDBC" /&gt; &lt;dataSource type="POOLED"&gt; &lt;property name="driver" value="${database.driver}" /&gt; &lt;property name="url" value="${database.url}" /&gt; &lt;property name="username" value="${database.username}" /&gt; &lt;property name="password" value="${database.password}" /&gt; &lt;/dataSource&gt; &lt;/environment&gt; &lt;/environments&gt; &lt;mappers&gt; &lt;mapper resource="org/example/mybatis/persistence/TrainMapper.xml" /&gt; &lt;/mappers&gt; &lt;/configuration&gt; </code></pre> <p><em>TrainMapper.xml</em></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"&gt; &lt;mapper namespace="org.example.mybatis.persistence.TrainMapper"&gt; &lt;cache /&gt; &lt;select id="getAllTrains" parameterType="list" resultType="Train"&gt; SELECT * FROM trains &lt;/select&gt; &lt;/mapper&gt; </code></pre> <p><em>JdbcStatementExample.java</em></p> <pre><code>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(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload