MyBatis和Spring整合案例
1.所需要导入的jar文件
!--MyBatis和Spring的整合包 由MyBatis提供--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--MyBatis的核心jar文件--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency>
2.MyBatis和Spring整合(xml版)
2.1创建Dao层接口
public interface AccountDao { //查询所有账户 public List<Account> getAllAccount(); }
2.2创建Dao层接口小配置文件
<!--namespace需要指向接口全路径--> <mapper namespace="cn.spring.dao.AccountDao"> <select id="getAllAccount" resultType="cn.spring.entity.Account"> select * from accounts </select> </mapper>
2.3创建Service层接口
public interface AccountService { //查询所有账户 public List<Account> getAllAccount(); }
2.4创建Service层接口实现类
public class AccountServiceImpl implements AccountService { AccountDao accountDao; @Override public List<Account> getAllAccount() { return accountDao.getAllAccount(); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } }
2.5 配置applicationContext.xml文件
<context:component-scan base-package="cn.spring"/> <!-- 导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!--配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置mybatis的核心对象sqlsessionfactorybean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.spring.entity"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--MyBatis的Dao接口的包扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.dao"></property> </bean> <!--将Service实现类对象声明到Spring容器中 隐式注入--> <bean id="accountService" class="cn.spring.service.impl.AccountServiceImpl" autowire="byType"></bean>
2.6编写测试类
@Test public void getAll(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = (AccountService)context.getBean("accountService"); List<Account> allAccount = accountService.getAllAccount(); for (Account account:allAccount){ System.out.println(account.getAccountid()); } }
3.MyBatis和Spring整合(注解版)
3.1创建Dao层接口
@Repository public interface AccountDao { //查询所有账户 public List<Account> getAllAccount(); }
3.2同上2.2
3.3同上2.3
3.4创建Service层接口实现类
@Service("accountService") public class AccountServiceImpl implements AccountService { @Autowired AccountDao accountDao; @Override public List<Account> getAllAccount() { return accountDao.getAllAccount(); } public AccountDao getAccountDao() { return accountDao; } public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } }
3.5编写applicationContext.xml文件
<context:component-scan base-package="cn.spring"/> <!-- 导入jdbc.properties文件--> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!--配置数据源 spring内置的数据源--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"></property> <property name="url" value="${jdbc.url}"></property> <property name="username" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!--配置mybatis的核心对象sqlsessionfactorybean--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <property name="typeAliasesPackage" value="cn.spring.entity"></property> <property name="configLocation" value="classpath:mybatis-config.xml"></property> </bean> <!--MyBatis的Dao接口的包扫描器--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.spring.dao"></property> </bean>
3.6编写测试类
@Test public void getAll(){ ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml"); AccountService accountService = (AccountService)context.getBean("accountService"); List<Account> allAccount = accountService.getAllAccount(); for (Account account:allAccount){ System.out.println(account.getAccountid()); } }
MyBatis和Spring整合案例的更多相关文章
- MyBatis学习(四)MyBatis和Spring整合
MyBatis和Spring整合 思路 1.让spring管理SqlSessionFactory 2.让spring管理mapper对象和dao. 使用spring和mybatis整合开发mapper ...
- Mybatis与Spring整合,使用了maven管理项目,作为初学者觉得不错,转载下来
转载自:http://www.cnblogs.com/xdp-gacl/p/4271627.html 一.搭建开发环境 1.1.使用Maven创建Web项目 执行如下命令: mvn archetype ...
- Mybatis+struts2+spring整合
把student项目改造成ssm struts2 +mybatis+spring 1,先添加spring支持:类库三个,applicationContext.xml写在webinf下四个命名空间,监 ...
- mybatis与spring整合(基于配置文件)
本文主要介绍了如何将mybatis和spring整合在一起使用,本人使用的是mybatis3.05 + spring3.1.0M2 ,使用dbcp作为数据库连接池. 1.编写数据访问接口(UserDa ...
- mybatis与spring整合时读取properties问题的解决
在学习mybatis与spring整合是,想从外部引用一个db.properties数据库配置文件,在配置文件中使用占位符进行引用,如下: <context:property-placehold ...
- Spring+SpringMVC+MyBatis深入学习及搭建(九)——MyBatis和Spring整合
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6964162.html 前面讲到:Spring+SpringMVC+MyBatis深入学习及搭建(八)--My ...
- Mybatis第五篇【Mybatis与Spring整合】
Mybatis与Spring整合 既然我们已经学了Mybatis的基本开发了,接下来就是Mybatis与Spring的整合了! 以下使用的是Oracle数据库来进行测试 导入jar包 aopallia ...
- MyBatis 与 Spring 整合
MyBatis-Spring 项目 目前大部分的 Java 互联网项目,都是用 Spring MVC + Spring + MyBatis 搭建平台的. 使用 Spring IoC 可以有效的管理各类 ...
- Mybatis(六) Spring整合mybatis
心莫浮躁~踏踏实实走,一步一个脚印,就算不学习,玩,能干嘛呢?人生就是那样,要找点有意思,打发时间的事情来做,而钻研技术,动脑动手的过程,还是比其他工作更有意思些~ so,努力啥的都是强迫自己做自以为 ...
随机推荐
- nlp算法
人工智能算法大体上来说可以分类两类:基于统计的机器学习算法(Machine Learning)和深度学习算法(Deep Learning) 总的来说,在sklearn中机器学习算法大概的分类如下: 1 ...
- C#中HttpWebRequest、WebClient、HttpClient的使用
HttpWebRequest: 命名空间: System.Net,这是.NET创建者最初开发用于使用HTTP请求的标准类.使用HttpWebRequest可以让开发者控制请求/响应流程的各个方面,如 ...
- C++ enable_if 模板特化实例(函数返回值特化、函数参数特化、模板参数特化、模板重载)
1. enable_if 原理 关于 enable_if 原理这里就不细说了,网上有很多,可以参考如下教程,这里只讲解用法实例,涵盖常规使用全部方法. 文章1 文章2 文章3 1. 所需头文件 #in ...
- C之typedef应用
1.0关于typedef关键字的基础: https://www.cnblogs.com/anSn/p/8783347.html 1.1 typedef 修饰“函数类型” 的调用方法: 1)我们写一段普 ...
- PAT甲级题分类汇编——序言
今天开个坑,分类整理PAT甲级题目(https://pintia.cn/problem-sets/994805342720868352/problems/type/7)中1051~1100部分.语言是 ...
- try except 异常捕获的方法、断言的使用
except as e中的'e'的作用总结 - 2puT - CSDN博客 Python使用try except处理程序异常的三种常用方法分析 Python3和Python2 异常处理except的不 ...
- springboot 使用consul 读取配置文件(遇到的坑太多,没记录)
最终成功版. pom引入mavn依赖: <!--consul--> <dependency> <groupId>org.springframework.cloud& ...
- Visual Studio 2013/15/17小技巧
1.Ctrl + F10 可以直接运行到光标处,而不用F10 逐过程 F11 逐语句了 2.当有多个断点时,按F5可以切换到下一个断点. 3.Ctrl+Shift+空格 显示函数签名,上下键可以查看所 ...
- 每周分享五个 PyCharm 使用技巧(三)
文章首发于 微信公众号:Python编程时光 PyCharm 是大多数 Python 开发者的首选 IDE,每天我们都在上面敲着熟悉的代码,写出一个又一个奇妙的功能. 一个每天都在使用的工具,如果能掌 ...
- 前端相关UED团队和个人博客整理
平时收集的UED的团队和个人博客一些有关/*********************************这次真的是搬运工,原文转载自蓝色理想********************/ 前端团队推荐 ...