MyBatis和Spring整合案例
2024-08-27 04:02:50
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()); } }
最新文章
- http status 301/302 &; java重定向/转发
- Python之实用的IP地址处理模块IPy
- Aspose.Cells设置自动列宽(最佳列宽)及一些方法总结
- LoadRunner参数化之数据取值和更新方式
- IOS启动页设置适应ios8/9
- H5调用Android拨打电话
- java中的不为空判断
- web设计经验<;二>;设计华丽的用户体验的6个热门技巧
- 乱谈Qt事件循环嵌套
- 浅谈Feature Scaling
- 在Netbeans上配置Android开发环境
- wifi 模块
- 手机自动化培训:Appium介绍
- 简单文本悬浮div提示效果
- Spring Boot:简介
- Java 多线程重排序的探究
- 四则运算第三次 PSP
- 【Spring】—— 自动装配
- Interceptor的基本介绍和使用
- 2018.11.24 poj2774Long Long Message(后缀数组)
热门文章
- LeetCode 746. 使用最小花费爬楼梯(Min Cost Climbing Stairs) 11
- 01 web概念概述
- Guava 工具类之Cache的使用
- Python 解LeetCode:394 Decode String
- fputcsv 导出excel,解决内存、性能、乱码、科学计数法问题
- Github相关问题集锦
- Python学习路线2019升级版(课程大纲+视频教程+网盘资源下载)
- Arm-Linux 移植 mtd-utils 1.x
- 记一次纯sqlite数据库的小项目开发经历
- idea 中 下载源码:Sources not download for: