欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

解决字段名与实体类属性名不相同的冲突

程序员文章站 2024-02-11 14:02:04
...

因为java工程师定义实体类属性名的和数据库工程师所定义的字段名不一致,所以要解决不一致的冲突

1、目录结构

解决字段名与实体类属性名不相同的冲突


2、在数据库中建立相应的表并填入数据

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

3、定义实体类

package com.sxau.rjxy.MyBatis_test4;

public class Order {
	private int id;
	private String orderno;
	private float price;
	public Order(int id,String orderno,float price){
		this.id = id;
		this.orderno = orderno;
		this.price = price;
	}
	public Order(){}
	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;
	}
	@Override
	public String toString() {
		return "Order [id=" + id + ", orderno=" + orderno + ", price=" + price + "]";
	}
	public void setPrice(float price) {
		this.price = price;
	}
	
}

OrderMapper.xml配置文件

select中的resultMap名应该与下面的resultMap中的id名对应

<?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="com.sxau.rjxy.MyBatis_test4.orderMapper">
	<select id="getOrder" parameterType ="int" 
	resultType="Order" resultMap="orderResultMap">
		select * from orders where order_id =#{id}
		 <!--select order_id id,order_no orderno,order_price price from orders where order_id =#{id}
		 -->
	</select>
	
	 <resultMap type = "com.sxau.rjxy.MyBatis_test4.Order" id="orderResultMap">
		<id property="id" column="order_id" />
		<id property="orderno" column="order_no" />
		<id property="price" column="order_price" />
	</resultMap>
</mapper>

方法二

将select * from orders where order_id =#{id}注解。将下面的那句select解除注解并删除resultMap也可以实现。

4、conf.xml配置文件

<?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="${driver}" />
				<property name="url" value="${url}" /><!-- 数据库名可更改 -->
				<property name="username" value="${username}" />
				<property name="password" value="${password}" /><!-- 密码可更改 -->
			</dataSource>
		</environment>
	</environments>
	<mappers>
		<mapper resource="com/sxau/rjxy/MyBatis_test/userMapper.xml" />
		<mapper resource="com/sxau/rjxy/MyBatis_test2/userMapper1.xml"  /><!-- 这个是右键userMapper.xml选择 copy qualified Name复制,并删除com之前的东西-->
		<mapper resource="com/sxau/rjxy/MyBatis_test3/userMapper.xml"  />
		<mapper resource="com/sxau/rjxy/MyBatis_test4/orderMapper.xml" />
	</mappers>
</configuration>



5、测试类

package com.sxau.rjxy.MyBatis_test4;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.sxau.rjxy.MyBatis_test2.MyBatisUtils;

public class Test4 {
	public static void main(String[] args) {
		
		//构建sqlSession的工厂
		SqlSessionFactory factory = MyBatisUtils.getFactory();
		//创建能执行映射文件中sql的sqlSession
		SqlSession session = factory.openSession();
		//映射sql的标识字符串
		String statement = "com.sxau.rjxy.MyBatis_test4.orderMapper.getOrder";
		//执行查询返回一个唯一user对象的sql
		Order order = session.selectOne(statement, 1);
		System.out.println(order);
	}
}