springboot整合postgresql

依赖

        
            org.mybatis
            mybatis
            3.4.6
        
        
            org.postgresql
            postgresql
            runtime
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        
        
            org.mybatis.generator
            mybatis-generator-maven-plugin
            1.3.7
        

配置application.properties

spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
spring.datasource.username=postgres
spring.datasource.password=123456
spring.datasource.driver-class-name=org.postgresql.Driver

controller

package com.example.demo.controller;


import com.example.demo.entity.Dome;
import com.example.demo.page.PageRequest;
import com.example.demo.service.DomeService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @creator wangli66
 * @create-time 11:06 2019/12/3
 **/
@Api(value = "Dome工程")
@RestController
@RequestMapping("/dome")
public class DomeController {

    @Autowired
    private DomeService domeService;

    @ApiOperation(value = "testInterceptor")
    @RequestMapping("/testInterceptor")
    public String testInterceptor(String param) {
        return domeService.testInterceptor(param);
    }

    @ApiOperation(value = "findDome")
    @RequestMapping("/findDome")
    public String findDome(Dome dome) {
        return domeService.findById(dome).toString();
    }

    @ApiOperation(value = "findAll")
    @RequestMapping("/findAll")
    public List findAll() {
        return domeService.findAll();
    }

    @ApiOperation(value = "insert")
    @RequestMapping("/insert")
    public void insert(Dome dome) {
        domeService.insertDome(dome);
    }
    
    @ApiOperation(value = "findAllPage")
    @RequestMapping("/findAllPage")
    public PageInfo findAllPage(PageRequest pageRequest) {
        return domeService.findAllPage(pageRequest);
    }
}

service

package com.example.demo.service;

import com.example.demo.entity.Dome;
import com.example.demo.page.PageRequest;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @creator wangli66
 * @create-time 17:14 2019/12/26
 **/
@Service
public interface DomeService {

    String testInterceptor(String param);
    Dome findById(Dome dome);
    List findAll();
    void insertDome(Dome dome);

    PageInfo findAllPage(PageRequest pageRequest);
}

impl

package com.example.demo.service;

import com.example.demo.dao.DomeDaoMapper;
import com.example.demo.entity.Dome;
import com.example.demo.page.PageRequest;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @creator wangli66
 * @create-time 17:17 2019/12/26
 **/
@Component
@Transactional
public class DomeServiceImpl implements DomeService {
    @Autowired
    private DomeDaoMapper domeDaoMapper;

    @Override
    public String testInterceptor(String param) {
        return "测试拦截器:" + param;
    }

    @Override
    public Dome findById(Dome dome) {
        return domeDaoMapper.findById(dome);
    }

    @Override
    public List findAll() {
        return domeDaoMapper.findAll();
    }

    @Override
    public void insertDome(Dome dome) {
        domeDaoMapper.insertDome(dome);
    }

    /**
     * 分页查询,使用pageHelper隔离sql的变化
     *
     * @param pageRequest
     * @return
     */
    @Override
    public PageInfo findAllPage(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum() == null ? 1 : pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize() == null ? 10 : pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List domes = domeDaoMapper.findAll();
        return new PageInfo(domes);
    }

    // 测试物理分页,手动计算pagenum,pagesize
    public void findAllPage1() {
        int pageNum = 2;
        int pageSize = 2;
        List page1 = domeDaoMapper.findPage1(pageNum, pageSize);
        System.out.println(page1);
    }

    public void insertTest(int count) {
        // 插入十条数据
        for (int i = 0; i < count; i++) {
            Dome dome = new Dome();
            dome.setName("wangli9" + i);
            dome.setCompanyId("3" + i);
            dome.setCompanyName("qqq" + i);
            dome.setNickName("nike" + i);
            dome.setPassword("123456" + i);
            insertDome(dome);
        }
    }
}

mapper

package com.example.demo.dao;

import com.example.demo.entity.Dome;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @creator wangli66
 * @create-time 9:58 2019/12/27
 **/
public interface DomeDaoMapper {
    void insertDome(@Param("dome")Dome dome);
    Dome findById(@Param("dome")Dome dome);
    List findAll();
    List findPage1(@Param("pageNum")int pageNum, @Param("pageSize")int pageSize);
}

mapper.xml





	
		id,name,nick_name,company_name,company_id,password,status
	
    

    
	
		insert into t_user(name,nick_name,company_name,company_id,password,status)values (
		#{dome.name,jdbcType=VARCHAR},
		#{dome.nickName,jdbcType=VARCHAR},
		#{dome.companyName,jdbcType=VARCHAR},
		#{dome.companyId,jdbcType=VARCHAR},
		#{dome.password,jdbcType=VARCHAR},
		0
		)
	
	

entity

package com.example.demo.entity;

import lombok.Data;

/**
 * @creator wangli66
 * @create-time 9:59 2019/12/27
 **/
@Data
public class Dome {
    private Integer id;
    private String name;
    private String nickName;
    private String companyName;
    private String companyId;
    private String password;
    private Integer status;
}

包结构

springboot整合postgresql_第1张图片

你可能感兴趣的:(javaAPI)