一、字段名与实体类名不一致
二、使用resultMap实体类
private int id; private String orderNo; private float price;
数据库字段
order_id,order_no, order_price
查询的结果是null<!-- 根据id查询,返回一个order对象 --> <select id="getOrder" parameterType="int" resultType="Order"> select * from orders where order_id= #{id} </select>
@Test public void test() { String sql = "com.weixuan.mybatis.bean.OrderMapper.getOrder"; Order order = sqlsession.selectOne(sql, 1); System.out.println(order); sqlsession.close(); }
<select id="getOrder2" parameterType="int" resultMap="getOrder2map"> select * from orders where order_id= #{id} </select> <!-- 主键使用id,其他的使用result --> <resultMap type="Order" id="getOrder2map"> <id property="id" column="order_id" /> <result property="orderNo" column="order_no" /> <result property="price" column="order_price" /> </resultMap>
@Test public void test2() { String sql = "com.weixuan.mybatis.bean.OrderMapper.getOrder2"; Order order = sqlsession.selectOne(sql, 1); System.out.println(order); sqlsession.close(); }
可以正确的查到数据