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

结构如下:

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

一、创建父工程

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

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

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

4、修改pom文件

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

    <groupId>com.lin</groupId>
    <artifactId>demo-parent</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!--子模块-->
    <modules>
        <module>demo-service</module>
        <module>demo-control</module>
    </modules>


</project>

二、创建子模块(service)

1.右击新创建的父工程

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

3、填写子项目名称

4、

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

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

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

8、修改pom文件

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-parent</artifactId>
        <groupId>com.lin</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-service</artifactId>
    <name>demo-service</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
        <mybatisplus-spring-boot-starter.version>1.0.5</mybatisplus-spring-boot-starter.version>
        <mybatisplus.version>2.1.8</mybatisplus.version>

    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mybatis-plus begin -->
        <!--mybatis-plus自动的维护了mybatis以及mybatis-spring的依赖,
             在springboot中这三者不能同时的出现,避免版本的冲突-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatisplus-spring-boot-starter</artifactId>
            <version>${mybatisplus-spring-boot-starter.version}</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus</artifactId>
            <version>${mybatisplus.version}</version>
        </dependency>
        <!-- mybatis-plus end -->

        <!-- 引入Druid依赖,阿里巴巴所提供的数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.28</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.6</version>
        </dependency>

        <!-- 提供mysql驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.39</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
        </dependency>
    </dependencies>

    <build>

    </build>
</project>

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<Dept> {
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lin.DeptDao">

</mapper>

11、service层

package com.lin.service;

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

public interface DeptService extends IService<Dept> {
}
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<DeptDao, Dept> implements DeptService {
}

三、创建control子模块

1、创建方法同service

2、修改pom

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>demo-parent</artifactId>
        <groupId>com.lin</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>demo-control</artifactId>
    <name>demo-control</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.lin</groupId>
            <artifactId>demo-service</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                    <configuration>
                        <mainClass>com.lin.Application</mainClass>
                    </configuration>
                </plugin>
            </plugins>
        </build>

</project>

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<Dept> 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<Dept> queryAll(){
        Page<Dept> page = new Page<>(1,2);
        Page<Dept> selectPage = deptService.selectPage(page);
        return selectPage;
    }
}

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

四、mybatisplus

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

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

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

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

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

猜你喜欢

转载自blog.csdn.net/weixin_43240792/article/details/89209577