springboot+postgresql+mybatisplus

1.引入jar包

        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            org.springframework.boot
            spring-boot-starter-web
            
                
                    log4j-to-slf4j
                    org.apache.logging.log4j
                
            
        
        
            org.postgresql
            postgresql
            runtime
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.3.2
        
        
            com.baomidou
            mybatis-plus-generator
            3.3.2
        
        
            org.projectlombok
            lombok
            true
        

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 {

    /**
     * 配置乐观锁插件
     * @return
     */
    @Bean
    public OptimisticLockerInterceptor optimisticLockerInterceptor(){
        return new OptimisticLockerInterceptor();
    }

    /**
     * 分页插件
     * @return
     */
    @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 {
    /**
     * 主键ID
     */
    @TableId
    private Integer id;
    /**
     * 名称
     */
    private String name;
    /**
     * 描述
     */
    private String dictDesc;
    /**
     * 父ID
     */
    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.测试结果

springboot+postgresql+mybatisplus_第1张图片

 

 

你可能感兴趣的:(springboot,springboot)