您现在的位置是:首页 > 文章详情

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

日期:2019-04-06点击:515

初始化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,并实现代码生成器完毕!

原文链接:https://yq.aliyun.com/articles/697016
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章