添加web依赖,基于springboot2.1.3稳定版本
初始化spring boot项目地址 https://start.spring.io/
包名:com.nqmysb.scaffold
下载项目,我这里使用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文件中加入热加载依赖,重新启动,修改代码时项目会自动重启更新项目。
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
在src/main/recesources下新建一个banner.txt文件,内容如下:佛系程序员
${AnsiColor.BRIGHT_YELLOW}
===================================================================================
_____ _ _ _ _ _ _ _
| __ \| | | | | | | | | | | | | |
| |__) | |__ ___ | |_ ___ | |__| | __ _ ___| | ____ _| |_| |__ ___ _ __
| ___/| '_ \ / _ \| __/ _ \ | __ |/ _` |/ __| |/ / _` | __| '_ \ / _ \| '_ \
| | | | | | (_) | || (_) | | | | | (_| | (__| < (_| | |_| | | | (_) | | | |
|_| |_| |_|\___/ \__\___/ |_| |_|\__,_|\___|_|\_\__,_|\__|_| |_|\___/|_| |_|
////////////////////////////////////////////////////////////////////
// _ooOoo_ //
// o8888888o //
// 88" . "88 //
// (| ^_^ |) //
// O\ = /O //
// ____/`---'\____ //
// .' \\| |// `. //
// / \\||| : |||// \ //
// / _||||| -:- |||||- \ //
// | | \\\ - /// | | //
// | \_| ''\---/'' | | //
// \ .-\__ `-` ___/-. / //
// ___`. .' /--.--\ `. . ___ //
// ."" '< `.___\_<|>_/___.' >'"". //
// | | : `- \`.;`\ _ /`;.`/ - ` : | | //
// \ \ `-. \_ __\ /__ _/ .-` / / //
// ========`-.____`-.___\_____/___.-`____.-'======== //
// `=---=' //
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ //
// 佛祖保佑 永不宕机 永无BUG //
////////////////////////////////////////////////////////////////////
:: Spring Boot :: ${spring-boot.version}
public static void main(String[] args) {
SpringApplication application=new SpringApplication(Application.class);
/**
* OFF G关闭
* CLOSED 后台控制台输出,默认就是这种
* LOG 日志输出
*/
application.setBannerMode(Banner.Mode.OFF);
application.run(args);
}
spring:
main:
banner-mode: off
http://www.network-science.de/ascii/
http://patorjk.com/software/taag/
添加 Mybatisplus ,druid, Oracle数据库驱动依赖 ,这里数据库用Oracle12c
Mybatisplus 安装文档参考:https://mp.baomidou.com/guide/install.html#release
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.8version>
dependency>
<dependency>
<groupId>com.oraclegroupId>
<artifactId>ojdbc7artifactId>
<version>12.1.0.2version>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>3.1.0version>
dependency>
WARNING : 引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。
@SpringBootApplication
@MapperScan("com.nqmysb.scaffold.mapper.*")
public class SpringbootScaffoldApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootScaffoldApplication.class, args);
}
}
<!-- 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 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());
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)
);
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里面写查询方法测试接口
/**
*
* 前端控制器
*
*
* @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,并实现代码生成器完毕!