Springboot2.0从零开始搭建脚手架-初始化和整合MybatisPlus3.0+
初始化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打印
- 方式一:在项目主类中添加设置
public static void main(String[] args) { SpringApplication application=new SpringApplication(Application.class); /** * OFF G关闭 * CLOSED 后台控制台输出,默认就是这种 * LOG 日志输出 */ application.setBannerMode(Banner.Mode.OFF); application.run(args); }
- 方式二:
在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代码生成器整合
- 添加依赖
<!-- 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>
- 修改模板引擎
注意!如果您选择了非默认引擎,需要在 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());
- 配置数据源
这里使用的是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);
- 创建数据库表
create table T_USER ( userId VARCHAR2(60) not null, userName VARCHAR2(60), fullName VARCHAR2(60), email VARCHAR2(60), mobile VARCHAR2(60), status VARCHAR2(5) );
- 运行generator生成代码
- 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,并实现代码生成器完毕!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
[翻译]高阶Python一学就会
高阶Python一学就会 在前一篇文章中,我们学习了几个一般来说比较有用的Python语言的特性。 考虑到这篇文章是前一篇文章的续集,在这里我们进一步延伸一些显式使用装饰器的概念,我们并没有扰乱前一篇文章的内容。 装饰器 装饰器的概念展现了python领域内最漂亮和最强大的设计可能性之一,这不仅仅是在Python编程中,也在整个软件设计领域。本质上来说,装饰器就是一种包装,主要是想在不改变被包装的原代码的原则下,实现延伸代码功能性的目的。为了能让这个概念更清晰易懂,我们来从最基础的内容开始。 函数亦称为第一类对象 函数简单来说就是基于给定的自变量返回一个值。在python中,这些函数还有另外一种荣誉称号,叫作第一类对象。考虑到函数可以被像普通的对象一样被作为自变量传递,函数荣膺这项称号还真的是恰如其分。比如说,它们可以被作为自变量传递给其它函数,同时也可以被用做一个函数返回值。 作为自变量的函数 def greet(name): print ('Hello ' + name) def send_greetings(fun, name): fun(name) send_greetings...
- 下一篇
阿里云ubuntu16.04安装ruby
阿里云ubuntu16.04安装ruby0x0 准备环境:阿里云ubuntu16.04 目的:安装beef需要的ruby环境 更新软件 sudo apt-get update sudo apt-get upgrade sudo apt-get dist-upgrade 修改host,方便github vim /etc/hosts//先按i,切换模式 //将下面的内容复制进去 //完成后,按两下esc,输入:wq保存 //注意别漏了冒号 复制代码192.30.253.113 github.com 192.30.253.113 github.com 192.30.253.118 gist.github.com 192.30.253.119 gist.github.com复制代码 0x1 安装rvmRvm是ruby的版本管理器,阿里云ubuntu16.04的apt-get方式安装的ruby是2.3版本,很老,运行beef时会报错:不支持,升级至2.4以上 首先,使用gpg命令联系公钥服务器,并请求用于对每个RVM版本进行签名的RVM项目的密钥。这样可以验证您将下载的RVM版本的合法性。从您的...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- Windows10,CentOS7,CentOS8安装Nodejs环境
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- 设置Eclipse缩进为4个空格,增强代码规范
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS关闭SELinux安全模块
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Windows10,CentOS7,CentOS8安装MongoDB4.0.16