Commit fcc292ab by 周田

feat(core): mybatis-plus 接入

parent f3d00d84
......@@ -26,12 +26,31 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--数据校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<!--db-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
<version>3.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-3-starter</artifactId>
<version>1.2.21</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
......
package org.linker.springboot;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@SpringBootApplication
@MapperScan("org.linker.springboot.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
......
......@@ -32,11 +32,11 @@ public class SpringMVCConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 拦截器一
registry.addInterceptor(this.firstInterceptor()).addPathPatterns("/**");
// 拦截器二
registry.addInterceptor(this.secondInterceptor()).addPathPatterns("/users/current_user");
// 拦截器三
registry.addInterceptor(this.thirdInterceptor()).addPathPatterns("/**");
// // 拦截器一
// registry.addInterceptor(this.firstInterceptor()).addPathPatterns("/**");
// // 拦截器二
// registry.addInterceptor(this.secondInterceptor()).addPathPatterns("/users/current_user");
// // 拦截器三
// registry.addInterceptor(this.thirdInterceptor()).addPathPatterns("/**");
}
}
......@@ -8,6 +8,7 @@ import org.linker.springboot.core.exception.ServiceException;
import org.linker.springboot.core.vo.CommonResult;
import org.linker.springboot.dto.UserAddDTO;
import org.linker.springboot.dto.UserUpdateDTO;
import org.linker.springboot.pojo.User;
import org.linker.springboot.service.UserService;
import org.linker.springboot.vo.UserVO;
import org.slf4j.Logger;
......@@ -177,4 +178,14 @@ public class UserController {
logger.info("[validate] userVO: {}", userVO.toString());
return CommonResult.success(userVO);
}
@GetMapping("/get_all_users")
public CommonResult<List<User>> getAllUsersFromDB() {
return CommonResult.success(userService.getAllUserFromDB());
}
@GetMapping("/get_user_by_id")
public CommonResult<User> getUserById(@Min(value = 1, message = "用户编号必须大于 0") Integer id) {
return CommonResult.success(userService.getUserById(id));
}
}
package org.linker.springboot.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.linker.springboot.pojo.User;
public interface UserMapper extends BaseMapper<User> {
}
package org.linker.springboot.pojo;
public class User {
private Long id;
private String username;
private Integer age;
private String email;
public User() {
}
public User(long id, String username, int age, String email) {
this.id = id;
this.username = username;
this.age = age;
this.email = email;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getusername() {
return username;
}
public void setusername(String username) {
this.username = username;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", age=" + age +
", email='" + email + '\'' +
'}';
}
}
\ No newline at end of file
package org.linker.springboot.service;
import org.linker.springboot.mapper.UserMapper;
import org.linker.springboot.pojo.User;
import org.linker.springboot.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public UserVO get(Integer id) {
return new UserVO().setId(id).setUsername("test");
}
public List<User> getAllUserFromDB() {
return userMapper.selectList(null);
}
public User getUserById(Integer id) {
return userMapper.selectById(id);
}
}
spring:
application:
name: springboot
# DataSource Config
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.0.210:3306/springboot_study?useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: root
password: lhr
initial-size: 1 # ?????
min-idle: 1 # ???????
max-active: 20 # ???????
max-wait: 600000 # ???????????????????
time-between-eviction-runs-millis: 60000 # ???????????????????????????????
min-evictable-idle-time-millis: 300000 # ??????????????????????
max-evictable-idle-time-millis: 900000 # ??????????????????????
validation-query: SELECT 1 FROM DUAL # ??????????
test-while-idle: true
test-on-borrow: false
test-on-return: false
mybatis-plus:
configuration:
# MyBatis ??
map-underscore-to-camel-case: true
global-config:
# ????
db-config:
# ?????
id-type: auto
logging:
level:
org.linker.springboot.mapper: debug
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment