04_ Resolving conflicts where field names differ from entity class property names

Source: Internet
Author: User

1 Preparing tables and data
  
 
  1. CREATE TABLE orders(
  2. order_id INT PRIMARY KEY AUTO_INCREMENT,
  3. order_no VARCHAR(20),
  4. order_price FLOAT
  5. );
  6. INSERT INTO orders(order_no, order_price) VALUES(‘aaaa‘, 23);
  7. INSERT INTO orders(order_no, order_price) VALUES(‘bbbb‘, 33);
  8. INSERT INTO orders(order_no, order_price) VALUES(‘cccc‘, 22);
2 Defining Entity classes: Table fields and entity properties are inconsistent
  
 
  1. public class Order {
  2. private int id;
  3. private String orderNo;
  4. private float price;
  5. }
3 Workaround 3.1 Alias by query
  
 
  1. <!-- 通过查询字段的别名来对应 -->
  2. <select id="getOrder" parameterType="int" resultType="Order">
  3. SELECT order_id id, order_no orderNo, order_price price FROM orders WHERE order_id=#{id}
  4. </select>
Test
  
 
  1. @Test
  2. public void test1(){
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. SqlSession session = factory.openSession();
  5. String statement = "cn.imentors.mybatis.test4.orderMapper.getOrder";
  6. Order order = session.selectOne(statement , 2);
  7. System.out.println(order);
  8. }
3.2 by Resultmap
  
 
  1. <resultMap type="Order" id="getOrder2Map">
  2. <id property="id" column="order_id"/>
  3. <result property="orderNo" column="order_no"/>
  4. <result property="price" column="order_price"/>
  5. </resultMap>
  6. <select id="getOrder2" parameterType="int" resultMap="getOrder2Map">
  7. SELECT * FROM orders WHERE order_id=#{id}
  8. </select>
    • Resultmap: Encapsulating Some mapping relationships
    • ID: specifically for primary key
    • Result: For general fields
Test
  
 
  1. @Test
  2. public void test2(){
  3. SqlSessionFactory factory = MybatisUtils.getFactory();
  4. SqlSession session = factory.openSession();
  5. String statement = "cn.imentors.mybatis.test4.orderMapper.getOrder2";
  6. Order order = session.selectOne(statement , 2);
  7. System.out.println(order);
  8. }

Donate US
The Mentor Studio has been working to help programmers learn to program more quickly and easily, and if you agree with our results and feel helpful, you are welcome to donate ^_^ to us.     

04_ Resolving conflicts where field names differ from entity class property names

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.