之前在写包的时候会划分为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对应一致
分享到:
相关推荐
springboot,mybatis,springboot-mybatis环境,自动生成dao实现类,自动扫描 springboot环境,mybatis自动扫描,mybatis无需实现类,mybatis mapper扫描 springboot mybatis 手动写dao实现类的: ...
spring和mybatis框架整合,采用mapper代理自动扫描方式实现,已添加junit测试类。建议参照我的另一资源spring和mybatis整合(原始dao方式实现)对比
springboot,mybatis,springboot-mybatis环境,手动写dao实现类,非自动扫描 springboot环境,mybatis springboot mybatis 自动生成dao实现类的: https://download.csdn.net/download/nowsdt/11085245
spring和mybatis框架整合,采用原始dao方式实现,已添加junit测试类
但是有一些部分自己没有能完成,主要是如何从spring容器里取出ApplicationContext,这个我的实现比较low,看了看讲义,才OK的。 我的实现: [java] view plain copy WebApplicationContext acc = ...
mybatis实战教程mybatis in action之七实现mybatis分页源码下载 mybatis实战教程mybatis in action之八mybatis 动态sql语句 mybatis实战教程mybatis in action之九mybatis 代码生成工具的使用 mybatis ...
MyBatis-Spring 整合 包括了原始的dao实现类的方式,和mapper 动态代理的方式,详细的配置文件讲解与注释
因为 simple-spring-memcached 暂时和 MyBatis3 没办法直接整合(MyBatis3 不再需要 DAO 实现类),simple-spring-memcached annotation 在 interface 方法里不起作用。见我提交的 Bug。 ssm3-springcache-mybatis3-...
一、建立数据库及新表 二、maven依赖pom.xml ...所以,该整合,只需要将 SqlSessionFactory 的对象生成器 SqlSessionFactoryBean 注册在 Spring 容器中,再将其注入给 Dao 的实现类即可完成整合
Spring+SpringMVC+Mybatis+Redis+OSS项目工具类大合集
真是不好意思,后来发现Dao层注入是在接口,接口实现类完全没必要要了,但是要保证接口方法和mybatis的配置文件一致,搁着一段时间没弄了,最近又开始捣鼓了,现在加权限到时候在放出来 SSM(struts2+spring3+mybatis)整合...
spring+mybatis dao层使用动态代理实现org.mybatis.spring.mapper.MapperScannerConfigurer类设置属性指定dao接口以及sqlSessionFactory 或SqlSessionTemplate
接口+实现类来完成。需要dao实现类需要继承SqlsessionDaoSupport类
① :原始dao开发方法(程序需要编写dao接口和dao实现类)(掌握) ② :mybaits的mapper(代理)接口(相当于dao接口)代理开发方法(掌握)第二种要掌握 mybatis配置文件SqlMapConfig.xml*** mybatis核心: ...
利用Mybatis逆向工程来生成pojo,dao和mapper。然后将pojo实体类和dao拷贝到工程当中。这样可以快速地实现数据库的开发
1、增加Dao配套的Service接口和实现类。 2、去掉Example相关的类,只保留基础的增删改查方法个代码,减少代码量更清爽。 3、常用配置项外置到config.properties中,修改更加方便。 4、原XML格式为两个个空格缩进,现...
框架封装了很多的细节,使开发者可以使用极简的方式实现功能。大大提高开发效率。 2.三层架构 表现层: 是用于展示数据的 业务层: 是处理业务需求的 持久层: 是和数据库交互的 3.持久层的技术解决...
9.3.2 Spring的事务管理器实现类 9.3.3 事务同步管理器 9.3.4 事务传播行为 9.4 编程式的事务管理 9.5 使用XML配置声明式事务 9.5.1 一个将被实施事务增强的服务接口 9.5.2 使用原始的 TransactionProxyFactoryBean ...
7.4.1 Dao接口实现类继承SqlSessionDaoSupport 70 7.4.2 使用org.mybatis.spring.mapper.MapperFactoryBean 71 7.4.3 使用mapper扫描器 71 8 Mybatis逆向工程 72 8.1 第一步:mapper生成配置文件: 72 8.2 第二步:...
9.3.2 Spring的事务管理器实现类 9.3.3 事务同步管理器 9.3.4 事务传播行为 9.4 编程式的事务管理 9.5 使用XML配置声明式事务 9.5.1 一个将被实施事务增强的服务接口 9.5.2 使用原始的 TransactionProxyFactoryBean ...