• 【Mybaits学习】05_ Mapping映射属性冲突


    在实际开发中,字段名与实体类属性名不相同的问题,本章主要看一下解决方法

    主要看第3个步骤中的操作说明,观察resultMap配置前后的结果

    1、环境准备,建表

    2、创建Pojo实体类,属性与数据库表中的column不一致

    package nh.ui.automation.tools.Mybaits;
    
    /**
     * 项目 :UI自动化测试 Mybaits 类描述:
     * 
     * @author Eric
     * @date 2017年3月4日 nh.ui.automation.tools.Mybaits
     */
    public class Order {
        private int id;
        private String orderNo;
        private float price;
    
        /**
         * 
         */
        public Order() {
            super();
        }
    
        /**
         * @param id
         * @param orderNo
         * @param price
         */
        public Order(int id, String orderNo, float price) {
            super();
            this.id = id;
            this.orderNo = orderNo;
            this.price = price;
        }
    
        /**
         * @return the id
         */
        public int getId() {
            return id;
        }
    
        /**
         * @param id
         *            the id to set
         */
        public void setId(int id) {
            this.id = id;
        }
    
        /**
         * @return the orderNo
         */
        public String getOrderNo() {
            return orderNo;
        }
    
        /**
         * @param orderNo
         *            the orderNo to set
         */
        public void setOrderNo(String orderNo) {
            this.orderNo = orderNo;
        }
    
        /**
         * @return the price
         */
        public float getPrice() {
            return price;
        }
    
        /**
         * @param price
         *            the price to set
         */
        public void setPrice(float price) {
            this.price = price;
        }
    
        /*
         * (non-Javadoc)
         * 
         * @see java.lang.Object#toString()
         */
        @Override
        public String toString() {
            return "Order [id=" + id + ", orderNo=" + orderNo + ", price=" + price + "]";
        }
    
    }

    3、创建mapper文件

    <?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="nh.ui.automation.tools.Mybaits.Order.orderMapper">
        <select id="getOrder" parameterType="int"
            resultType="nh.ui.automation.tools.Mybaits.Order">
            select * from orders where id=#{id}
        </select>
    
        <insert id="addOrder" parameterType="nh.ui.automation.tools.Mybaits.Order">
            insert into
            orders(order_no,order_price) values(#{orderNo},#{price})
        </insert>
    
        <select id="getAllOrder" resultType="nh.ui.automation.tools.Mybaits.Order"
            resultMap="orderResultMap">
            select * from orders
        </select>
        
    //第一次测试代码时可以不引用resultMap,观察执行结果。 <resultMap type="nh.ui.automation.tools.Mybaits.Order" id="orderResultMap"> <id property="id" column="id" /> <result property="orderNo" column="order_no" /> <result property="price" column="order_price" /> </resultMap> </mapper>

    4、在Mybaits配置文件中注册orderMapper文件

    <?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="db.properties"></properties>
    
        <environments default="development">
            <environment id="development">
                <transactionManager type="JDBC" />
                <dataSource type="POOLED">
                    <property name="driver" value="${db.driver}" />
                    <property name="url" value="${db.url}" />
                    <property name="username" value="${db.username}" />
                    <property name="password" value="${db.password}" />
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="Mappings/userMapper.xml"/>
            <mapper resource="Mappings/orderMapper.xml"/>
            <mapper class="nh.ui.automation.tools.mapper.UserMapper" />
        </mappers>
    </configuration>

    5、测试代码

            String statement = "nh.ui.automation.tools.Mybaits.Order.orderMapper.getOrder";
            Order order1 = sqlSession.selectOne(statement,1);
            System.out.println(order1);
            
            String statement1 = "nh.ui.automation.tools.Mybaits.Order.orderMapper.addOrder";
            int insert = sqlSession.insert(statement1,new Order(1,"dsf",11));
            System.out.println(insert);
            
            String statement2 = "nh.ui.automation.tools.Mybaits.Order.orderMapper.getAllOrder";
            
            List<Object> selectList = sqlSession.selectList(statement2);
            System.out.println(selectList);
  • 相关阅读:
    Django基于form组件实现注册校验
    Django 框架概况
    【ARM】S5PV210芯片的启动流程
    【ARM】S3C6410芯片的启动流程
    Go's Declaration Syntax
    挺经
    软件系统最重要的一点
    Laravel 服务容器实例教程 —— 深入理解控制反转(IoC)和依赖注入(DI)
    你大概走了假敏捷:认真说说敏捷的实现和问题(手绘版)
    我的书单
  • 原文地址:https://www.cnblogs.com/hylinux/p/6503342.html
Copyright © 2020-2023  润新知