springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目

结构如下:

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第1张图片

小案例地址:链接:https://pan.baidu.com/s/1PTZz4wYT9vY2QulRaV2SGg    提取码:syuy

一、创建父工程

1、File -- new -- project -- maven -- 不勾选模板 -- next

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第2张图片

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第3张图片

2.GroupId(一般填反转后公司域名)和ArtifactId(项目名)还有Version,这三个属性目的是标识你的项目的唯一性,点击next:

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第4张图片

3.创建好的新的工程如图所示:

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第5张图片

4、修改pom文件



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.7.RELEASE
    

    com.lin
    demo-parent
    pom
    1.0-SNAPSHOT

    
        UTF-8
        UTF-8
        1.8
    

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

    
    
        demo-service
        demo-control
    


二、创建子模块(service)

1.右击新创建的父工程

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第6张图片

2、为子项目添加模板,可根据实际情况添加,也可不添加

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第7张图片

3、填写子项目名称

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第8张图片

4、

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第9张图片

5、此处一定要注意填写正确的文件目录,否则会报错pom.xml已经存在,子模板创建失败

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第10张图片

6.点击FINISH,至此整个service子项目创建完成。

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第11张图片

7、然后创建上对应的java/resource目录

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第12张图片

8、修改pom文件




    
        demo-parent
        com.lin
        1.0-SNAPSHOT
    
    4.0.0

    demo-service
    demo-service
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7
        1.0.5
        2.1.8

    

    
        
            junit
            junit
            4.11
            test
        

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

        
        
        
            com.baomidou
            mybatisplus-spring-boot-starter
            ${mybatisplus-spring-boot-starter.version}
        
        
            com.baomidou
            mybatis-plus
            ${mybatisplus.version}
        
        

        
        
            com.alibaba
            druid
            1.0.28
        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.6
        

        
        
            mysql
            mysql-connector-java
            5.1.39
        
        
            org.projectlombok
            lombok
            1.16.16
        
    

    

    

9、entity

package com.lin.entity;

import com.baomidou.mybatisplus.annotations.TableField;
import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;

/**
 * @author :linyf
 * @date :Created in 2019/4/9 10:49
 * @description:
 */
@TableName("dept1")
public class Dept {

    @TableId("id")
    private String id;

    @TableField("name")
    private String name;

    @TableField("dbsource")
    private String dbsource;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getDbsource() {
        return dbsource;
    }

    public void setDbsource(String dbsource) {
        this.dbsource = dbsource;
    }

    @Override
    public String toString() {
        return "Dept{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", dbsource='" + dbsource + '\'' +
                '}';
    }
}

10、dao接口和mapper文件

package com.lin.dao;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.lin.entity.Dept;
import org.springframework.stereotype.Repository;


//------------------可以用@Repository,也可以用@Mapper
@Repository
//@Mapper
public interface DeptDao extends BaseMapper {
}




11、service层

package com.lin.service;

import com.baomidou.mybatisplus.service.IService;
import com.lin.entity.Dept;

public interface DeptService extends IService {
}
package com.lin.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.lin.dao.DeptDao;
import com.lin.entity.Dept;
import com.lin.service.DeptService;
import org.springframework.stereotype.Service;

/**
 * @author :linyf
 * @date :Created in 2019/4/10 17:12
 * @description:
 */
@Service
public class DeptServiceImpl extends ServiceImpl implements DeptService {
}

三、创建control子模块

1、创建方法同service

2、修改pom




    
        demo-parent
        com.lin
        1.0-SNAPSHOT
    
    4.0.0

    demo-control
    demo-control
    
    http://www.example.com

    
        UTF-8
        1.7
        1.7
    

    
        
            junit
            junit
            4.11
            test
        

        
            com.lin
            demo-service
            1.0-SNAPSHOT
        
        
            org.springframework.boot
            spring-boot-starter-web
        
    


        
            
                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        
                            
                                repackage
                            
                        
                    
                    
                        com.lin.Application
                    
                
                
                    org.apache.maven.plugins
                    maven-release-plugin
                    2.5.3
                
            
        


3、application.properties

server.port=8089
server.context-path=/demo

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc\:mysql\://127.0.0.1\:3306/test1
spring.datasource.username=root
spring.datasource.password=root
#druid
spring.datasource.initial-size=1
spring.datasource.max-active=20
spring.datasource.max-idle=30
spring.datasource.min-idle=5
spring.datasource.max-wait=60000
spring.datasource.time-between-eviction-runs-millis=60000
spring.datasource.min-evictable-idle-time-millis=300000
spring.datasource.validation-query=SELECT 'x'
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=false
spring.datasource.test-on-return=false
spring.datasource.pool-prepared-statements=false
spring.datasource.max-open-prepared-statements=20
spring.datasource.filters=stat

mybatis-plus.mapper-locations=classpath:/mapper/*Mapper.xml
mybatis-plus.type-aliases-package=com.lim.entity

4、入口类

package com.lin;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Hello world!
 *
 */


//---------------------如果dao接口用的是@Mapper注解,则不需写@MapperScan
//---------------------如果用的是@Repository注解,则必须写@MapperScan,去扫描dao

@SpringBootApplication
@MapperScan("com.lin.dao")
public class Application {
    public static void main( String[] args ) {
        SpringApplication.run(Application.class,args);
    }
}

5、controller

package com.lin.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.lin.entity.Dept;
import com.lin.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author :linyf
 * @date :Created in 2019/4/10 17:14
 * @description:
 */

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

    @Autowired
    private DeptService deptService;

    @RequestMapping("/query")
    @ResponseBody
    public Dept query(String id){
        Dept dept = deptService.selectById(id);
        return dept;
    }

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "hello world";
    }

    @RequestMapping("/insert")
    public boolean insert(){
        Dept dept = new Dept();
        dept.setId("4");
        dept.setName("采购部");
        dept.setDbsource("test1");
        boolean flag = deptService.insert(dept);
        System.out.println(flag);
        return flag;
    }

    @RequestMapping("/delete")
    public boolean delete(){
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id",1);
        boolean flag = deptService.delete(wrapper);
        System.out.println(flag);
        return flag;
    }

    @RequestMapping("/update")
    public boolean update(String id){
        Dept dept = new Dept();
        dept.setId(id);
        dept.setName("天蓝蓝");
        dept.setDbsource("lalalalal");
        boolean flag = deptService.updateById(dept);
        System.out.println(flag);
        return flag;
    }

    @RequestMapping("/queryByPage")
    public Page queryAll(){
        Page page = new Page<>(1,2);
        Page selectPage = deptService.selectPage(page);
        return selectPage;
    }
}

6、至此简单整合完成,浏览器访问

springboot + Mybatisplus + MySQL 简单整合 -- 多模块项目_第13张图片

四、mybatisplus

Mybatis-Plus(简称MP)是一个 Mybatis 的增强工具,在 Mybatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

官方网站:http://mp.baomidou.com

简单来说,Mybatis-PlusMybatis的增强工具包,其简化了CRUD操作,提供了代码生成器,强大的条件构造器,同时内置了多个实用插件:标配的分页插件、性能分析插件、全局拦截插件等。使得开发过程中,基本的范式代码都一句话解决了,省去了很多重复的操作。

 上面只是几个简单的方法使用,也可在service调用

@Service
public class DeptServiceImpl extends ServiceImpl implements DeptService {
    
    @Autowired
    private DeptDao deptDao;
    
    public Integer delete(String id){
        EntityWrapper wrapper = new EntityWrapper<>();
        wrapper.eq("id",id);
        Integer delete = baseMapper.delete(wrapper);
       //等价于    Integer delete1 = deptDao.delete(wrapper);
        return delete;
    }
}

 

你可能感兴趣的:(springboot)