美文网首页
springboot 框架集成(一)

springboot 框架集成(一)

作者: amazing_s10plus | 来源:发表于2019-06-16 17:02 被阅读0次

参考 https://juejin.im/post/5caed4e4e51d456e27504b80
这是一个单体应用,使用springboot框架集成druid,redis,spring security,是一个最佳实践的框架,可以基于这个框架开发出不同的springboot项目。
代码地址:https://github.com/toyranger/spring-boot-personal-framework.git

1. 创建项目和模块

  1. 父工程
  2. 子模块
  <groupId>com.chins</groupId>
  <artifactId>springboot</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <modules>
    <module>chins-springboot-core</module>
    <module>chins-springboot-cache</module>
    <module>chins-springboot-common</module>
    <module>chins-springboot-security</module>
  </modules>
  <packaging>pom</packaging>

子模块依赖关系
core -> securitry -> cache -> common

2. 集成mybatis-plus druid

相关依赖放在父工程pom中,因为其他子模块也会 用到。

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      url: jdbc:mysql://192.168.0.102:3306/chins_springboot?useUnicode=true&characterEncoding=UTF-8&useSSL=false
      username: root
      password: 123456
      driver-class-name: com.mysql.cj.jdbc.Driver
      stat-view-servlet:
        login-username: admin
        login-password: admin
        enabled: true
        url-pattern: /druid/*

mybatis-plus:
  mapper-locations: classpath*:/mybatis-mappers/*
  # MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名,注册后在 Mapper 对应的 XML 文件中可以直接使用类名,而不用使用全限定的类名
  type-aliases-package: com.chins.springboot.core.entity
  # 数据库表与实体类的驼峰命名自动转换
  configuration:
    map-underscore-to-camel-case: true

3. 从controller - service - dao,测试整个流程是否可以正确执行

package com.chins.springboot.core.dao;

import com.chins.springboot.core.entity.ExampleEntity;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface ExampleDao {

  @Select("select * from user")
  List<ExampleEntity> getAllEntity();

}
package com.chins.springboot.core.service.impl;

import com.chins.springboot.core.dao.ExampleDao;
import com.chins.springboot.core.entity.ExampleEntity;
import com.chins.springboot.core.service.ExampleService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ExampleServiceImpl implements ExampleService {

  @Autowired
  private ExampleDao exampleDao;

  @Override
  public List<ExampleEntity> findAllEntity() {

    return exampleDao.getAllEntity();

  }
}

package com.chins.springboot.core.controller;

import com.chins.springboot.core.entity.ExampleEntity;
import com.chins.springboot.core.service.ExampleService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ExampleController {


  @Autowired
  private ExampleService exampleService;

  @GetMapping("/all")
  public List<ExampleEntity> getAllUsers() {

    return exampleService.findAllEntity();
  }
}

image.png

相关文章

网友评论

      本文标题:springboot 框架集成(一)

      本文链接:https://www.haomeiwen.com/subject/gaygfctx.html