SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/

源代码下载见:http://fanshuyao.iteye.com/blog/2415933

一、引入Mybatis依赖包:

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>1.3.2</version>
  5. </dependency>

数据库连接依赖包:

  1. <dependency>
  2. <groupId>mysql</groupId>
  3. <artifactId>mysql-connector-java</artifactId>
  4. <scope>runtime</scope>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.alibaba</groupId>
  8. <artifactId>druid</artifactId>
  9. <version>1.1.9</version>
  10. </dependency>

二、建立对应的表和Java实体(过程略)

三、建立实体对应的Mapper(如UserMapper )

  1. import org.apache.ibatis.annotations.Insert;
  2. import org.apache.ibatis.annotations.Mapper;
  3. import org.apache.ibatis.annotations.Options;
  4. import org.apache.ibatis.annotations.Select;
  5. import com.lqy.springboot.bean.User;
  6. @Mapper
  7. public interface UserMapper {
  8. @Select("select * from user where user_id = #{userId}")
  9. public User getById(Integer userId);
  10. @Options(useGeneratedKeys=true,keyProperty="userId")
  11. @Insert("insert into user(user_name,age) values(#{userName},#{age})")
  12. public Integer save(User user);
  13. }

注意:使用注解版需要在类上加上@Mapper注解,让SpringBoot自动扫描能识别

  1. @Mapper

 问题:如果有很多Mapper接口,能不能一次性扫描呢?

有。

在程序启动入口加入注解:

  1. @MapperScan(basePackages= {"com.lqy.springboot.mapper"})

具体如下:

  1. import org.mybatis.spring.annotation.MapperScan;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. //Mybatis自动扫描包
  5. //@MapperScan(basePackages= {"com.lqy.springboot.mapper"})
  6. @SpringBootApplication
  7. public class SpringbootDatasourceApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(SpringbootDatasourceApplication.class, args);
  10. }
  11. }

这样就能解决多Mapper需要添加注解的麻烦。

四、测试Mapper

  1. import org.springframework.beans.factory.annotation.Autowired;
  2. import org.springframework.web.bind.annotation.RequestMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import com.lqy.springboot.bean.User;
  5. import com.lqy.springboot.mapper.UserMapper;
  6. @RestController
  7. public class UserController {
  8. @Autowired
  9. private UserMapper userMapper;
  10. @RequestMapping("/getUser")
  11. public User getUser(Integer userId) {
  12. if(userId == null) {
  13. userId = 1;
  14. }
  15. return userMapper.getById(userId);
  16. }
  17. @RequestMapping("/saveUser")
  18. public User saveUser(User user) {
  19. userMapper.save(user);
  20. return user;
  21. }
  22. }

五、SpringBoot Mybatis增加驼峰命名规则:

因为是注解版,没有配置文件,所以SpringBoot增加驼峰命名需要增加一个自定义配置类(ConfigurationCustomizer):

  1. import org.mybatis.spring.boot.autoconfigure.ConfigurationCustomizer;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. /**
  5. * mybatis 注解版
  6. *
  7. */
  8. @Configuration
  9. public class MybatisConfig {
  10. @Bean
  11. public ConfigurationCustomizer configurationCustomizer() {
  12. return new ConfigurationCustomizer() {
  13. @Override
  14. public void customize(org.apache.ibatis.session.Configuration configuration) {
  15. configuration.setMapUnderscoreToCamelCase(true);//设置驼峰命名规则
  16. }
  17. };
  18. }
  19. }

================================

©Copyright 蕃薯耀 2018年4月8日

http://www.cnblogs.com/fanshuyao/

SpringBoot Mybatis整合(注解版),SpringBoot集成Mybatis(注解版)的更多相关文章

  1. springboot 学习之路 3( 集成mybatis )

    目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...

  2. springBoot和MyBatis整合中出现SpringBoot无法启动时处理方式

    在springBoot和Myatis   整合中出现springBoot无法启动   并且报以下错误 Description: Field userMapper in cn.lijun.control ...

  3. SpringBoot+SpringCloud+vue+Element开发项目——集成MyBatis框架

    添加mybatis-spring-boot-starter依赖 pox.xml <!--mybatis--> <dependency> <groupId>org.m ...

  4. Spring系列之新注解配置+Spring集成junit+注解注入

    Spring系列之注解配置 Spring是轻代码而重配置的框架,配置比较繁重,影响开发效率,所以注解开发是一种趋势,注解代替xml配置文件可以简化配置,提高开发效率 你本来要写一段很长的代码来构造一个 ...

  5. Spring boot Mybatis整合构建Rest服务(超细版)

     Springboot+ Mybatis+MySql整合构建Rest服务(涵盖增.删.改.查) 1.概要 1.1 为什么要使用Spring  boot? 1.1.1 简单方便.配置少.整合了大多数框架 ...

  6. SpringBoot SpringSecurity4整合,灵活权限配置,弃用注解方式.

    SpringSecurity 可以使用注解对方法进行细颗粒权限控制,但是很不灵活,必须在编码期间,就已经写死权限 其实关于SpringSecurity,大部分类都不需要重写,需要的只是妥善的配置. 每 ...

  7. 项目SpringMVC+Spring+Mybatis 整合环境搭建(1)-> Spring+Mybatis搭建

    目录结构 第一步:web.xml 先配置contextConfigLocation 对应的application-context.xml文件 打开webapp\WEB-INF\web.xml, 配置s ...

  8. mybatis整合spring,使用org.mybatis.spring.mapper.MapperScannerConfigurer扫描出现问题

    <!-- 加载配置文件 --> <context:property-placeholder location="classpath:db.properties" ...

  9. 在springboot中集成mybatis开发

    在springboot中利用mybatis框架进行开发需要集成mybatis才能进行开发,那么如何在springboot中集成mybatis呢?按照以下几个步骤就可以实现springboot集成myb ...

  10. SpringBoot+Shiro+mybatis整合实战

    SpringBoot+Shiro+mybatis整合 1. 使用Springboot版本2.0.4 与shiro的版本 引入springboot和shiro依赖 <?xml version=&q ...

随机推荐

  1. JWTtoken的原理以及在django中的应用

    JWT 在用户注册或者登陆完成之后,记录用户状态,或者为用户创建身份凭证(功能类似于session的作用). 什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而 ...

  2. Linux命令第一篇

    作业一: 1)   新建用户natasha,uid为1000,gid为555,备注信息为“master” natasha:x:1004:555:master:/home/natasha:/bin/ba ...

  3. 【二分+拓扑排序】Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348

    目录 Milking Order @USACO 2018 US Open Contest, Gold/upc_exam_6348 PROBLEM 题目描述 输入 输出 样例输入 样例输出 提示 MEA ...

  4. js生成1-100不重复的随机数及生成10个1-100不重复的随机数

    //生成1-100不重复的随机数 var count=100; var a=new Array(); for(var i=0;i<100;i++){ a[i]=i+1; } a.sort(fun ...

  5. c++中函数的参数传递,内联函数和默认实参的理解

    1.参数传递 1)函数调用时,c++中有三种传递方法:值传递.指针传递.引用传递. 给函数传递参数,遵循变量初始化规则.非引用类型的形参一相应的实参的副本初始化.对(非引用)形参的任何修改仅作用域局部 ...

  6. 【性能提升神器】Covering Indexes

    可能有小伙伴会问,Covering Indexes到底是什么神器呢?它又是如何来提升性能的呢?接下来我会用最通俗易懂的语言来进行介绍,毕竟不是每个程序猿都要像DBA那样深刻理解数据库,知道如何用以及如 ...

  7. PHP性能分析——xhprof(window 安装xhporf)

    1 下载xhprof的php扩展 因为官方的xhprof不支持php7,所以采用tideways版本的xhprof 下载地址:windows版tideways_xhprof 将windows版的dll ...

  8. C# 图解视频教程 全集200多集

    观看地址 https://www.bilibili.com/video/av21896829/?spm_id_from=333.23.home_video_list.3 学习交流地址 http://w ...

  9. Mac NPM 配置

    1.NPM 简介 NPM(node package manager),通常称为 node 包管理器,是目前世界上最大的开源库生态系统.使用 NPM 可以对 node 包进行安装.卸载.更新.查看.搜索 ...

  10. c# 基于redis分布式锁

    在单进程的系统中,当存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步,使其在修改这种变量时能够线性执行消除并发修改变量. 而同步的本质是通过锁来实现的.为了实现多个线程在 ...