1.引入jar包
org.springframework.boot spring-boot-starterorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-starter-weblog4j-to-slf4j org.apache.logging.log4j org.postgresql postgresqlruntime com.baomidou mybatis-plus-boot-starter3.3.2 com.baomidou mybatis-plus-generator3.3.2 org.projectlombok lomboktrue
2.添加配置文件application.properties
server.port=8080 spring.datasource.driver-class-name=org.postgresql.Driver spring.datasource.username=postgres spring.datasource.password=root spring.datasource.url=jdbc:postgresql://localhost:5432/test mybatis-plus.mapper-locations=com.example.demo.mapper.* mybatis-plus.configuration.auto-mapping-behavior=full mybatis-plus.configuration.call-setters-on-nulls=true mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl mybatis-plus.configuration.map-underscore-to-camel-case=true
3. 添加mybatisplus的插件配置
@EnableTransactionManagement
@Configuration
public class MybatisPlusConfig {
@Bean
public OptimisticLockerInterceptor optimisticLockerInterceptor(){
return new OptimisticLockerInterceptor();
}
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
4.添加entity层实体类
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("t_dict")
public class DictEntity {
@TableId
private Integer id;
private String name;
private String dictDesc;
private Integer parentId;
private String dictType;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date createAt;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
private Date updateAt;
}
5.添加dao层mapper
package com.example.demo.dao; import com.baomidou.mybatisplus.core.mapper.baseMapper; import com.example.demo.entity.DictEntity; import org.apache.ibatis.annotations.Mapper; @Mapper public interface DictMapper extends baseMapper{ }
6.添加service业务层代码
package com.example.demo.service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.example.demo.dao.DictMapper; import com.example.demo.entity.DictEntity; import org.springframework.stereotype.Service; import java.util.List; @Service public class DictService extends ServiceImpl{ public List findAll(){ List list = this.list(); return list; } }
7. controller层代码(这里仅写查询,增删改类似这种方式)
package com.example.demo.controller;
import com.baomidou.mybatisplus.extension.api.R;
import com.example.demo.entity.DictEntity;
import com.example.demo.service.DictService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;
@RestController
@RequestMapping("/api/dict")
public class DictController {
@Resource
private DictService dictService;
@PostMapping("findAll")
public R> findAll() {
List all = dictService.findAll();
return R.ok(all);
}
}
8.测试结果



