标签:
CREATE TABLE orders(
order_id INT PRIMARY KEY AUTO_INCREMENT,
order_no VARCHAR(20),
order_price FLOAT
);
INSERT INTO orders(order_no, order_price) VALUES(‘aaaa‘, 23);
INSERT INTO orders(order_no, order_price) VALUES(‘bbbb‘, 33);
INSERT INTO orders(order_no, order_price) VALUES(‘cccc‘, 22);
public class Order {
private int id;
private String orderNo;
private float price;
}
<!-- 通过查询字段的别名来对应 -->
<select id="getOrder" parameterType="int" resultType="Order">
SELECT order_id id, order_no orderNo, order_price price FROM orders WHERE order_id=#{id}
</select>
@Test
public void test1(){
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test4.orderMapper.getOrder";
Order order = session.selectOne(statement , 2);
System.out.println(order);
}
<resultMap type="Order" id="getOrder2Map">
<id property="id" column="order_id"/>
<result property="orderNo" column="order_no"/>
<result property="price" column="order_price"/>
</resultMap>
<select id="getOrder2" parameterType="int" resultMap="getOrder2Map">
SELECT * FROM orders WHERE order_id=#{id}
</select>
@Test
public void test2(){
SqlSessionFactory factory = MybatisUtils.getFactory();
SqlSession session = factory.openSession();
String statement = "cn.imentors.mybatis.test4.orderMapper.getOrder2";
Order order = session.selectOne(statement , 2);
System.out.println(order);
}
标签:
原文地址:http://www.cnblogs.com/mentorStudio/p/4809487.html