博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
mybatis--面向接口编程
阅读量:4514 次
发布时间:2019-06-08

本文共 1193 字,大约阅读时间需要 3 分钟。

假设使用hiberante作为dao层,经常使用的方式是:定义一个dao层接口包(com.dao.service)然后在定义一个dao层接口实现包(com.dao.service.impl),这样定义结构清晰,方便维护和开发工作。假设使用mybatis作为dao层,我们就能够省略到dao实现包,直接将sql实如今xml配置文件里编写,这样维护起来更方便了!

首先将mybatis整合到spring中:

    <!-- define the SqlSessionFactory -->

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />

配置javabean所在的包

        <property name="typeAliasesPackage" value="org.mybatis.jpetstore.domain" />
    </bean>
    <!-- scan for mappers and let them be autowired -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

配置dao接口层

        <property name="basePackage" value="org.mybatis.jpetstore.persistence" />
    </bean>

整合完spring后,就能够使用spring的autowire自己主动注入功能!

在接口层定义了:

public interface UserMapper
{
void persistence(User user);
}

然后在编写UserMapper实现的配置文件:

<mapper namespace="UserMapper">

<cache />加入�缓存
<insert id="persistence" parameterType="User">
insert into
user(account,password,name,address,man)
values(#{account},#{password},#{name},#{address},#{man})
</insert>
</mapper>

dao接口实现成就实现完毕了,在使用时仅仅须要:

@Autowired

UserMapper userMapper; 

就能够直接使用UserMapper 对数据进行操作了!

这样感觉比hibernate操作dao层更方便了!

转载于:https://www.cnblogs.com/yxwkf/p/3921831.html

你可能感兴趣的文章
[CSS3] :empty Selector
查看>>
webpack4 入门(二)
查看>>
vim配置成c++IDE
查看>>
利用node搭建本地服务器
查看>>
python pickle命令执行与marshal 任意代码执行
查看>>
Elasticsearch 2.3 java api
查看>>
golang写入csv
查看>>
基础2
查看>>
java基础篇---网络编程(UDP程序设计)
查看>>
Kafka Producer相关代码分析【转】
查看>>
LeetCode 121. Best Time to Buy and Sell Stock
查看>>
麻省理工学院公开课-第四讲:快速排序 及 随机化 算法
查看>>
复杂表达式
查看>>
R12.1.3 & R12.2.X 注册客户化应用
查看>>
实验十七 线程同步控制
查看>>
SQL Server 触发器
查看>>
Ural 1146 Maximum Sum(DP)
查看>>
《STL源代码分析》---stl_stack.h读书笔记
查看>>
UVA 10385 - Duathlon(三分法)
查看>>
div同时使用两个class
查看>>