一、将Maven项目初始化为SpringBoot项目
1.先创建一个Maven项目(可使用Maven原型创建:maven-archetype-webapp
),在pom.xml中配置好<parent>
标签和<dependency>
标签
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2.新建一个Application类,注解为@SpringBootApplication
@SpringBootApplication
public class Application
{
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
3.在controller
包中创建一个Controller进行测试 (@Controller
)
Controller默认返回至指定页面,若需返回自定义内容至页面,需加上@ResponseBody
注解,或@Controller
将改为@RestController
在类上加上@RequestMapping
为类相对于根的请求路径,
在方法上加上@RequestMapping
为方法相对于类的请求路径
@PostMapping
和@GetMapping
可以限制@RequestMapping
的请求方式
二、连接数据库,添加JDBC与事务
1.@ConfigurationProperties
和@Value
注解
前者的优势为松散绑定(驼峰,中划线,下划线转换)、对象引导(对象内部的属性注入)
2.添加依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
3.编写数据库配置(application.properties/yml)
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/*database*?serverTimezone=UTC&characterEncoding=utf-8
username: root
password: root
4.创建Service层测试类
先创建一个pojo对象,加上@Data
注解可以省去getter和setter方法
@Data
public class User
{
private String name;
private String id;
}
在service
包下创建Service1
类
@Service
public class Service1
{
@Transactional
public void saveUser(User user){
System.out.println("save user");
}
}
三、修改SpringMVC端口,配置拦截器
1.修改tomcat端口
在application.yml
中加入
server:
port: 80
2.新建一个拦截器,实现HandlerInterceptor
接口,并且加入拦截器链
在interceptor
包冲创建一个拦截器类
@Slf4j
public class Interceptor1 implements HandlerInterceptor
{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.debug("PreHandle");
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
log.debug("PostHandle");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
log.debug("AfterHandle");
}
}
注意,使用@Slf4j
需要设置日志级别,在application.yml
中加入
#日志记录级别
logging:
level:
*target*: debug
org.springframework: info
在config
包中创建一个类,在拦截器链中加入该拦截器
@Configuration
public class MvcConfig implements WebMvcConfigurer
{
@Bean
public Interceptor1 interceptor1(){
return new Interceptor1();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(interceptor1()).addPathPatterns("/*");
}
}
四、整合MyBatis与通用Mapper
1.添加依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
2.配置mybatis
在application.yml
中加入
mybatis:
#实体类别名包路径
type-aliases-package: com.bill.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
3.添加Mapper扫描注解
在Application
类上添加@MapperScan("包路径")
注解
注意!如果使用tk包中的通用Mapper,MapperScan引用的包应使用同样的包
4.创建Mapper测试类,使用通用Mapper
在mapper
包中创建Mapper1
类
public interface Mapper1 extends Mapper<User>{}
由于使用通用Mapper,很多常用方法不用写实现
Tips.在pojo类上添加注解
@Data
@Table(name = "user_table")
public class User
{
private String name;
//主键、主键回填
@Id
@KeySql(useGeneratedKeys = true)
private String id;
}
Tips.在Service中注入Mapper,在Controller中注入Service,进行测试
五、整合Junit测试
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.创建测试类
在需要创建测试的类名上按Ctrl+Shift+T
,创建一个测试类,并加上注解
@RunWith(SpringRunner.class)
@SpringBootTest
public class Service1Test
{
@Autowired
private Service1 service1;
@Test
public void queryById() {
//...
}
}
3.测试Junit是否能成功运行
如果报错:Could not initialize plugin: interface org.mockito.plugins.MockMaker,可以添加如下依赖
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.16</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.8.16</version>
<scope>test</scope>
</dependency>
六、整合Redis
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.在application.yml
中添加配置
spring:
redis:
host: localhost
port: 6379
3.添加测试类,注入Redis
@Autowired
private RedisTemplate redisTemplate;
七、附件
1.完整依赖
<parent>
<artifactId>spring-boot-starter-parent</artifactId>
<groupId>org.springframework.boot</groupId>
<version>2.1.5.RELEASE</version>
</parent>
<dependencies>
<!--Junit测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--SpringBoot相关启动器-->
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jdbc-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--通用Mapper-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.1.5</version>
</dependency>
<!--Junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy</artifactId>
<version>1.8.16</version>
</dependency>
<dependency>
<groupId>net.bytebuddy</groupId>
<artifactId>byte-buddy-agent</artifactId>
<version>1.8.16</version>
<scope>test</scope>
</dependency>
<!--Mysql连接-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<!--LOMBOK-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>