zoukankan      html  css  js  c++  java
  • 【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);
  • 相关阅读:
    已解决[Authentication failed for token submission,Illegal hexadecimal charcter s at index 1]
    远程快速安装redis和远程连接
    远程快速安装mysql
    Swiper的jquery动态渲染不能滑动
    微服务架构攀登之路(三)之gRPC入门
    微服务架构攀登之路(二)之RPC
    微服务架构攀登之路(一)之微服务初识
    Go语言中new和make的区别
    Go语言实战爬虫项目
    Go语言系列(十一)- 日志收集系统架构
  • 原文地址:https://www.cnblogs.com/hylinux/p/6503342.html
Copyright © 2011-2022 走看看