赞
踩
- <!-- MySQL驱动 -->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- mybatis-plus启动器 -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.5.1</version>
- </dependency>
- <!-- 代码生成器 -->
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-generator</artifactId>
- <version>3.5.2</version>
- </dependency>
- <!-- 模板引擎 -->
- <dependency>
- <groupId>org.freemarker</groupId>
- <artifactId>freemarker</artifactId>
- <version>2.3.29</version>
- </dependency>
- <!-- 校验工具 -->
- <dependency>
- <groupId>javax.validation</groupId>
- <artifactId>validation-api</artifactId>
- <version>2.0.1.Final</version>
- </dependency>
- <!-- lombok -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- server.port=9800
-
- #dbname为具体连接的数据库名
- spring.datasource.url=jdbc:mysql://localhost:3306/dbname?&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true
- spring.datasource.username=root
- spring.datasource.password=123456
- spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
-
- #上传文件大小限制
- spring.servlet.multipart.max-file-size=10000MB
- spring.servlet.multipart.max-request-size=10000MB
-
- #xml文件存放位置为resources目录下新建mapper文件夹
- mybatis-plus.global-config.db-config.logic-delete-field=isDeleted
- mybatis-plus.mapper-locations=classpath:mapper/*.xml
- mybatis-plus.type-aliases-package=com.example.collection.dao
- @Configuration
- @MapperScan("com.example.collection.dao")
- public class MybatisPlusConfig {
-
- @Bean
- public MybatisPlusInterceptor mybatisPlusInterceptor() {
- MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
- // 分页插件
- interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
- // 乐观锁插件
- interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
- return interceptor;
- }
-
- }
其中mapper文件存放在dao文件夹下
- public class CommonResult<T> {
-
- private T data;
- private String msg;
- private Integer code;
-
- public CommonResult() {
-
- }
-
- public CommonResult(T data) {
- this.data = data;
- this.msg = "success";
- this.code = 200;
- }
-
- public CommonResult(T data, String msg, Integer code) {
- this.data = data;
- this.msg = msg;
- this.code = code;
- }
-
- public T getData() {
- return data;
- }
-
- public void setData(T data) {
- this.data = data;
- }
-
- public String getMsg() {
- return msg;
- }
-
- public void setMsg(String msg) {
- this.msg = msg;
- }
-
- public Integer getCode() {
- return code;
- }
-
- public void setCode(Integer code) {
- this.code = code;
- }
- }
- @TableName(value = "user", autoResultMap = true)
- public class User {
-
- private Long id;
-
- private String account;
-
- private String username;
-
- private String password;
-
- //get&set...
- }
- public class PageParam {
-
- @ApiModelProperty("当前页, 默认1")
- private long current = 1;
- @ApiModelProperty("每页显示条数,默认10")
- private long size = 10;
- @ApiModelProperty("正序排列字段,多个以逗号隔开")
- private String ascs;
- @ApiModelProperty("反序排列字段,多个以逗号隔开")
- private String descs;
-
- /**
- * 将分页参数转换成Page
- *
- * @param <T> 实体类型
- * @return Page
- */
- public <T> Page<T> toPage() {
- Page<T> page = new Page<>();
- page.setCurrent(this.getCurrent());
- page.setSize(this.getSize());
- if (StrUtil.isNotBlank(this.getAscs())) {
- page.addOrder(OrderItem.ascs(this.getAscs().split(",")));
- }
- if (StrUtil.isNotBlank(this.getDescs())) {
- page.addOrder(OrderItem.descs(this.getDescs().split(",")));
- }
- return page;
- }
-
- //set&get...
-
- }
- public interface UserMapper extends BaseMapper<User> {
-
- }
其中命名空间namespace一定要准确对应mapper文件
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.example.collection.dao.UserMapper">
-
- </mapper>
- public interface UserService extends IService<User> {
-
- void removeUser(List<Long> ids);
-
- IPage<User> pageUser(PageParam page, String account);
-
- }
- @Service
- public class UserImpl extends ServiceImpl<UserMapper, User> implements UserService {
-
- @Override
- public void removeUser(List<Long> ids) {
- for (Long id: ids) {
- lambdaUpdate()
- .eq(User::getId, id)
- .remove();
- }
- }
-
- @Override
- public IPage<User> pageUser(PageParam page, String account) {
- return this.lambdaQuery()
- .eq(StrUtil.isNotEmpty(account), User::getAccount, account)
- .page(page.toPage());
- }
-
- }
- @RestController
- @RequestMapping("/user")
- @CrossOrigin(value = "*", maxAge = 3600)
- public class UserController {
-
- @Resource
- private UserService server;
-
- //用户注册
- @RequestMapping(value = "/register", method = RequestMethod.POST)
- public CommonResult<String> saveUser(@RequestBody User user) {
- //在数据库中将account字段设置为unique,不允许字段值重复
- try {
- server.save(user);
- return new CommonResult<>("创建成功");
- } catch (Exception e) {
- return new CommonResult<>("创建失败,账号已存在", "Bad Request",400);
- }
- }
-
-
- //查询用户列表(可以传入account精准查询或不传account批量查询)
- @RequestMapping(value = "/query", method = RequestMethod.GET)
- public CommonResult queryUserList(
- PageParam page,
- @RequestParam(required = false, value = "account") String account) {
- return new CommonResult(server.getUserList(page, account));
- }
-
- //删除用户
- @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
- public CommonResult removeUser(@RequestBody List<String> ids) {
- server.removeUser(ids);
- return new CommonResult("成功删除", "Delete Failed", 200);
- }
-
-
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。