SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)
================================
©Copyright 蕃薯耀 2018年4月8日
http://www.cnblogs.com/fanshuyao/
源代码下载见:http://fanshuyao.iteye.com/blog/2415933
一、引入Mybatis依赖包:
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>1.3.2</version>
- </dependency>
数据库连接依赖包:
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.1.9</version>
- </dependency>
二、建立对应的表和Java实体(过程略)
三、建立实体对应的Mapper(如UserMapper )
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Options;
- import org.apache.ibatis.annotations.Select;
- import com.lqy.springboot.bean.User;
- @Mapper
- public interface UserMapper {
- @Select("select * from user where user_id = #{userId}")
- public User getById(Integer userId);
- @Options(useGeneratedKeys=true,keyProperty="userId")
- @Insert("insert into user(user_name,age) values(#{userName},#{age})")
- public Integer save(User user);
- }
注意:使用注解版需要在类上加上@Mapper注解,让SpringBoot自动扫描能识别
- @Mapper
问题:如果有很多Mapper接口,能不能一次性扫描呢?
有。
在程序启动入口加入注解:
- @MapperScan(basePackages= {"com.lqy.springboot.mapper"})
具体如下:
- import org.mybatis.spring.annotation.MapperScan;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- //Mybatis自动扫描包
- //@MapperScan(basePackages= {"com.lqy.springboot.mapper"})
- @SpringBootApplication
- public class SpringbootDatasourceApplication {
- public static void main(String[] args) {
- SpringApplication.run(SpringbootDatasourceApplication.class, args);
- }
- }
这样就能解决多Mapper需要添加注解的麻烦。
四、测试Mapper
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
- import com.lqy.springboot.bean.User;
- import com.lqy.springboot.mapper.UserMapper;
- @RestController
- public class UserController {
- @Autowired
- private UserMapper userMapper;
- @RequestMapping("/getUser")
- public User getUser(Integer userId) {
- if(userId == null) {
- userId = 1;
- }
- return userMapper.getById(userId);
- }
- @RequestMapping("/saveUser")
- public User saveUser(User user) {
- userMapper.save(user);
- return user;
- }
- }
五、SpringBoot Mybatis增加驼峰命名规则:
因为是注解版,没有配置文件,所以SpringBoot增加驼峰命名需要增加一个自定义配置类(ConfigurationCustomizer):
- import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- /**
- * mybatis 注解版
- *
- */
- @Configuration
- public class MybatisConfig {
- @Bean
- public ConfigurationCustomizer configurationCustomizer() {
- return new ConfigurationCustomizer() {
- @Override
- public void customize(org.apache.ibatis.session.Configuration configuration) {
- configuration.setMapUnderscoreToCamelCase(true);//设置驼峰命名规则
- }
- };
- }
- }
================================
©Copyright 蕃薯耀 2018年4月8日
http://www.cnblogs.com/fanshuyao/
SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)的更多相关文章
- springboot 学习之路 3( 集成mybatis )
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- springBoot和MyBatis整合中出现SpringBoot无法启动时处理方式
在springBoot和Myatis 整合中出现springBoot无法启动 并且报以下错误 Description: Field userMapper in cn.lijun.control ...
- SpringBoot+SpringCloud+vue+Element开发项目——集成MyBatis框架
添加mybatis-spring-boot-starter依赖 pox.xml <!--mybatis--> <dependency> <groupId>org.m ...
- Spring系列之新注解配置+Spring集成junit+注解注入
Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...
- Spring boot Mybatis整合构建Rest服务(超细版)
Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...
- SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.
SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...
- 项目SpringMVC+Spring+Mybatis 整合环境搭建(1)-> Spring+Mybatis搭建
目录结构 第一步:web.xml 先配置contextConfigLocation 对应的application-context.xml文件 打开webapp\WEB-INF\web.xml, 配置s ...
- mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题
<!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...
- 在springboot中集成mybatis开发
在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...
- SpringBoot+Shiro+mybatis整合实战
SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...
随机推荐
- Centos-Mariadb(新版mysql)无法远程连接
实际上之前有别人安装过了,对于这种交接的工作最难做,也就是沟通问题.我需要解决的问题是远程连接的问题,百度了一下后,被各路大佬绕的稀里糊涂,耗费了3 个小时 对于即将提交项目的我们来说,简直头疼.表示 ...
- Python 列表改为嵌套列表再转为矩阵
最终目的--------->[下面的形式我就可以直接转为矩阵,用mat即可] 下午为了实现list的这个功能,查找了好多库,但是都没有找到怎么做,索性就自己封装了一个函数实现了其功能: def ...
- Linux 压缩解压缩
先直接给出各种解压缩命令 .tar 压缩:tar cvf FileName.tar FileName 解压:tar xvf FileName.tar ------------------------- ...
- JAVA的基本数据类型和类型转换
一.数据类型 java是一种强类型语言,第一次申明变量必须说明数据类型,第一次变量赋值称为变量的初始化. java数据类型分为基本数据类型和引用数据类型 基本数据类型有4类8种 第一类(有4种)整型: ...
- C# Xamarin移动开发基础进修篇
一.课程介绍 英文原文:C# is the best language for mobile app development. Anything you can do in Objective-C, ...
- CSS 小技巧(不定时更新)
1.Web 文本中的省略号 在Web开发中,对于一种情况很常见.那就是,文本太长,而放置文本的容器不够长,而我们又不想让文本换行,所以,我们想使用省略号来解决这个问题.在今天HTML的标准中并没有相关 ...
- python接口自动化测试(四)-Cookie&Sessinon
掌握了前面几节的的内容,就可以做一些简单的http协议接口的请求发送了,但是这些还不够.HTTP协议是一个无状态的应用层协议,也就是说前后两次请求是没有任何关系的,那如果我们测试的接口之前有相互依赖关 ...
- Foxmail中配置O365邮箱和Hotmail邮箱
一.简述 平时我们经常使用第三方的邮箱工具来管理邮箱.可能性能比Outlook好很多,比如Foxmail,而且也能和Skype会议关联的很好.本文就讲如何在Outlook中关联Office365邮箱和 ...
- asp.net mvc 实战化项目之三板斧
laravel实战化项目之三板斧 spring mvc 实战化项目之三板斧 asp.net mvc 实战化项目之三板斧 接上文希望从一张表(tb_role_info 用户角色表)的CRUD展开asp. ...
- java logAspect
@Around("execution(* com.iotx.cep.biz.rpc.impl.*.*(..)) " + "&& !execution(* ...