关于spring整合mybatis
第一步导入依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.46</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--spring事务-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
</dependencies>
第二部获取必要的bean以及applicationContext.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/happy?useSSL=false"/>
<property name="username" value="root"/>
<property name="password" value="889886hp"/>
</bean>
<!-- 获取sqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!-- 在mybatis.xml配置文件已经配置mapper映射的时候就不需要在配置了 -->
<property name="mapperLocations" value="classpath:com/swun/mapper/Tb_randMapper.xml"/>
</bean>
<!-- 获取sqlSessionTemplate-->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg index="0" ref="sqlSessionFactory"/>
</bean>
</beans>
需要实现接口类如
<bean id="tb_brandMapperImpl" class="com.swun.mapper.impl.Tb_brandMapperImpl">
<property name="sqlSessionTemplate" ref="sqlSessionTemplate"/>
</bean>

另一种使用继承SQLSessionDaoSupport的方式
不需要引入SQLSessionTemplate
<!-- 使用继承SqlSessionDaoSupport的方式-->
<bean id="tb_brandMapperImpl2" class="com.swun.mapper.impl.Tb_brandMapperImpl2">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

直接扫描接口的方式(推荐方式)
需要service层的接口和dao层接口有相同方法,但不一定全部实现(不需要extend已经implement),dao层里面的@param注解也不需要,然后在由service层的实现类注入dao层接口
<!-- 4. 配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中! -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描的Dao接口包 -->
<property name="basePackage" value="com.inspur.dao"/>
</bean>
然后就是编写测试类
@Test
public void testAnn(){
//获取ioc容器
ApplicationContext Context = new ClassPathXmlApplicationContext("applicationContext.xml");
Tb_brandMapper tb_brandMapper = (Tb_brandMapper) Context.getBean("tb_brandMapperImpl");
//Tb_brandMapper tb_brandMapper = (Tb_brandMapper) Context.getBean("tb_brandMapperImpl2");
List<Tb_brand> tb_brands = tb_brandMapper.selectAll();
for (Tb_brand tb_brand : tb_brands) {
System.out.println(tb_brand);
}
}
声明事务
<!-- 声明事务-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg ref="dataSource"/>
</bean>
<!-- 实现事务注入(AOP)-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<!-- 配置事务注入-->
<aop:config>
<aop:pointcut id="txPointCut" expression="execution(* com.swun.mapper*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut"/>
</aop:config>
关于spring整合mybatis的更多相关文章
- Spring学习总结(六)——Spring整合MyBatis完整示例
为了梳理前面学习的内容<Spring整合MyBatis(Maven+MySQL)一>与<Spring整合MyBatis(Maven+MySQL)二>,做一个完整的示例完成一个简 ...
- Spring学习总结(五)——Spring整合MyBatis(Maven+MySQL)二
接着上一篇博客<Spring整合MyBatis(Maven+MySQL)一>继续. Spring的开放性和扩张性在J2EE应用领域得到了充分的证明,与其他优秀框架无缝的集成是Spring最 ...
- 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- 2017年2月16日 分析下为什么spring 整合mybatis后为啥用不上session缓存
因为一直用spring整合了mybatis,所以很少用到mybatis的session缓存. 习惯是本地缓存自己用map写或者引入第三方的本地缓存框架ehcache,Guava 所以提出来纠结下 实验 ...
- spring整合mybatis错误:class path resource [config/spring/springmvc.xml] cannot be opened because it does not exist
spring 整合Mybatis 运行环境:jdk1.7.0_17+tomcat 7 + spring:3.2.0 +mybatis:3.2.7+ eclipse 错误:class path reso ...
- spring 整合Mybatis 《报错集合,总结更新》
错误:java.lang.NoClassDefFoundError: org/aspectj/weaver/reflect/ReflectionWorld$ReflectionWorldExcepti ...
- spring整合mybatis(hibernate)配置
一.Spring整合配置Mybatis spring整合mybatis可以不需要mybatis-config.xml配置文件,直接通过spring配置文件一步到位.一般需要具备如下几个基本配置. 1. ...
- spring 整合 mybatis 中数据源的几种配置方式
因为spring 整合mybatis的过程中, 有好几种整合方式,尤其是数据源那块,经常看到不一样的配置方式,总感觉有点乱,所以今天有空总结下. 一.采用org.mybatis.spring.mapp ...
- Mybatis学习(六)————— Spring整合mybatis
一.Spring整合mybatis思路 非常简单,这里先回顾一下mybatis最基础的根基, mybatis,有两个配置文件 全局配置文件SqlMapConfig.xml(配置数据源,全局变量,加载映 ...
- Spring整合MyBatis 你get了吗?
Spring整合MyBatis 1.整体架构dao,entity,service,servlet,xml 2..引入依赖 <dependencies> <dependency> ...
随机推荐
- throws子句在继承当中overrride时有什么规则
8.throws子句在继承当中overrride时的规则 马克-to-win:当子类方法override父类方法时,throws子句不能引进新的checked异常.换句话说:子类override方法的 ...
- 定时-TimerTask
/** * @param args * @throws InterruptedException */ public static void main(String[] args) throws In ...
- Linux_连接工具_SecureCRT的使用教程
什么是SecureCRT? SecureCRT是一款支持 SSH2.SSH1.Telnet.Telnet/SSH.Relogin.Serial.TAPI.RAW 等协议的终端仿真程序,最吸引我的是,S ...
- Unable to negotiate with xx.xxx.xxxx port 22: no matching host key type found. Their offer: ssh-rsa(解决的两种方式)
异常问题: 下班之前升级了一下Git的版本,结果第二天过来拉取远程最新代码的时候就提示了下面的异常问题: Unable to negotiate with xx.xxx.xxxx port 22: n ...
- 帝国cms7.5忘记登录密码以及多次登录失败被锁定终极解决办法
其实网上很多忘记登录密码的解决方法都是相对于7.5版本以下的,在7.5的版本里根本不适用,今天小编主要给大家说的是针对帝国cms7.5忘记登录密码正确的解决办法. 前提是你能登陆服务器: 一.忘记登录 ...
- 内网穿透NPS
内网穿透实现 nps文档 https://ehang-io.github.io/nps/#/install nps docker镜像 https://hub.docker.com/r/ffdfgdfg ...
- 使用 Nginx 实现 URL 的重定向
1. 概述 老话说的好:取乎上,得其中:取乎中,得其下.因此我们不妨把目标定的高一些,去努力,才能得到更好回报. 言归正传,今天我们来聊聊 使用 Nginx 实现 URL 的重定向. 2. 使用 Ng ...
- 关闭Mac的Microsoft AutoUpdate
最近使用Office 发现AutoUpdate一直会启动.我也不需要里面的更新.每次还要把它推出. 网上看到有两种方法,一种是暴力删除,另一种是通过权限限制. 暴力可不是我喜欢的方式,所以选择后者. ...
- for in 语法遍历对象
1. 语法格式 for (变量 in 对象) { console.log(变量) } 2. 代码实例 // 遍历对象 var obj = { name: 'pink老师', age: 18, se ...
- 【Vue3+Express实战】项目开发环境搭建
大家好,我是半夏,一个刚刚开始写文的沙雕程序员.如果喜欢我的文章,可以关注 点赞 加我微信:frontendpicker,一起学习交流前端,成为更优秀的工程师-关注公众号:搞前端的半夏,了解更多前端知 ...