Shang Silicon Valley-mybatis-resolves the conflict between field names and object class names, and Silicon Valley-mybatis-

Source: Internet
Author: User

Shang Silicon Valley-mybatis-resolves the conflict between field names and object class names, and Silicon Valley-mybatis-

Project Structure:



Prepare tables and data:

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);

Order entity class code:

package com.atguigu.mybatis.bean;public class Order {private int id;private String orderNo;private float price;public Order(int id, String orderNo, float price) {super();this.id = id;this.orderNo = orderNo;this.price = price;}public Order() {super();}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getOrderNo() {return orderNo;}public void setOrderNo(String orderNo) {this.orderNo = orderNo;}public float getPrice() {return price;}public void setPrice(float price) {this.price = price;}@Overridepublic String toString() {return "Order [id=" + id + ", orderNo=" + orderNo + ", price=" + price+ "]";}}

OrderMapper. xml ing file code:

<? Xml version = "1.0" encoding = "UTF-8"?> <! DOCTYPE mapper PUBLIC "-// mybatis.org//DTD Mapper 3.0 //" http://mybatis.org/dtd/mybatis-3-mapper.dtd "> <mapper namespace =" com. atguigu. mybatis. bean. orderMapper "> <! -- Query the orders table by id to obtain an order object. --> <! -- Method 1 --> <select id = "getOrder" parameterType = "int" resultType = "Order"> select order_id id, order_no orderNo, order_price price from orders where order_id =#{ id} </select> <! -- Method 2 --> <select id = "getOrder2" parameterType = "int" resultMap = "getOrder2Map"> select * from orders where order_id =#{ id} </select> <! -- ResultMap: encapsulate some ing relationship IDs: specifically for the primary key result: for general fields --> <resultMap type = "Order" id = "getOrder2Map"> <id property = "id" column = "order_id"/> <result property = "orderNo" column = "order_no"/> <result property = "price" column = "order_price"/> </resultMap> </mapper>

Obtain the MybatisUtils code of the SqlSessionFactory Factory:

package com.atguigu.mybatis.utils;import java.io.InputStream;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class MybatisUtils {public static SqlSessionFactory getFactory() {String resource = "conf.xml";InputStream inputStream = MybatisUtils.class.getClassLoader().getResourceAsStream(resource);SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);return factory;}}

Configuration File conf. xml code:

<? 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"/> <! -- Define aliases for object classes to simplify SQL ing references in xml files --> <typeAliases> <! -- All classes in this package are named as entity classes (for example, the alias of the User class is User) --> <package name = "com. atguigu. mybatis. bean "/> </typeAliases> <! -- Development: development Mode work: working Mode --> <environments default = "development"> <environment id = "development"> <transactionManager type = "JDBC"/> <dataSource type = "POOLED"> <property name = "driver" value = "$ {driver}"/> <property name = "url" value = "$ {url}"/> <property name = "username" value = "$ {name}"/> <property name = "password" value = "$ {password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource = "com/atguigu/mybatis/bean/orderMapper. xml "/> </mappers> </configuration>

Test class Test3 code:

Package com. atguigu. mybatis. test3; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import com. atguigu. mybatis. bean. order; import com. atguigu. mybatis. utils. mybatisUtils;/** test: resolves conflicts between field names and object class property names */public class Test3 {public static void main (String [] args) {SqlSessionFactory factory = MybatisUtils. getFactory (); SqlSession session = factory. openSession (); String statement = "com. atguigu. mybatis. bean. orderMapper. getOrder "; statement =" com. atguigu. mybatis. bean. orderMapper. getOrder2 "; Order order = session. selectOne (statement, 3); System. out. println (order); session. close ();}}

Database Configuration code db. properties:

driver=com.mysql.jdbc.Driverurl=jdbc:mysql://localhost:3306/mybatisname=rootpassword=123456

Record the notes for learning mybatis and hope to help you learn, discuss, and make progress together!

Related Article

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.