一、创建springboot项目
在idea中新建项目,选择springboot initializr,点击next创建项目即可。
【项目】创建时记得勾选lombok,springweb
?
?二、测试项目是否创建成功
在src下创建TestController进行测试
package com.example.sessiondemo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @description: TestController <br>
* @date: 2022/7/17 16:54 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
@RestController
@RequestMapping("test/")
public class TestController {
@GetMapping({"/","/hello"})
public String hello(){
return "hello";
}
}
启动项目测试
?项目能够正常启动并访问,说明第一步项目创建没有问题
三、整合jar包
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>session-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>session-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid依赖 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
四、配置applicartion.yml
server:
port: 8080 # 配置启动端口号
mybatis:
mapper-locations:
- classpath:mapper/*.xml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=GMT%2B8&useSSL=FALSE
username: root #数据库用户名,root为管理员
password: 123456 #该数据库用户的密码
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
五、创建mybatis-plus配置类
package com.example.sessiondemo.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
/**
* @description: MybatisPlusConfig <br>
* @date: 2022/7/17 17:30 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
@Component
public class MybatisPlusConfig {
/**
* mybatis-plus SQL执行效率插件【生产环境可以关闭】
*/
@Bean
public PerformanceInterceptor performanceInterceptor() {
return new PerformanceInterceptor();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
六、修改启动类
在启动类上添加mapper扫描.
?七、创建数据库
创建test数据库并创建user表
CREATE TABLE `user` (
`id` bigint(12) NOT NULL AUTO_INCREMENT,
`user_name` varchar(8) DEFAULT NULL COMMENT '用户名',
`password` varchar(50) DEFAULT NULL COMMENT '密码',
`sex` int(1) DEFAULT '0' COMMENT '性别',
`phone` varchar(20) DEFAULT NULL COMMENT '手机号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8
八、创建实体类、mapper、service
package com.example.sessiondemo.pojo;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.io.Serializable;
/**
* @description: User <br>
* @date: 2022/7/17 17:27 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
@Data
@TableName("user")
public class User implements Serializable {
@TableId(type = IdType.AUTO)
private Long id;
/**
* 用户名
*/
private String userName;
/**
* 密码
*/
private String password;
/**
* 性别
*/
private Integer sex;
/**
* 手机号
*/
private String phone;
}
package com.example.sessiondemo.dao;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sessiondemo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
/**
* @description: UserMapper <br>
* @date: 2022/7/17 17:32 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
package com.example.sessiondemo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.sessiondemo.pojo.User;
/**
* @description: UserService <br>
* @date: 2022/7/17 17:33 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
public interface UserService extends IService<User> {
}
package com.example.sessiondemo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sessiondemo.dao.UserMapper;
import com.example.sessiondemo.pojo.User;
import com.example.sessiondemo.service.UserService;
import org.springframework.stereotype.Service;
/**
* @description: UserServiceImpl <br>
* @date: 2022/7/17 17:34 <br>
* @author: William <br>
* @version: 1.0 <br>
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
九、测试配置是否成功
创建测试
package com.example.sessiondemo;
import com.example.sessiondemo.pojo.User;
import com.example.sessiondemo.service.UserService;
import net.bytebuddy.asm.Advice;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class SessionDemoApplicationTests {
@Autowired
UserService userService;
@Test
void contextLoads() {
List<User> users = userService.list();
users.forEach((item) ->{
System.out.println("item = " + item);
});
}
}
输出结果如下:
?成功执行,并且配置插件生效。
|