之前的博客我们介绍了如何将类与表进行映射,如何在此基础上CRUD操作。在那些例子中,我们假定表的字段名称与类的属性名称是相同且一一对应的。但实际情况是,类的设计人与数据库的设计人往往没有那种默契,对于小小的coder来说应该怎么办呢?一个人很多时候需要当别人的润滑剂和连接点,就像搞定导师和老板的关系一样、搞定老妈和MM的关系一样、搞定开发和运维的关系一样,还有搞定类的属性名和表的字段名的命名不一致一样。
好吧,我们首先将类和表都定义好。(本文出自:http://my.oschina.net/happyBKs/blog/474840)
CREATE TABLE orders(
order_id INT PRIMARY KEY AUTO_INCREMENT,
order_no VARCHAR(20),
order_priceorders 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);
类定义:
package com.happyBKs.mybatis.C1_1.beans;
public class 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;
}
public void setPrice(float price) {
this.price = price;
}
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();
}
@Override
public String toString() {
return "Order [id=" + id + ", orderNo=" + orderNo + ", price=" + price
+ "]";
}
}
好,现在我们正式介绍当表与类的属性命名不一致的解决办法。办法有两种:
利用SQL语句的返回字段指定字段别名,别名需要与类的属性名一致,这样mybatis会自动将表的字段名和类的属性名进行关联匹配。但是,这种方法一般只适合用于select的情况,update、delete、insert不方便指定字段别名。
所以,按照这种方法,我写了一个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.happyBKs.mybatis.C1_1.orderMapper">
<!-- 根据id查询对象 -->
<select id="getOrder" parameterType="int" resultType="com.happyBKs.mybatis.C1_1.beans.Order">
select order_id id, order_no orderNo,order_price price from orders where order_id=#{id}
</select>
</mapper>
如果我们想在CUD中解决这个问题,就需要应用第二种方法,即指定parameterMap或resultMap。parameterMap会对输入参数的类进行字段属性映射,resultMap会对返回的对象进行字段属性映射。
<?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.happyBKs.mybatis.C1_1.orderMapper2">
<select id="selectOrderResultMap" parameterType="int" resultMap="orderResultMap">
select * from orders where order_id=#{id}
</select>
<resultMap type="com.happyBKs.mybatis.C1_1.beans.Order" id="orderResultMap">
<id property="id" column="order_id" />
<result property="orderNo" column="order_no" />
<result property="price" column="order_price" />
</resultMap>
<parameterMap type="com.happyBKs.mybatis.C1_1.beans.Order" id="orderParameterMap">
<parameter property="id" />
<parameter property="orderNo" />
<parameter property="price" />
</parameterMap>
<insert id="insertOrderResultMap" parameterMap="orderParameterMap">
insert into orders(order_id,order_no,order_price) values (#{id},#{orderNo},#{price})
</insert>
</mapper>
测试一下上面的例子吧:
package com.happyBKs.mybatis.C1_1;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import com.happyBKs.mybatis.C1_1.beans.Order;
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
test3();
}
private static void test1() {
String resource = "config.xml";
//加载mybatis的配置文件(它也加载关联的映射文件)
Reader reader=null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//映射sql的标识字符串
String statement = "com.happyBKs.mybatis.C1_1.orderMapper"+".getOrder";//先找映射文件,后找标签
//执行查询返回一个唯一user对象的sql
Order order = session.selectOne(statement, 2);
System.out.println(order);
}
private static void test2() {
String resource = "config.xml";
//加载mybatis的配置文件(它也加载关联的映射文件)
Reader reader=null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession();
//映射sql的标识字符串
String statement = "com.happyBKs.mybatis.C1_1.orderMapper2"+".selectOrderResultMap";//先找映射文件,后找标签
//执行查询返回一个唯一user对象的sql
Order order = session.selectOne(statement, 1);
System.out.println(order);
}
private static void test3() {
String resource = "config.xml";
//加载mybatis的配置文件(它也加载关联的映射文件)
Reader reader=null;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//构建sqlSession的工厂
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
//创建能执行映射文件中sql的sqlSession
SqlSession session = sessionFactory.openSession(true);
//映射sql的标识字符串
String statement = "com.happyBKs.mybatis.C1_1.orderMapper2"+".insertOrderResultMap";//先找映射文件,后找标签
//执行查询返回一个唯一user对象的sql
int n = session.insert(statement, new Order(-1,"hhhh",12.0f));
System.out.println(n);
}
}