Mybatis实例

来源:互联网 发布:mysql 名称去重复连接 编辑:程序博客网 时间:2024/05/22 06:25
一、简介:
MyBatis[1] 是支持普通 SQL查询,存储过程和高级映射的优秀持久层框架。MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Ordinary Java Objects,普通的 Java对象)映射成数据库中的记录。
每个MyBatis应用程序主要都是使用SqlSessionFactory实例的,一个SqlSessionFactory实例可以通过SqlSessionFactoryBuilder获得。SqlSessionFactoryBuilder可以从一个xml配置文件或者一个预定义的配置类的实例获得。

用xml文件构建SqlSessionFactory实例是非常简单的事情。推荐在这个配置中使用类路径资源(classpath resource),但你可以使用任何Reader实例,包括用文件路径或file://开头的url创建的实例。MyBatis有一个实用类----Resources,它有很多方法,可以方便地从类路径及其它位置加载资源。

二、总体流程
(1)加载配置并初始化
触发条件:加载配置文件
处理过程:将SQL的配置信息加载成为一个个MappedStatement对象(包括了传入参数映射配置、执行的SQL语句、结果映射配置),存储在内存中。
(2)接收调用请求
触发条件:调用Mybatis提供的API
传入参数:为SQL的ID和传入参数对象
处理过程:将请求传递给下层的请求处理层进行处理。
(3)处理操作请求
触发条件:API接口层传递请求过来
传入参数:为SQL的ID和传入参数对象
处理过程:
(A)根据SQL的ID查找对应的MappedStatement对象。
(B)根据传入参数对象解析MappedStatement对象,得到最终要执行的SQL和执行传入参数。
(C)获取数据库连接,根据得到的最终SQL语句和执行传入参数到数据库执行,并得到执行结果。
(D)根据MappedStatement对象中的结果映射配置对得到的执行结果进行转换处理,并得到最终的处理结果。
(E)释放连接资源。
(4)返回处理结果将最终的处理结果返回。
三、开发案例
1.创建javaBean
public class BtProdOfferRelation {
@Override
public String toString() {
return "BtProdOfferRelation [SALE_CODE=" + SALE_CODE + ", SALE_DESC="
+ SALE_DESC + ", PROD_OFFER_ID=" + PROD_OFFER_ID
+ ", PRODUCT_DESC=" + PRODUCT_DESC + "]";
}

private String SALE_CODE;
private String SALE_DESC;
private String PROD_OFFER_ID;
private String PRODUCT_DESC;

public String getSALE_CODE() {
return SALE_CODE;
}

public void setSALE_CODE(String sALE_CODE) {
SALE_CODE = sALE_CODE;
}

public String getSALE_DESC() {
return SALE_DESC;
}

public void setSALE_DESC(String sALE_DESC) {
SALE_DESC = sALE_DESC;
}

public String getPROD_OFFER_ID() {
return PROD_OFFER_ID;
}

public void setPROD_OFFER_ID(String pROD_OFFER_ID) {
PROD_OFFER_ID = pROD_OFFER_ID;
}

public String getPRODUCT_DESC() {
return PRODUCT_DESC;
}

public void setPRODUCT_DESC(String pRODUCT_DESC) {
PRODUCT_DESC = pRODUCT_DESC;
}
}
2.创建Mapper类
public interface BtProdOfferRelationMapper {
/**
* 获取销售品配置
* 获取用户信息
* @return List<BtProdOfferRelation>
*/
public List<BtProdOfferRelation> getProdOfferRelation(String offerProdId);

}
3.创建Mapper类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="cn.com.bonc.ssm.mapper.BtProdOfferRelationMapper" >

<resultMap id="BaseResultMap" type="cn.com.bonc.ssm.pojo.BtProdOfferRelation" >
<result column="SALE_CODE" property="SALE_CODE" jdbcType="VARCHAR" />
<result column="SALE_DESC" property="SALE_DESC" jdbcType="VARCHAR" />
<result column="PROD_OFFER_ID" property="PROD_OFFER_ID" jdbcType="VARCHAR" />
<result column="PRODUCT_DESC" property="PRODUCT_DESC" jdbcType="VARCHAR" />
</resultMap>

<select id="getProdOfferRelation" parameterType="string" resultMap="BaseResultMap">
select SALE_CODE,SALE_DESC,PROD_OFFER_ID,PRODUCT_DESC
from frame_dim.d_rpt_wap_promo a
where a.dg_type = 2
and a.sale_code = to_number(#{offerProdId,jdbcType=VARCHAR})
</select>
</mapper>
4.spring配置文件
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">

<bean id="btprodofferrelationService" class="cn.com.bonc.ssm.service.impl.BtProdOfferRelationServiceImpl"></bean>

</beans>

原创粉丝点击