Springboot2.0从零开始搭建脚手架-初始化和整合MybatisPlus3.0+

简介: 初始化springboot项目添加web依赖,基于springboot2.1.3稳定版本初始化spring boot项目地址 https://start.spring.io/包名:com.nqmysb.

初始化springboot项目

添加web依赖,基于springboot2.1.3稳定版本
初始化spring boot项目地址 https://start.spring.io/
包名:com.nqmysb.scaffold
在这里插入图片描述

导入IDE

下载项目,我这里使用eclipse ,导入eclipse之后如下图
在这里插入图片描述

编写控制器

写一个控制器,并启动查看结果,这里直接将controller写在入口类

@RestController
@SpringBootApplication
public class SpringbootScaffoldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScaffoldApplication.class, args);
    }
    
    @RequestMapping("/index")
    public String index(String[] args) {
        
        System.out.println("hello world");
        
        return "springboot2.0 hello!";
    }

}

验证访问

通过访问浏览器查看结果 http://localhost:8080/index ,浏览器显示和控制台打印正常!
在这里插入图片描述
在这里插入图片描述

热加载配置

在项目pom.xml文件中加入热加载依赖,重新启动,修改代码时项目会自动重启更新项目。

        <!-- hot reload -->
        <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-devtools</artifactId>
              <optional>true</optional>
        </dependency>

自定义启动banner图案

在src/main/recesources下新建一个banner.txt文件,内容如下:佛系程序员

${AnsiColor.BRIGHT_YELLOW}
===================================================================================

 _____  _           _          _    _            _         _   _
 |  __ \| |         | |        | |  | |          | |       | | | |
 | |__) | |__   ___ | |_ ___   | |__| | __ _  ___| | ____ _| |_| |__   ___  _ __
 |  ___/| '_ \ / _ \| __/ _ \  |  __  |/ _` |/ __| |/ / _` | __| '_ \ / _ \| '_ \
 | |    | | | | (_) | || (_) | | |  | | (_| | (__|   < (_| | |_| | | | (_) | | | |
 |_|    |_| |_|\___/ \__\___/  |_|  |_|\__,_|\___|_|\_\__,_|\__|_| |_|\___/|_| |_|

////////////////////////////////////////////////////////////////////
//                          _ooOoo_                               //
//                         o8888888o                              //
//                         88" . "88                              //
//                         (| ^_^ |)                              //
//                         O\  =  /O                              //
//                      ____/`---'\____                           //
//                    .'  \\|     |//  `.                         //
//                   /  \\|||  :  |||//  \                        //
//                  /  _||||| -:- |||||-  \                       //
//                  |   | \\\  -  /// |   |                       //
//                  | \_|  ''\---/''  |   |                       //
//                  \  .-\__  `-`  ___/-. /                       //
//                ___`. .'  /--.--\  `. . ___                     //
//              ."" '<  `.___\_<|>_/___.'  >'"".                  //
//            | | :  `- \`.;`\ _ /`;.`/ - ` : | |                 //
//            \  \ `-.   \_ __\ /__ _/   .-` /  /                 //
//      ========`-.____`-.___\_____/___.-`____.-'========         //
//                           `=---='                              //
//      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^        //
//            佛祖保佑       永不宕机     永无BUG                    //
////////////////////////////////////////////////////////////////////


:: Spring Boot :: ${spring-boot.version}

启动项目,控制台输出:
在这里插入图片描述

关闭banner打印

  1. 方式一:在项目主类中添加设置
public static void main(String[] args) {
       SpringApplication application=new SpringApplication(Application.class);
       /**
        * OFF G关闭
        * CLOSED 后台控制台输出,默认就是这种
        * LOG 日志输出
        */
       application.setBannerMode(Banner.Mode.OFF);
       application.run(args);
    }
  1. 方式二:
    在application.yml配置文件中配置也行
spring:
  main:
    banner-mode: off

推荐的ASCII字符图案生成网站

http://www.network-science.de/ascii/
http://patorjk.com/software/taag/

集成Mybatisplus

添加 Mybatisplus ,druid, Oracle数据库驱动依赖 ,这里数据库用Oracle12c
Mybatisplus 安装文档参考:https://mp.baomidou.com/guide/install.html#release

        <!-- druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.8</version>
        </dependency>
        <!-- oracle7 -->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.2</version>
        </dependency>
        <!-- mybatis-plus  引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-               Spring-->
        <!-- <dependency>  mvc引入的包
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>3.1.0</version>
        </dependency> -->

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.1.0</version>
        </dependency>

WARNING : 引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。

配置扫描mapper的注解

@SpringBootApplication
@MapperScan("com.nqmysb.scaffold.mapper.*")
public class SpringbootScaffoldApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootScaffoldApplication.class, args);
    }
    
}

MyBatis-Plus代码生成器整合

  1. 添加依赖
        <!-- mybatis-plus  代码生成器-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.1.0</version>
        </dependency>
        
        <!-- mybatis-plus 代码生成器的模板引擎  默认是velocity -->
        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.28</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.1</version>
        </dependency>
  1. 修改模板引擎
    注意!如果您选择了非默认引擎,需要在 AutoGenerator 中 设置模板引擎
AutoGenerator generator = new AutoGenerator();

// set freemarker engine
generator.setTemplateEngine(new FreemarkerTemplateEngine());

// set beetl engine
generator.setTemplateEngine(new BeetlTemplateEngine());

// set custom engine (reference class is your custom engine class)
generator.setTemplateEngine(new CustomTemplateEngine());
  1. 配置数据源
    这里使用的是oracle数据库 官方实例用的是mysql
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.ORACLE);
        dsc.setTypeConvert(new OracleTypeConvert());
        dsc.setDriverName("oracle.jdbc.driver.OracleDriver");
        dsc.setUsername("LC_TEST");
        dsc.setPassword("LC_TEST");
        dsc.setUrl("jdbc:oracle:thin:@192.168.1.102:1521:orclpdb");
        mpg.setDataSource(dsc);
  1. 创建数据库表
create table T_USER
(
  userId VARCHAR2(60) not null,
  userName  VARCHAR2(60),
  fullName VARCHAR2(60),
  email VARCHAR2(60),
  mobile VARCHAR2(60),
  status VARCHAR2(5)
);
  1. 运行generator生成代码
    在这里插入图片描述
  2. Mapper生成没有方法,因为继承了BaseMapper的方法
    mapper记得加上@Mapper注解不然会报错
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.nqmysb.scaffold.user.mapper.TUserMapper' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

项目主配置

application.properties
server.port=8080
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:oracle:thin:@//192.168.8.150:1521/orclpdb
spring.datasource.username=LC_TEST
spring.datasource.password=LC_TEST
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

测试接口

在controller里面写查询方法测试接口

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liaocan
 * @since 2019-04-07
 */
@Controller
@RequestMapping("/user/t-user")
public class TUserController {
    
    
    @Autowired
    private TUserServiceImpl  TUserService;
    
    @RequestMapping("/getUser")
    @ResponseBody
    public TUser getUsers() {
        TUser data = TUserService.getById("007");
        System.out.println(data.getMobile()+"----");
        return data;
    }

}

启动运行项目,http://localhost:8080/user/t-user/getUser 访问接口
在这里插入图片描述
至此,springboot2.0整合Mybatis3.0,并实现代码生成器完毕!

目录
相关文章
|
2月前
|
SQL Java 关系型数据库
SpringBoot整合MybatisPlus
SpringBoot整合MybatisPlus
29 0
|
1月前
|
druid Java 数据库连接
Spring Boot3整合MyBatis Plus
Spring Boot3整合MyBatis Plus
39 1
|
10天前
|
缓存 前端开发 Java
SpringBoot启动后加载初始化数据
SpringBoot启动后加载初始化数据
|
18天前
|
Java 应用服务中间件
Springboot启动的时候初始化的线程池默认配置tomcat
Springboot启动的时候初始化的线程池默认配置tomcat
12 1
|
29天前
|
Java 关系型数据库 MySQL
springboot+mybatis-plus实例demo
springboot+mybatis-plus实例demo
28 0
|
1月前
|
SQL Java 关系型数据库
MyBatisPlus学习笔记(SpringBoot版)
MyBatisPlus学习笔记(SpringBoot版)
98 0
|
1月前
|
前端开发 JavaScript Java
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
springboot+mybatis plus+vue+elementui+axios 表格分页查询demo
32 0
|
1月前
|
前端开发 Java 数据安全/隐私保护
【五】springboot整合mybatis-plus
【五】springboot整合mybatis-plus
17 0
|
1月前
|
SQL Java 数据库连接
【四】springboot整合mybatis-plus
【四】springboot整合mybatis-plus
34 0
|
1月前
|
SQL 安全 Java
Springboot整合MybatisPlus
Springboot整合MybatisPlus
37 0