<h1>
<span class="link_title"><a href="/lablenet/article/details/50440321">
Spring + Mybatis - 原始dao开发整合 与 Mapper代理整合
</a>
</span>
</h1>
<div class="article_manage clearfix">
<div class="article_l">
<span class="link_categories">
标签:
<a href="http://www.csdn.net/tag/mybatis" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">mybatis</a><a href="http://www.csdn.net/tag/Spring" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">Spring</a><a href="http://www.csdn.net/tag/bean" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">bean</a><a href="http://www.csdn.net/tag/ApplicationContext" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">ApplicationContext</a><a href="http://www.csdn.net/tag/Mapper" target="_blank" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_tag']);">Mapper</a>
</span>
</div>
<div class="article_r">
<span class="link_postdate">2015-12-31 11:01</span>
<span class="link_view" title="阅读次数">2160人阅读</span>
<span class="link_comments" title="评论次数"> <a href="#comments" onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_pinglun'])">评论</a>(0)</span>
<span class="link_collect tracking-ad" data-mod="popu_171"> <a href="javascript:void(0);" onclick="javascript:collectArticle('Spring+%2b+Mybatis+-+%e5%8e%9f%e5%a7%8bdao%e5%bc%80%e5%8f%91%e6%95%b4%e5%90%88+%e4%b8%8e+Mapper%e4%bb%a3%e7%90%86%e6%95%b4%e5%90%88','50440321');return false;" title="收藏" target="_blank">收藏</a></span>
<span class="link_report"> <a href="#report" onclick="javascript:report(50440321,2);return false;" title="举报">举报</a></span>
</div>
</div> <style type="text/css">
.embody{
padding:10px 10px 10px;
margin:0 -20px;
border-bottom:solid 1px #ededed;
}
.embody_b{
margin:0 ;
padding:10px 0;
}
.embody .embody_t,.embody .embody_c{
display: inline-block;
margin-right:10px;
}
.embody_t{
font-size: 12px;
color:#999;
}
.embody_c{
font-size: 12px;
}
.embody_c img,.embody_c em{
display: inline-block;
vertical-align: middle;
}
.embody_c img{
30px;
height:30px;
}
.embody_c em{
margin: 0 20px 0 10px;
color:#333;
font-style: normal;
}
</style>
<script type="text/javascript">
$(function () {
try
{
var lib = eval("("+$("#lib").attr("value")+")");
var html = "";
if (lib.err == 0) {
$.each(lib.data, function (i) {
var obj = lib.data[i];
//html += '<img src="' + obj.logo + '"/>' + obj.name + " ";
html += ' <a href="' + obj.url + '" target="_blank">';
html += ' <img src="' + obj.logo + '">';
html += ' <em><b>' + obj.name + '</b></em>';
html += ' </a>';
});
if (html != "") {
setTimeout(function () {
$("#lib").html(html);
$("#embody").show();
}, 100);
}
}
} catch (err)
{ }
});
</script>
<div class="category clearfix">
<div class="category_l">
<img src="http://static.blog.csdn.net/images/category_icon.jpg">
<span>分类:</span>
</div>
<div class="category_r">
<label onclick="GetCategoryArticles('6038676','LABLENET','top','50440321');">
<span onclick="_gaq.push(['_trackEvent','function', 'onclick', 'blog_articles_fenlei']);">--> 3.2 spring<em>(11)</em></span>
<img class="arrow-down" src="http://static.blog.csdn.net/images/arrow_triangle _down.jpg" style="display:inline;">
<img class="arrow-up" src="http://static.blog.csdn.net/images/arrow_triangle_up.jpg" style="display:none;">
<div class="subItem">
<div class="subItem_t"><a href="http://blog.csdn.net/LABLENET/article/category/6038676" target="_blank">作者同类文章</a><i class="J_close">X</i></div>
<ul class="subItem_l" id="top_6038676">
</ul>
</div>
</label>
</div>
</div>
<div class="bog_copyright">
<p class="copyright_p">版权声明:本文为博主原创文章,未经博主允许不得转载。</p>
</div>
1.准备
这段时间学习了Spring和Mybatis的知识,我们知道持久层的 mapper,dao都需要spring进行管理, 需要spring通过单例的方式管理SqlSessionFactory,spring和mybatis整合生成代理对象使用sqlsessionFactory创建sqlsession;
练习全部jar (Spring+Mybatis+Spring-mybatis整合包)下载 :
http://download.csdn.net/detail/lablenet/9385114
demo数据准备 :
(1) 创建一个客户表 :
- --客户表
- CREATE TABLE F_CLIENT(
- ID NUMBER(12) PRIMARY KEY,--用户编号
- USERNAME VARCHAR2(20) NOT NULL,--用户姓名
- CLIENT_CERTIFICATE_NO VARCHAR2(20) NOT NULL UNIQUE,--证件号码
- BORN_DATE DATE,--出生日期
- FAMILY_REGISTER_ADDRESS VARCHAR2(200),--家庭住址
- NOW_ADDRESS VARCHAR2(200) NOT NULL,--现在住址
- CONTACT_MODE VARCHAR2(50) NOT NULL,--联系方式
- URGENCY_CONTACT_MODE VARCHAR2(50) NOT NULL,--紧急联系方式
- CREATE_DATE DATE NOT NULL--创建时间
- );
(2) 初始化数据 :
- insert into f_client(id,username,client_certificate_no,born_date,family_register_address,now_address,contact_mode,urgency_contact_mode,create_date) values (14,
- 'yuan','311209070127',to_date('1993-03-12','yyyy-mm-dd'),'河南省焦作市','河南省河南理工大学','150000000','110',sysdate);
- insert into f_client(id,username,client_certificate_no,born_date,family_register_address,now_address,contact_mode,urgency_contact_mode,create_date) values(
- 15,'yang','311209070126',to_date('1993-04-12','yyyy-mm-dd'),'河南温县','河南理工大学','3987321','110',sysdate);
- insert into f_client values(
- 16,'yang','311209070129',to_date('1997-04-12','yyyy-mm-dd'),'河南新乡','河南理工大学','3987321','110',sysdate);
- public class FClient {
- private Integer id;
- private String username;
- private String client_certificate_no;
- private Date born_date;
- private String family_register_address;
- private String now_address;
- private String contact_mode;
- private String urgency_contact_mode;
- private Date create_data;
- public Integer getId() {
- return id;
- }
- public void setId(Integer id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getClient_certificate_no() {
- return client_certificate_no;
- }
- public void setClient_certificate_no(String client_certificate_no) {
- this.client_certificate_no = client_certificate_no;
- }
- public Date getBorn_date() {
- return born_date;
- }
- public void setBorn_date(Date born_date) {
- this.born_date = born_date;
- }
- public String getFamily_register_address() {
- return family_register_address;
- }
- public void setFamily_register_address(String family_register_address) {
- this.family_register_address = family_register_address;
- }
- public String getNow_address() {
- return now_address;
- }
- public void setNow_address(String now_address) {
- this.now_address = now_address;
- }
- public String getContact_mode() {
- return contact_mode;
- }
- public void setContact_mode(String contact_mode) {
- this.contact_mode = contact_mode;
- }
- public String getUrgency_contact_mode() {
- return urgency_contact_mode;
- }
- public void setUrgency_contact_mode(String urgency_contact_mode) {
- this.urgency_contact_mode = urgency_contact_mode;
- }
- public Date getCreate_data() {
- return create_data;
- }
- public void setCreate_data(Date create_data) {
- this.create_data = create_data;
- }
- }
2.原始dao整合
整合图解 :
下面我们使用一个例子来说明 :
场景描述 : 通过客户id查询客户信息 ;
(1)dao层
dao接口
- public interface ClientDao {
- FClient findClientById(int id);
- }
dao接口实现 :
注意 : 在这里我们让dao实现接口继承SqlSessionDaoSupport (在spring-mybatis整合包中 org.mybatis.spring.support.SqlSessionDaoSupport),则在dao接口实现中不需要sqlsessionFactory属性了;就不用了在注入sqlSessionFactory了;
- public class ClinetDapimpl extends SqlSessionDaoSupport implements ClientDao {
- /*
- * 原始的sqlSessionFactory的注入方式
- * private SqlSessionFactory sqlSessionFactory;
- public ClinetDapimpl(SqlSessionFactory sqlSessionFactory) {
- super();
- this.sqlSessionFactory = sqlSessionFactory;
- }*/
- @Override
- public FClient findClientById(int id) {
- SqlSession session = this.getSqlSession();
- FClient client = (FClient) session.selectOne("cn.labelnet.dao.ClientDao.findClientById",id);
- return client;
- }
- }
-
原始的sqlSessionFactory的注入方式
-
private SqlSessionFactory sqlSessionFactory;
public ClinetDapimpl(SqlSessionFactory sqlSessionFactory) {
super();
this.sqlSessionFactory = sqlSessionFactory;
}*/@Override
public FClient findClientById(int id) {
SqlSession session = this.getSqlSession();
FClient client = (FClient) session.selectOne("cn.labelnet.dao.ClientDao.findClientById",id);
return client;
}
}
(2)Clientmap.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.labelnet.dao.ClientDao">
- <select id="findClientById" parameterType="int" resultType="cn.labelnet.po.FClient">
- select * from f_client where id=#{value}
- </select>
- </mapper>
(3)SqlMapConfig.xml
在之前的练习中,我们在这里加载数据源,但是在整合中我们将数据源加载交给了spring的ApplicationContext.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>
- <mappers>
- <mapper resource="cn/lablenet/dao/sqlmap/ClientMap.xml"/>
- <!-- 加载mapper映射文件 -->
- <!-- <mapper resource="cn/labelnet/mapper/ClientMapper.xml" /> -->
- </mappers>
- </configuration>
(4)ApplicationContext.xml
注意 :1)数据源加载用的是 org.apache.commons.dbcp.BasicDataSource ;
2)配置sqlsessionFactory 用的是 org.mybatis.spring.SqlSessionFactoryBean ;
3)配置dao的接口实现,为其注入sqlSessionFactory ;
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd">
- <!-- 加载数据库配置文件 -->
- <context:property-placeholder location="classpath:db.properties"/>
- <!-- 配置数据源,使用dbcp -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="${oracle.driver}"></property>
- <property name="url" value="${oracle.url}"></property>
- <property name="username" value="${oracle.name}"></property>
- <property name="password" value="${oracle.pass}"></property>
- <property name="maxActive" value="10"></property>
- <property name="maxIdle" value="5"></property>
- </bean>
- <!-- 1.配置sqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
- <!-- 加载mybatis的配置信息 -->
- <property name="configLocation" value="cn/labelnet/mybatis/config/SqlmapDaoConfig.xml"></property>
- <!-- 加载数据源 dataSource-->
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- <!-- 配置dao的接口实现,为其注入sqlSessionFactory -->
- <bean id="ClientDao" class="cn.labelnet.dao.ClinetDapimpl">
- <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
- </bean>
- </beans>
5)测试
- //得到Spring 容器
- private ApplicationContext applicationContext;
- @Before
- public void setUp() throws Exception {
- applicationContext=new ClassPathXmlApplicationContext("classpath:cn/labelnet/spring/config/applicationContext.xml");
- }
- @Test
- public void test() {
- ClientMapper bean = (ClientMapper) applicationContext.getBean("clientMapper");
- bean.findClientById(15);
- }
<div id="digg" articleid="50440321">
<dl id="btnDigg" class="digg digg_disable" onclick="btndigga();">
<dt>顶</dt>
<dd>0</dd>
</dl>
<dl id="btnBury" class="digg digg_disable" onclick="btnburya();">
<dt>踩</dt>
<dd>0</dd>
</dl>
</div>
<div class="tracking-ad" data-mod="popu_222"><a href="javascript:void(0);" target="_blank"> </a> </div>
<div class="tracking-ad" data-mod="popu_223"> <a href="javascript:void(0);" target="_blank"> </a></div>
<script type="text/javascript">
function btndigga() {
$(".tracking-ad[data-mod='popu_222'] a").click();
}
function btnburya() {
$(".tracking-ad[data-mod='popu_223'] a").click();
}
</script>
<div style="clear:both; height:10px;"></div>
<div class="similar_article">
<h4></h4>
<div class="similar_c" style="margin:20px 0px 0px 0px">
<div class="similar_c_t">
相关文章推荐
</div>
<div class="similar_wrap tracking-ad" data-mod="popu_36" style="max-height:250px">
<ul class="similar_list fl">
<li>
<em>•</em>
<a href="http://blog.csdn.net/mickjoust/article/details/51612482" title="Spring Boot 实践折腾记(二):切入点,Spring MVC集成Mybatis的经典例子" strategy="BlogCommendFromBaidu_0" target="_blank">Spring Boot 实践折腾记(二):切入点,Spring MVC集成Mybatis的经典例子</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/huiyiCourse/detail/596?utm_source=blog7" title="免费直播 神经网络的原理及结构设计--何宇健" strategy="undefined" target="_blank">免费直播 神经网络的原理及结构设计--何宇健</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/cwh056056/article/details/51051042" title="(五)mybatis学习之mapper代理开发方式及与spring整合" strategy="BlogCommendFromBaidu_1" target="_blank">(五)mybatis学习之mapper代理开发方式及与spring整合</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/huiyiCourse/detail/602?utm_source=blog7" title="Apache Weex:移动研发的进阶之路--董岩" strategy="undefined" target="_blank">Apache Weex:移动研发的进阶之路--董岩</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/h3243212/article/details/50778937" title="mybatis学习笔记(18)-mybatis逆向工程" strategy="BlogCommendFromBaidu_2" target="_blank">mybatis学习笔记(18)-mybatis逆向工程</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/course/detail/6252?utm_source=blog7" title="C++ 百万并发网络通信引擎架构与实现" strategy="undefined" target="_blank">C++ 百万并发网络通信引擎架构与实现</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/u011051912/article/details/73611618" title="SpringMVC中PostForEntity的中文乱码问题" strategy="BlogCommendFromBaidu_3" target="_blank">SpringMVC中PostForEntity的中文乱码问题</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/course/detail/6271?utm_source=blog7" title="PHP入门到精通及项目实战" strategy="undefined" target="_blank">PHP入门到精通及项目实战</a>
</li>
</ul>
<ul class="similar_list fr">
<li>
<em>•</em>
<a href="http://blog.csdn.net/h3243212/article/details/50837878" title="springmvc学习笔记(7)-springmvc整合mybatis之mapper" strategy="BlogCommendFromBaidu_4" target="_blank">springmvc学习笔记(7)-springmvc整合mybatis之mapper</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/course/detail/6409?utm_source=blog7" title=" Kotlin基本语法到实战开发" strategy="undefined" target="_blank"> Kotlin基本语法到实战开发</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/QH_JAVA/article/details/43699137" title="Spring之ORM(spring 与mybatis的4种整合实例)" strategy="BlogCommendFromBaidu_5" target="_blank">Spring之ORM(spring 与mybatis的4种整合实例)</a>
</li>
<li>
<em>•</em>
<a href="http://edu.csdn.net/course/detail/2932?utm_source=blog7" title=" Android 实战开发 第三方SDK 讯飞语音SDK" strategy="undefined" target="_blank"> Android 实战开发 第三方SDK 讯飞语音SDK</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/mickjoust/article/details/51605515" title="Spring Boot 实践折腾记(一):快速,Hello World例子" strategy="BlogCommendFromBaidu_6" target="_blank">Spring Boot 实践折腾记(一):快速,Hello World例子</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/u011051912/article/details/74295172" title="SpringBoot集成Mybatis+xml格式的sql配置文件" strategy="BlogCommendFromBaidu_7" target="_blank">SpringBoot集成Mybatis+xml格式的sql配置文件</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/zbw18297786698/article/details/53241167" title="Spring与Mybatis的整合------原始Dao的整合方式" strategy="BlogCommendFromBaidu_8" target="_blank">Spring与Mybatis的整合------原始Dao的整合方式</a>
</li>
<li>
<em>•</em>
<a href="http://blog.csdn.net/u010435203/article/details/51527404" title="SSM之DAO部分Spring与MyBatis整合" strategy="BlogCommendFromBaidu_9" target="_blank">SSM之DAO部分Spring与MyBatis整合</a>
</li>
</ul>
</div>
</div>
</div>