近期项目使用springboot框架开发,学习实践的过程中,把总结的一些内容分享出来,和正在使用的童鞋一起学习。
Spring Boot 集成MyBatis有两种方式,一种简单的方式就是使用MyBatis官方提供的:
mybatis-spring-boot-starter
另外一种方式就是使用:
mybatis-spring
第一种方式暂时没实践,第二种方式需要自己写一些代码,但是可以很方便的控制MyBatis的各项配置,下面说明第二种方式的过程。
1、 pom.xml里面配置依赖
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring-version}</version>
</dependency>
2、 创建 mybatis 的配置类:
@Configuration
@EnableTransactionManagement
public class MyBatisConfiguration implements TransactionManagementConfigurer{
@Autowired
DataSource dataSource;
@Bean
public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
return new SqlSessionTemplate(sqlSessionFactory);
}
@Bean
public SqlSessionFactory sqlSessionFactory(){
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
bean.setDataSource(dataSource);
bean.setTypeAliasesPackage("com.aa.model"); //指定mybatis的模型层
SqlSessionFactory sqlFactory = null;
//添加XML目录
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
try {
bean.setMapperLocations(resolver.getResources("classpath:com/aa/mapper/*.xml"));
sqlFactory = bean.getObject();
} catch (IOException e) {
log.error("fail", e);
}catch (Exception e) {
log.error("fail", e);
}
return sqlFactory;
}
@Bean
@Override
public PlatformTransactionManager annotationDrivenTransactionManager() {
return new DataSourceTransactionManager(dataSource);
}
}
注意: 该类主要用于spring容器创建 SqlSessionFactory 和 SqlSessionTemplate
1)EnableTransactionManagement注解用来支持事务,关于事务控制,后续再具体讲。
2)dataSource注入的信息来自 application.properties配置的数据库相关信息,比如url,username等
3)mybatis涉及很多.xml文件,getResources支持通配
3、 mybatis涉及很多 Mapper接口,需创建这些接口的扫描类 MapperScannerConfigurer
@Configuration
@AutoConfigureAfter(MyBatisConfiguration.class)
public class MyBatisMapperScannerConfig {
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setBasePackage("com.aa.mapper"); //指定mybatis的mapper接口
return mapperScannerConfigurer;
}
}
1)必须配置AutoConfigureAfter注解,否则报错,作用: 声明在 MyBatisConfiguration 后执行
到此,springboot集成mybatis的代码结构大致完成。至于mybatis的模型层、mapper类、.xml文件等,可通过官方的插件自动生成。
关于mybatis 开发的一些细节问题,后续再另贴。
转载于:https://my.oschina.net/superwind20/blog/1524162
分享到:
相关推荐
项目描述 学生成绩管理系统,有三...spring boot+spring mvc+mybatis+layui+jquery+thymeleaf http://localhost:8080/Sys/loginView 管理员账号 admin admin 老师登录 2020031920 111111 学生账号登录 20200319 111111
总的来说,"基于Spring Boot+MyBatis的CRM客户管理系统"是一个全面的实践案例,它涵盖了后端开发的多个重要方面,包括框架集成、数据库操作、API设计、模块化开发等。对于Java开发者来说,深入学习并理解这个项目,...
基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + SpringMvc + Mybatis + Shiro+ Redis 开发单点登录管理系统 基于 SpringBoot + Spring + ...
RuoYi-Vue 全新 Pro 版本,...基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 微信小程序,支持 RBAC 动态权限、数据权限、SaaS 多租户、Flowable 工作流、三方登录、支付、短信、商城等功能
在本项目"Spring boot +mybatis + layui简单demo"中,我们主要探讨了如何整合这三个技术来构建一个功能完备的Web应用。Spring Boot是Java领域的一款快速开发框架,旨在简化Spring应用的初始搭建以及开发过程;...
标题中的"全注解 spring boot +spring security + mybatis+druid+thymeleaf+mysql+bootstrap"是一个集成开发环境的配置,涉及到的主要技术有Spring Boot、Spring Security、MyBatis、Druid、Thymeleaf、MySQL以及...
在本项目中,"spring boot+thymeleaf+mybatis+mysql" 是一个常见的Web应用开发框架组合,尤其适合初学者快速构建基于Java的Web应用程序。以下将详细阐述这四个关键技术及其整合过程中的关键知识点。 1. Spring Boot...
spring boot+mybatis+spring mvc整合开发超市订单后台管理系统 项目描述 超市订单管理系统 运行环境 jdk8(jdk7)+mysql+Eclipse+maven+tomcat7 项目技术(必填) springboot+spring mvc+mybatis+jquery+jsp ...
RuoYi-Vue 全新 Pro 版本,...基于 Spring Boot + MyBatis Plus + Vue & Element 实现的后台管理系统 + 微信小程序,支持 RBAC 动态权限、数据权限、SaaS 多租户、Flowable 工作流、三方登录、支付、短信、商城等功能。
Spring Boot + MyBatis + Mapper.xml 项目是一个基础但实用的Java Web开发框架组合,它大大简化了传统Spring项目的配置和启动过程。本项目利用Spring Boot的自动配置特性,配合MyBatis作为持久层框架,以及Mapper....
基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring Boot + MySQL 开发的博客系统源码 基于 Spring ...
总结,这个项目基础框架充分利用了Spring Boot的便捷性、MyBatis的灵活性以及Layui的美观性,为后续的业务开发打下了坚实的基础。开发者只需在此基础上扩展具体业务逻辑,就能快速构建出完整的Web应用。
全注解 spring boot +spring security + mybatis+druid+thymeleaf+jsp+mysql+bootstrap 支持thymeleaf和jsp并存 全注解 spring boot spring security thymeleaf+jsp同时使用 mybatis druid mysql bootstrap 访问 ...
mybatisSpring Boot + Mybatis开发实习生招聘网站 前端Vue.js实现双向数据绑定; 后端基于用户和基于项协同过滤推荐算法实现职位推荐;
在本项目中,我们主要探讨的是如何利用Spring Boot、MySQL、Mybatis以及Maven这四大核心技术,配合IDEA集成开发环境来实现一个基础的数据库增删改查功能。这是一个典型的Web应用开发流程,旨在帮助开发者快速搭建...
通过以上知识点的介绍,我们可以看到 Spring Boot 和 MyBatis 的集成使得开发更加便捷,减少了大量繁琐的配置工作,提高了开发效率。在实际项目中,结合这些功能模块,可以快速构建出稳定、功能丰富的后端服务。
在本项目中,我们主要探讨如何使用Spring Boot框架与MyBatis持久层框架结合MySQL数据库来构建一个完整的用户登录功能。下面将详细讲解这个过程中的关键知识点。 首先,`Spring Boot`是一个基于Java的框架,它简化了...
《Spring Boot+Mybatis图书...这个图书管理系统展示了Spring Boot和Mybatis在实际项目中的应用,通过学习和理解这个源码,开发者可以提升对Spring Boot集成Mybatis的实践能力,以及在Web应用开发中的整体架构设计能力。
通过这个项目,你可以深入理解Spring Boot的快速开发能力,MyBatis的数据库操作方式,以及Spring Security在构建安全Web应用中的作用。对于初学者来说,这是一个很好的起点,可以帮助他们建立起对现代Web开发框架的...