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数据库驱动依赖 ,这里数据库用Oracle12cMybatisplus 安装文档参考: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,并实现代码生成器完毕!