springboot-mybatisplus-layui结构
分层结构如下所示
yml配置文件
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot-mybatis-layui?useSSL=false&serverTimezone=UTC
username: root
password: 123456
max-active: 20
max-wait: 5000
initial-size: 1
filters: stat,log4j,wall
validation-query: SELECT 'x'
enable: true
stat-view-servlet:
enabled: true
login-username: root
login-password: 123456
url-pattern: /druid/*
mybatis-plus:
mapper-locations: classpath:mapper/*/*Mapper.xml
global-config:
db-config:
id-type: auto
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
Mapper层
@Mapper
public interface UserMapper extends BaseMapper<User> {
}
@Mapper注释 + 继承BaseMapper<entity>
Service层
public interface UserService extends IService<User> {
}
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
Entity层
@Data
@ToString
@TableName("user")
@EqualsAndHashCode(callSuper = false)
public class User implements Serializable {
private Integer id;
private String userName;
private Integer point;
private String sex;
private Integer score;
private String city;
private String signaure;
private String occupation;
private Integer wealth;
@TableField(exist = false) //抽离表示不是数据库中的元素
private Integer page=1;
@TableField(exist = false)
private Integer limit=10;
}
controller层
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
//查询所有用户
@RequestMapping("/listUser")
public DataView getALllUser(User user){
IPage<User> page=new Page<>(user.getPage(),user.getLimit());
// List<User> list = userService.list();
userService.page(page);
return new DataView(page.getTotal(),page.getRecords());
}
@PutMapping("/deleteUser")
public DataView deleteUser(User user){
boolean b = userService.removeById(user.getId());
DataView dataView=new DataView();
if(b){
dataView.setCode(200);
dataView.setMsg("删除成功!");
}else {
dataView.setCode(500);
dataView.setMsg("删除失败!");
}
return dataView;
}
}
vo层
将数据分装成Layui可以解析的格式
@Data
@NoArgsConstructor
@AllArgsConstructor
public class DataView {
private Integer code = 0;
private String msg = "";
private Long count = 0L;
private Object data;
public DataView(Long count,Object data){
this.count=count;
this.data=data;
}
public DataView(Object data){
this.data=data;
}
}
config层
@Configuration
@ConditionalOnClass(value = {PaginationInnerInterceptor.class})
public class MybatisPlusConfig {
@Bean
public PaginationInnerInterceptor paginationInnerInterceptor(){
return new PaginationInnerInterceptor();
}
}
配置分页
log4j.properties
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
放在resource层,与application.yml 同级