1. 创建Spring Boot项目
通过Spring Tool Suite的Spring Starter Project对话框,其实是把项目生成的工作委托上的Spring Initializr来做的,因此必须联网才能使用这一功能。
1.1 解决创建Spring Boot项目时遇到的问题
右键项目 --> Java EE Tools --> Generate Deployment Descriptor StubHelp --> Install New SoftWarehttps://otto.takari.io/content/sites/m2e.extras/m2eclipse-mavenarchiver/0.17.2/N/LATEST/
安装完成后就重启Spring Tool Suite,然后右键项目Maven --> Update Project。 2. 编写配置文件
package com.example.demo;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class ServletInitializer extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(DemoApplication.class); }}
package com.example.demo;import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;//import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;@SpringBootApplication(exclude={DruidDataSourceAutoConfigure.class})//@EnableAutoConfiguration(exclude={DruidDataSourceAutoConfigure.class})@ComponentScan(basePackages="com.example")@MapperScan(basePackages="com.example.mapper")public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); }}
//我们需要类似于web.xml的配置方式来启动spring上下文了,在Application类的同级添加一个SpringBootStartApplication类//参考:https://blog.csdn.net/liupantao/article/details/74942833package com.example.demo;import org.springframework.boot.builder.SpringApplicationBuilder;import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;public class SpringBootStartApplication extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(DemoApplication.class); } }
我们删除src/main/resources目录下的application.properties文件,新建一个application.yml文件。
相关阅读:# application.ymlserver: port: 8080 servlet: context-path: /demospring: mvc: view: prefix: /WEB-INF/views/ suffix: .jsp datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/demosql?useSSL=false username: root password: root mybatis: config-location: classpath:mybatis-config.xml mapper-locations: classpath:mapping/*.xml type-aliases-package: com.example.entity
2.1 MyBatis自动生成代码
参考:
在pom.xml文件中配置一下mybatis-generator-maven-plugin即可。4.0.0 org.springframework.boot spring-boot-starter-parent 2.1.1.RELEASE com.example demo 0.0.1-SNAPSHOT war demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-tomcat org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 org.springframework.boot spring-boot-starter-tomcat provided org.springframework.boot spring-boot-starter-test test mysql mysql-connector-java 5.1.38 com.alibaba druid-spring-boot-starter 1.1.0 org.springframework.boot spring-boot-devtools true demo org.springframework.boot spring-boot-maven-plugin org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 ${basedir}/src/main/resources/generator/generatorConfig.xml true true
创建数据库和表(手动创建- -)。
执行mybatis-generator:generate命令即可。3. 编写源码以及启动项目
package com.example.controller;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;import com.example.entity.User;import com.example.service.UserService;@RequestMapping("/hjj")@Controllerpublic class UserController { @Autowired private UserService userService; @RequestMapping("test") public String test(){ return "test"; } @ResponseBody @RequestMapping("/getUserById/{id}") public User getUserById(@PathVariable("id")Integer id){ return userService.getUserById(id); }}
package com.example.service;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import com.example.entity.User;import com.example.mapper.UserMapper;@Servicepublic class UserService { @Autowired private UserMapper userMapper; public User getUserById(Integer id){ return userMapper.selectByPrimaryKey(id); }}
将项目放到Tomcat就可以启动运行了。
注意:Maven打包项目时,在src/main/java目录下的Java文件会被编译成class文件,而其他类型的文件会被忽略。下载源码: 提取码:9wsn