mybatis功能之spring自动代理完成dao的实现类功能

之前在写包的时候会划分为dao层,service层,action层,以及实现类层

有了mybatis后dao接口层的实现类不需要写了,有spring代理完成,步骤如下

在spring的配置文件中spring.xml配置如下:

1. spring.xml


<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd

">

<!-- 1. 配置c3p0连接池 -->
<bean id="comboPooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="Oracle.jdbc.driver.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="user" value="scm"/>
<property name="password" value="666"/>
</bean>

<!-- 2. 配置sqlsession代替原生mybatisUtil工具类 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载配置文件mybatis.xml -->
<property name="configLocation" value="classpath:mybatis.xml"/>
<!-- 引入数据资源 -->
<property name="dataSource" ref="comboPooledDataSource"/>
</bean>

<!-- 3. mybatis事务管理器,底层用的是jdbc -->
<bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 引入数据源 -->
<property name="dataSource" ref="comboPooledDataSource"/>
</bean>


<!-- 由于使用了接口的命名空间,不需要dao的实现类,也不需要sqlSessionTemplate了 -->
<!--<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/>

</bean>
-->

<!-- 4. 配置事物通知,如何管理事物 -->
<tx:advice id="tx" transaction-manager="dataSourceTransactionManager">
<tx:attributes>
<!-- rollback-for="Exception" 回滚异常 -->
<tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="update*" propagation="REQUIRED" rollback-for="Exception"/>
<tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception"/>
<!-- 如果有事务则在当前事务执行,当前没事务则在没事务情况下执行 -->
<tx:method name="*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>

<!-- 5.配置事物切面aop,拦截哪些方法 -->
<aop:config>
<aop:pointcut expression="execution(* com.coffee.scm.service.impl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="tx" pointcut-ref="pointcut"/>
</aop:config>

<!-- 注册dao -->
<!-- <bean id="deptDao" class="com.coffee.scm.dao.impl.DeptDao">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean> -->
<!-- 注册action(注解),里面有service,service加了注解,扫描 -->
<!-- <context:component-scan base-package="*"/> -->

<!-- 扫描过滤掉controller,因为他们是在springmvc里面配的,否则会出现问题 -->
<context:component-scan base-package="com.coffee">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 配置dao接口映射,然后就不用写dao实现类了
配置 转换器,对于在basePackage设置的包(包括子包)下的接口类,如果在Mapper.xml文件中定义过,
将被转换成spring的BEAN,在调用 的地方通过@Autowired方式将可以注入接口实例
由spring自己完成接口的实现
value="com.coffee.scm.dao"映射接口全路径

-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
<property name="basePackage" value="com.coffee.scm.dao"></property>
</bean>

<!-- 通知springioc注解作用 -->
<context:annotation-config />

</beans>



2.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">
<!-- namespace定义接口的全路径,只需要在接口定义方法
不需要dao层的实现类,然后再写sql语句就行了
原理:
-->
<mapper namespace="com.coffee.scm.dao.IDeptDao">
<resultMap type="dept" id="deptResultMap">
<id property="deptId" column="dept_id"/>
<result property="deptName" column="dept_name"/>
<result property="deptAddress" column="dept_address"/>
</resultMap>

<select id="selectDept" parameterType="Integer" resultMap="deptResultMap">
<!--参数的写法#{deptID} -->
select * from dept where dept_id =#{deptId}
</select>


<insert id="insert" parameterType="dept">
insert into dept values(#{deptId},#{deptName},#{deptAddress})
</insert>

</mapper>


3. service实现层


package com.coffee.scm.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.coffee.scm.dao.IDeptDao;
import com.coffee.scm.entity.Dept;
import com.coffee.scm.service.IDeptService;


@Service
public class DeptService implements IDeptService {


// 这个注解是根据类型配置,只需找类型,加上这个注解,然后配置接口映射可以去掉dao的接口实现,由spring自动代理完成
@Autowired
private IDeptDao deptDao;


/**
* 插入部门信息,如果遇到异常会回滚,因为spring.xml的事务通知配置里面配置了rollback-for="Exception"
*/
@Override
public void insertDept(Dept dept) throws Exception {
try {
deptDao.insert(dept);


} catch (Exception e) {
throw new Exception(e);
}
}


}


注意:dao接口层定义的方法名要和mapper.xml的sql标签的id对应一致
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值