Spring Boot 整合MyBatis、Druid连接池、PageHelper插件

Spring Boot 整合MyBatis、Druid连接池、PageHelper插件,第1张

Spring Boot 整合MyBatis、Druid连接池、PageHelper插件 SpringBoot整合MyBatis
1 导入依赖

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.projectlombok
            lombok
            true
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            2.2.0
        

        
            mysql
            mysql-connector-java
        

        
            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.4.0
        
        
            com.github.pagehelper
            pagehelper-spring-boot-autoconfigure
            1.4.0
        

        
            com.alibaba
            druid-spring-boot-starter
            1.2.6
        


        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                1.5.4.RELEASE
            
            
                org.apache.maven.plugins
                maven-resources-plugin
                2.4
            
        
    

2 在application.properties或者application.yml中配置数据库信息 我这里是在application.yml中配置的
#配置数据库信息 数据源为DruidDataSource
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql:///emp_db?serverTimezone=Asia/Shanghai&characterEconding=utf8
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource
#mybatis扫描
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:mapper/*.xml
#端口号的更改
server:
  port: 8085
#pagehelper插件的配置
pagehelper:
  helper-dialect: mysql
  reasonable: true
  support-methods-arguments: true

3 根据数据库创建实体类 数据库中两张表为这样的

 

 

 

 

import lombok.Data;
import java.io.Serializable;
import java.util.Date;

@Data
public class Emp implements Serializable {

    private Integer empno;

    private String ename;

    private String job;

    private Integer deptno;

    private Date hireday;
}
import lombok.Data;
import java.io.Serializable;

@Data
public class DepartMent implements Serializable {

    private Integer deptno;

    private String dname;

    private String loc;
}
 因为需要有连表查询,所以我多建了一个工具类
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.util.Date;

@Data
public class Emp_Department implements Serializable {

    private Integer empno;

    private String ename;

    private String job;

    private Integer deptno;

    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date hireday;

    private String dname;

    private String loc;
}
 
4 创建mapper 
EmpMapper 
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface EmpMapper {

    List findAll();

    void deleteAll(Integer[] empnos);

    void updateById(Emp emp);

    void deleteById(Integer empno);

    void add(Emp emp);

    Emp_Department findById(Integer empno);
}
​DeptMapper
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface DeptMapper {
    DepartMent findById(Integer deptno);

    List findAll();
}

5 在srcmainresourcesmapper路径下创建对应的Mapper.xml文件 EmpMapper.xml





    
        
        
        
        
        
        
        
    

    
        empno,ename,job,deptno,hireday,dname,loc
    

    
        select empno,ename,job,a.deptno,hireday,dname,loc
        from emp_tb a
        inner join department_tb b
        on a.deptno = b.deptno
        where empno = #{empno}
    

    
        delete from emp_tb
        where empno in
        
            #{empno}
        
    

    
        delete from emp_tb
        where empno = #{empno}
    

    
        insert into emp_tb(ename,job,deptno,hireday)
        values (#{ename},#{job},#{deptno},#{hireday})
    

    
        update emp_tb
        set ename = #{ename},job = #{job},deptno = #{deptno},hireday = #{hireday}
        where empno = #{empno}
    

DeptMapper.xml





    
        
        
        
    

    
        deptno,dname,loc
    

    
        select 
        from department_tb
        where deptno = #{deptno}
    

6.创建service EmpService
import java.util.List;

public interface EmpService {
    List findAll();

    void add(Emp emp);

    void deleteAll(Integer[] empnos);

    void deleteById(Integer empno);

    void updateById(Emp emp);

    Emp_Department findById(Integer empno);
}
import java.util.List;

public interface DeptService {
    DepartMent findById(Integer deptno);

    List findAll();
}
7 创建serviceImpl
mport org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private EmpMapper empMapper;

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

    @Override
    public void add(Emp emp) {
        empMapper.add(emp);
    }

    @Override
    public void deleteAll(Integer[] empnos) {
        empMapper.deleteAll(empnos);
    }

    @Override
    public void deleteById(Integer empno) {
        empMapper.deleteById(empno);
    }

    @Override
    public void updateById(Emp emp) {
        empMapper.updateById(emp);
    }

    @Override
    public Emp_Department findById(Integer empno) {
        Emp_Department emp_department = empMapper.findById(empno);
        return emp_department;
    }


}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public DepartMent findById(Integer deptno) {
        DepartMent departMent = deptMapper.findById(deptno);
        return departMent;
    }

    @Override
    public List findAll() {
      List list = deptMapper.findAll();
      return list;
    }
}
 
 8 创建controller 
EmpController 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("emp")
public class EmpController {

    @Autowired
    private EmpService empService;


    @RequestMapping("findAll")
    public List findAll(){
        return empService.findAll();
    }

    @RequestMapping("findByPage")
    public PageInfo findByPage(@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                               @RequestParam(value = "pageSize",required = false,defaultValue = "3") Integer pageSize){
        PageHelper.startPage(pageNum,pageSize);

        List list = empService.findAll();

        PageInfo listPageInfo = new PageInfo<>(list);

        return listPageInfo;
    }

    @RequestMapping("findById")
    public Emp_Department findById(@RequestParam(value = "empno") Integer empno){
        Emp_Department emp_department = empService.findById(empno);
        return emp_department;
    }

    @RequestMapping("addEmp")
    public String add(@RequestBody  Emp emp){
        empService.add(emp);
        return "Success";
    }

    @RequestMapping("deleteById")
    public String deleteById(Integer empno){
        empService.deleteById(empno);
        return "Success";
    }

    @RequestMapping("deleteAll")
    public String deleteAll(Integer[] empnos){
        empService.deleteAll(empnos);
        return "Success";
    }

    @RequestMapping("update")
    public String updateById(@RequestBody Emp emp){
        empService.updateById(emp);
        return "Success";
    }

}
DeptController
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @RequestMapping("findAll")
    public List findAll(){
        List list = deptService.findAll();
        return list;
    }

    @RequestMapping("findById")
    public DepartMent findById(@RequestParam("deptno") Integer deptno){
        DepartMent departMent = deptService.findById(deptno);
        return departMent;
    }

}
9启动配置类,进行测试(注意:需要在启动类上添加@MapperScan扫描Mapper)
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@MapperScan("com.qf.mapper")
public class ManagerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ManagerApplication.class, args);
    }

}

启动工程,进行测试

查找全部

 分页查找

 增删改的就不进行测试了,伙伴们可以自行测试

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5574002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-14
下一篇 2022-12-14

发表评论

登录后才能评论

评论列表(0条)

保存