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!