首页 > 数据库 > 其他综合 >

尚硅谷-mybatis-解决字段名与实体类属性名不相同的冲突

2015-02-02

项目结构: 准备表和数据: CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no VARCHAR(20), order_price FLOAT ); INSERT INTO orders(order_no, order_price) VALUE

项目结构:

\

准备表和数据:<喎"http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:sql;">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(&#39;aaaa&#39;, 23); INSERT INTO orders(order_no, order_price) VALUES(&#39;bbbb&#39;, 33); INSERT INTO orders(order_no, order_price) VALUES(&#39;cccc&#39;, 22);

Order实体类代码:

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

	@Override
	public String toString() {
		return "Order [id=" + id + ", orderNo=" + orderNo + ", price=" + price
				+ "]";
	}

}

orderMapper.xml映射文件代码:

<?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.atguigu.mybatis.bean.orderMapper">
	
	<!-- 根据id查询orders表得到一个order对象的两种方法 -->
	<!-- 方法一 -->
	<select id="getOrder" parameterType="int" resultType="Order">
		select order_id id, order_no orderNo, order_price price from orders where order_id=#{id}
	</select>
	
	<!-- 方法二 -->
	<select id="getOrder2" parameterType="int" resultMap="getOrder2Map">
		select * from orders where order_id=#{id}
	</select>
	
	<!-- 
		resultMap:封装一些映射关系
		id:专门针对主键
		result:针对一般字段
	 -->
	 <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>

获取SqlSessionFactory工厂的MybatisUtils代码:

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

配置文件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"/>
	<!-- 为实体类定义别名,简化sql映射xml文件中的引用-->
	<typeAliases>
		<!-- 该包下的所有类以其简单类名为实体类别名(如:User类的别名为User) -->
		<package name="com.atguigu.mybatis.bean"/>
	</typeAliases>
	<!-- 
		development : 开发模式
		work : 工作模式
 	-->
	<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>

测试类Test3代码:

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;

/*
 * 测试: 解决字段名与实体类属性名不相同的冲突
 */
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();
	}
	
}

数据库配置代码db.properties:

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis
name=root
password=123456

记录学习mybatis的笔记,也希望对大家有所帮助,共同学习,共同讨论,共同进步!

相关文章
最新文章
热点推荐