mybatis学习笔记(3)—数据库和bean名称不一样处理方案

  • 之前写的mybatis对象的bean对象和数据库的对象名称是对应的,然而再实际开发的过程有很多不一一对应的情况。就需要解决。
    bean对象:
package com.test.bean;
/*
 * javabean 对象
 */
public class order {
	 private int id;
	 private String no;
	 private float price;
	 public int getId() {
	 return id;
	 }

	 public void setId(int id) {
	 this.id = id;
	 }

	 public String getOrderNo() {
	 return no;
	 }

	 public void setOrderNo(String orderNo) {
	 this.no = orderNo;
	 }

	 public float getPrice() {
	 return price;
	 }

	 public void setPrice(float price) {
	 this.price = price;
	 }

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

而mysql的对象为
mybatis学习笔记(3)—数据库和bean名称不一样处理方案_第1张图片
再xml中的配置为:


-<mapper namespace="ordermapper">




<select resultType="com.test.bean.order" parameterType="int" id="getorderbyid">select * from orders where order_id=#{id} select>




<select resultType="com.test.bean.order" parameterType="int" id="getorderbyid2">select order_id id,order_no no,order_price price from orders where order_id=#{id} select>

<select parameterType="int" id="getorderbymap" resultMap="orderResultMap">select * from orders where order_id=#{id} select>


-<resultMap id="orderResultMap" type="com.test.bean.order">




<id column="order_id" property="id"/>




<result column="order_no" property="no"/>

<result column="order_price" property="price"/>

resultMap>




<select id="getUserCount" statementType="CALLABLE" parameterMap="getUserCountMap">CALL mybits.ges_user_count(?,?) select>





-<parameterMap id="getUserCountMap" type="java.util.Map">

<parameter property="sexid" jdbcType="INTEGER" mode="IN"/>

<parameter property="usercount" jdbcType="INTEGER" mode="OUT"/>

parameterMap>

mapper>

test实例(先封装一下)

import java.io.InputStream;

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



public class returnsqlsession {
	public static SqlSessionFactory getSqlSessionFactory()
	{
		String resource="conf.xml";
		InputStream in=returnsqlsession.class.getClassLoader().getResourceAsStream(resource);
		SqlSessionFactory factory=new SqlSessionFactoryBuilder().build(in);
		return factory;
	}
	public static SqlSession getSqlSession(){//返回sqlsession
		return getSqlSessionFactory().openSession();	
	}
	 /**
	 * 获取SqlSession
	 * @param isAutoCommit 
	 * true 表示创建的SqlSession对象在执行完SQL之后会自动提交事务
	 * false 表示创建的SqlSession对象在执行完SQL之后不会自动提交事务,这时就需要我们手动调用sqlSession.commit()提交事务
	 * @return SqlSession
	 */
	 public static SqlSession getSqlSession(boolean isAutoCommit) {
	 return getSqlSessionFactory().openSession(isAutoCommit);
	 }
}

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.test.bean.order;

public class test1 {
 
	public static void testselect1() throws IOException
	{
		SqlSession sqlsession=returnsqlsession.getsqlsession(true);
		//String statement="ordermapper.getorderbyid1";
		//String statement="ordermapper.getorderbyid2";
		String statement="ordermapper.getorderbymap";
		order order=sqlsession.selectOne(statement, 2);
	 sqlsession.close();
	 System.out.println(order);
	}

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
 testselect1();	
	}
}

两种基本的解决方式,如果用到的较少,可以选择第一种,如果用到较多较复杂,可以选择第二种。

你可能感兴趣的:(#,java,EE,#,javaWeb)