All Projects → MyHerux → spring-boot-demo

MyHerux / spring-boot-demo

Licence: other
SpringBoot Demo.集成统一异常处理,Swagger,Druid,Mybatis,Redis,Mongo.

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to spring-boot-demo

Spring Boot Leaning
Spring Boot 2.X 最全课程代码
Stars: ✭ 2,008 (+9461.9%)
Mutual labels:  druid, mybatis
My Blog Layui
layui 版本的 My-Blog : A simple & beautiful blogging system implemented with spring-boot & layui & thymeleaf & mybatis My Blog 是由 SpringBoot + Layui + Mybatis + Thymeleaf 等技术实现的 Java 博客系统,页面美观、功能齐全、部署简单及完善的代码,一定会给使用者无与伦比的体验
Stars: ✭ 204 (+871.43%)
Mutual labels:  druid, mybatis
Seconds Kill
基于 Springboot + Redis + Kafka 的秒杀系统,乐观锁 + 缓存 + 限流 + 异步,TPS 从 500 优化到 3000
Stars: ✭ 180 (+757.14%)
Mutual labels:  druid, mybatis
Lovinghome Real Estate Platform
⚡️基于springboot+MyBatis+FreeMarker+redis+nginx+Echarts+druid等技术的JavaWeb项目------恋家房产平台(采用B/S架构,项目包含前后台,分为前台展示系统及后台管理系统。前台系统包含首页门户、登录注册、房产推荐、房产详情、热门房产、房产及小区搜索、经纪人列表及经纪机构创建、创建房产、房产百科、地图找房、用户个人中心、房产评论、房产打分等模块。 后台管理系统包含房产信息管理、用户管理、管理员管理、小区信息管理、博客管理、评论管理、经纪人管理、系统统计与多种图表展示、数据报表导入导出等模块。系统介绍及详细功能点、技术点见项目内文档描述)
Stars: ✭ 140 (+566.67%)
Mutual labels:  druid, mybatis
RuoYi-Vue-Oracle
🎉 (RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统
Stars: ✭ 225 (+971.43%)
Mutual labels:  druid, mybatis
Skill6 Website Backend
java进阶开发,高级版web项目。基于dubbo实现分布式微服务架构,基于spring boot、spring mvc、mybatis、docker、ELK、redis、vue.js、node.js等技术实现的技术分享交流网站。网站名称:技术流,英文名:skill6。主要功能有:登录注册、单点登录、restful设计、文章及评论、代码及资源下载、主题讨论等。持续更新中...
Stars: ✭ 140 (+566.67%)
Mutual labels:  druid, mybatis
Springcloud Miaosha
一个基于spring cloud Greenwich的简单秒杀电子商城项目,适合新人阅读。A simple spring cloud based seckill shopping mall project, suitable for young people to read. It can be used as a paper material for academic defense.
Stars: ✭ 187 (+790.48%)
Mutual labels:  druid, mybatis
Dubbo2.5 Spring4 Mybastis3.2 Springmvc4 Mongodb Redis
dubbo2.5-spring4-mybastis3.2-springmvc4-mongodb-redis整合
Stars: ✭ 123 (+485.71%)
Mutual labels:  druid, mybatis
Spring Cloud Shop
spring cloud 版分布式电商项目,全力打造顶级多模块,高可用,高扩展电商项目
Stars: ✭ 248 (+1080.95%)
Mutual labels:  druid, mybatis
Spring Dubbo Service
微服务 spring dubbo项目:dubbo rpc;druid数据源连接池;mybatis配置集成,多数据源;jmx监控MBean;定时任务;aop;ftp;测试;Metrics监控;参数验证;跨域处理;shiro权限控制;consul服务注册,发现;redis分布式锁;SPI服务机制;cat监控;netty服务代理;websocket;disconf;mongodb集成;rest;docker;fescar
Stars: ✭ 224 (+966.67%)
Mutual labels:  druid, mybatis
Ruoyi Oracle
(RuoYi)官方仓库 基于SpringBoot的权限管理系统 易读易懂、界面简洁美观。 核心技术采用Spring、MyBatis、Shiro没有任何其它重度依赖。直接运行即可用
Stars: ✭ 134 (+538.1%)
Mutual labels:  druid, mybatis
springboot-learning-demo
springboot学习示例
Stars: ✭ 17 (-19.05%)
Mutual labels:  druid, mybatis
Spring Boot Quick
🌿 基于springboot的快速学习示例,整合自己遇到的开源框架,如:rabbitmq(延迟队列)、Kafka、jpa、redies、oauth2、swagger、jsp、docker、spring-batch、异常处理、日志输出、多模块开发、多环境打包、缓存cache、爬虫、jwt、GraphQL、dubbo、zookeeper和Async等等📌
Stars: ✭ 1,819 (+8561.9%)
Mutual labels:  druid, mybatis
Okhelper Service
OK帮 云进销存 (SpringBoot Restful 全家桶)
Stars: ✭ 146 (+595.24%)
Mutual labels:  druid, mybatis
Nimrod
Nimrod - 基于 Spring Boot 构建 的 Java Web 平台企业级单体应用快速开发框架,适合中小型项目的应用和开发。所采用的技术栈包括 Spring Boot、Spring、Spring Web MVC、MyBatis、Thymeleaf 等,遵守阿里巴巴 Java 开发规约,帮助养成良好的编码习惯。整体采用 RBAC ( Role-Based Access Control ,基于角色的访问控制),具有严格的权限控制模块,支持系统与模块分离开发。最后希望这个项目能够对你有所帮助。Nimrod 开发交流群:547252502(QQ 群)
Stars: ✭ 125 (+495.24%)
Mutual labels:  druid, mybatis
Spring Boot Sample
Spring-boot 集成druid数据库连接池,Spring-boot实现druid的动态数据源,Spring-boot实现定时任务schedule,spring-boot集成mybatis
Stars: ✭ 180 (+757.14%)
Mutual labels:  druid, mybatis
Autumn
Autumn-Blog and Autumn-CMS
Stars: ✭ 101 (+380.95%)
Mutual labels:  druid, mybatis
Ruoyi Vue Fast
(RuoYi)官方仓库 基于SpringBoot,Spring Security,JWT,Vue & Element 的前后端分离权限管理系统
Stars: ✭ 107 (+409.52%)
Mutual labels:  druid, mybatis
Layim
基于HTML5 WebSocket的一款IM即时通讯软件,使用Gradle集成了Scala、SpringBoot、Spring MVC、Mybatis、Redis等,前端使用了LayIm框架
Stars: ✭ 218 (+938.1%)
Mutual labels:  druid, mybatis
learning
学习笔记
Stars: ✭ 17 (-19.05%)
Mutual labels:  druid, mybatis

spring-boot-demo

  • 统一异常处理
  • Swagger
  • Druid
  • Mybatis
  • Redis
  • MongoDB

使用同一的异常处理

  • 配置 GlobalExceptionHandler.java

      @RestControllerAdvice
      public class GlobalExceptionHandler {
          private Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
    
          @ExceptionHandler(value = BusinessException.class)
          public JSONObject BusinessExceptionHandler(BusinessException exception) throws IOException {
              logger.info(exception.toString());
              JSONObject jsonObject = new JSONObject();
              jsonObject.put("response_code", exception.getCode());
              jsonObject.put("message", exception.getMessage());
              return jsonObject;
          }
    
          @ExceptionHandler(value = Exception.class)
          public JSONObject OtherExceptionHandler(Exception e) throws IOException {
              logger.error(e.toString());
              JSONObject jsonObject = new JSONObject();
              jsonObject.put("response_code", "300005");
              jsonObject.put("message", "系统异常");
              return jsonObject;
          }
      }
    

集成swagger

  • 配置 pom.xml

        <!--swagger-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.2.2</version>
        </dependency>
    
    
  • 使用

      @ApiOperation(value = "获取信息", response = Info.class)
      @ApiImplicitParams({
              @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer")
      })
      @ApiResponses({
              @ApiResponse(code = 200, message = "success")
      })
      @GetMapping(value = "/get_user")
      public Info GetUser() {
          return infoMapper.findById(1);
      }
    
  • 访问:http://localhost:8089/swagger-ui.html

集成Druid

  • 配置 appplication.yaml

      # mysql-druid
      spring:
          datasource:
              type: com.alibaba.druid.pool.DruidDataSource
              driver-class-name: com.mysql.jdbc.Driver
              url: jdbc:mysql://localhost:3306/xus
              username: root
              password: 4321
              # 初始化大小,最小,最大
              initialSize: 5
              minIdle: 5
              maxIdle: 100
              maxActive: 200
              # 配置获取连接等待超时的时间
              maxWait: 5000
              # 每次使用连接时进行校验,会影响系统性能。默认为false
              testOnBorrow: false
              # 验证使用的SQL语句
              validationQuery: SELECT 1
              # 指明连接是否被空闲连接回收器(如果有)进行检验.如果检测失败,则连接将被从池中去除
              testWhileIdle: true
              # 每30秒运行一次空闲连接回收器(默认-1)
              timeBetweenEvictionRunsMillis: 30000
              # 池中的连接空闲30分钟后被回收(默认30分钟)
              minEvictableIdleTimeMillis: 1800000
              # 在每次空闲连接回收器线程(如果有)运行时检查的连接数量(设置为和maxIdle一样)
              numTestsPerEvictionRun: 100
    
  • 使用DataSourceConfig来替换默认配置

    因为Druid暂时没有专门提供给SpringBoot的定制版本,所以需要手动配置Druid的属性。

    DataSourceConfig.java:

      @Value("${spring.datasource.url}")
      private String url;
    
      @Value("${spring.datasource.username}")
      private String user;
    
      @Value("${spring.datasource.password}")
      private String password;
    
      @Value("${spring.datasource.driver-class-name}")
      private String driverClass;
    
      @Value("${spring.datasource.maxActive}")
      private Integer maxActive;
    
      @Value("${spring.datasource.initialSize}")
      private Integer initialSize;
    
      @Value("${spring.datasource.minIdle}")
      private Integer minIdle;
    
      @Value("${spring.datasource.maxWait}")
      private Integer maxWait;
    
      @Bean(name = "DataSource")
      public DataSource DataSource() {
          DruidDataSource dataSource = new DruidDataSource();
          dataSource.setDriverClassName(driverClass);
          dataSource.setUrl(url);
          dataSource.setUsername(user);
          dataSource.setPassword(password);
          dataSource.setMaxActive(maxActive);
          dataSource.setInitialSize(initialSize);
          dataSource.setMinIdle(minIdle);
          dataSource.setMaxWait(maxWait);
          return dataSource;
      }
    
    
  • 配置状态监控的FilterServlet

    Filter与Servlet配置.

    直接访问http://localhost:8080/druid/login.html,输入账号密码即可进入druid监控。

集成Mybatis

  • 配置DataSourceConfig

    使用 setMapUnderscoreToCamelCase(true) 来将数据库的下划线格式自动转换为驼峰格式。

      @Bean(name = "TransactionManager")
      public DataSourceTransactionManager TransactionManager() {
          return new DataSourceTransactionManager(DataSource());
      }
    
      @Bean(name = "SqlSessionFactory")
      public SqlSessionFactory SqlSessionFactory(@Qualifier("DataSource") DataSource dataSource) throws Exception {
          final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
          sessionFactory.setDataSource(dataSource);
          sessionFactory.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
          return sessionFactory.getObject();
      }
    
  • 推荐使用Mapper注解,替代复杂的XML文件

    @Mapper
    public interface InfoMapper {
    
        @Select("select * from info where id=#{id}")
        Info findById(@Param("id") Integer id);
    }
    
  • Mapper的使用

      private final InfoMapper infoMapper;
    
      @Autowired
      public MysqlController(InfoMapper infoMapper) {
          this.infoMapper = infoMapper;
      }
    
      @ApiOperation(value = "获取信息", response = Info.class)
      @ApiImplicitParams({
              @ApiImplicitParam(name = "id", value = "用户id", dataType = "Integer")
      })
      @ApiResponses({
              @ApiResponse(code = 200, message = "success")
      })
      @GetMapping(value = "/get_user")
      public Info GetUser() {
          return infoMapper.findById(1);
      }
    

集成Redis

  • 配置 appplication.yaml

      redis:
          database: 0       # Redis数据库索引(默认为0)
          host:
          port:
          password: :
          timeout:    # ms
          pool:
            max-active: 12   # 连接池最大连接数(使用负值表示没有限制)
            max-wait: -1     # 连接池最大阻塞等待时间(使用负值表示没有限制)
            max-idle: 4      # 连接池中的最大空闲连接
            min-idle: 1      # 连接池中的最小空闲连接
    
  • 使用 StringRedisTemplate

      private final StringRedisTemplate redisTemplate;
    
      @Autowired
      public RedisController(StringRedisTemplate redisTemplate) {
          this.redisTemplate = redisTemplate;
      }
    
    
      @ApiOperation(value = "测试Redis")
      @GetMapping(value = "/set")
      public void set() {
          // 保存字符串
          redisTemplate.opsForValue().set("aaa", "111");
    
          redisTemplate.opsForValue().get("aaa");
    
          redisTemplate.opsForSet().add("test_set", "1111");
          redisTemplate.opsForSet().add("test_set", "2222");
      }
      }
    

集成Mongo

  • 配置 appplication.yaml

    # mongo
    data:
      mongodb:
        database:
        host:
        port:
        username:
        password:
    
  • 使用 MongoTemplate

      private final MongoTemplate mongoTemplate;
    
      @Autowired
      public MongoController(MongoTemplate mongoTemplate) {
          this.mongoTemplate = mongoTemplate;
      }
    
    
      @GetMapping(value = "/test")
      public void mongoT1() {
          //对象存储
          String BASE_INFO_COLLECTION = "baseInfo";
          JSONObject json = new JSONObject();
          json.put("id", "123");
          mongoTemplate.insert(json, BASE_INFO_COLLECTION);
    
          Query query = new Query(Criteria.where("id").is("123"));
          Long count = mongoTemplate.count(query, BASE_INFO_COLLECTION);
          System.out.println("count=" + count);
      }
    
      @GetMapping(value = "/test2")
      public void MongoT2() {
          Info info = new Info();
          info.setId(111);
          info.setName("dou");
          mongoTemplate.save(info);
      }
    
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].