springboot+mybatisplus基础项目搭建

1、创建项目

File->New->Project->Maven->。。。->完成。

目录结构:
springboot+mybatisplus基础项目搭建_第1张图片

2、快速创建相关文件

DeptController.java

package com.strex.controller;

import com.strex.entity.DeptEntity;
import com.strex.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DeptController {

    @Autowired
    private DeptService deptService;

    @GetMapping("/insert")
    public void insert() {
        deptService.insert();
    }

    //    单条件查询
    //    http://localhost:8080/find?id=1
    @GetMapping("/findOne")
    public DeptEntity findOne(Integer id) {
        return deptService.selectById(id);
    }

    @GetMapping("/getList")
    public List<DeptEntity> getList(DeptEntity deptEntity) {
        return deptService.findList(deptEntity);
    }

    //    http://localhost:8080/insertDept?deptno=23&dname=%E5%A4%96%E4%BA%A4%E9%83%A8&db_source=DB02

    /***
     * 表中虽然主键字段是自动增长,但是mybatisplus不会自动增长,这里必须传入deptno
     * 如果要自动增长需要在实体类中添加注解 type设置自增
     *
     * @param deptEntity
     * @return
     */
    @GetMapping("/insertDept")
    public String insertDept(DeptEntity deptEntity) {
        return deptService.insertDept(deptEntity);
    }

    @GetMapping("/updateDept")
    public String updateDept(DeptEntity deptEntity) {
        return deptService.updateDept(deptEntity);
    }


}

DeptEntity.java

package com.strex.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;

//查询需要知道主键,指定表名
@Data
@TableName("dept")
public class DeptEntity {
    //TableId表示主键 type设置自增
    @TableId(value = "deptno",type = IdType.AUTO)
    private Integer deptno;
    private String dname;
    private String db_source;

}

DeptMapper.java

package com.strex.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.strex.entity.DeptEntity;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

//这里只需要继承BaseMapper就可以自动实现增删改查,不需要写sql
//Mapper注解为一个实体类

/**
 * 参考该博客:https://blog.csdn.net/qq_42381317/article/details/108283395   愿心无迫
 *
 * 在对应的功能模块添加对应的注解就不会报错,不然会报错,但是能正常运行
 * 配上以下注解自自动装配时@Autowired 才不会报找不到错误
 *
 * @Repository:代表dao层
 * @Component:这个惯用实体层
 * @Service : 这个用于service层
 * @Controller : 这个用于Controller
 * 这四个注解的功能都是一样的,都是代表某个类注册到Spring中,装配到Bean中
 */

@Mapper
@Repository
public interface DeptMapper extends BaseMapper<DeptEntity> {
    //单表增删改查不需要写sql
    //    @Insert("INSERT INTO DB01.dept (dname,db_source) VALUES ('[ˈteilə]','n. 裁缝师'), ('[ʌnˈkɔnʃəs]','adj. 不省人事的;失去知觉的')")
    //    void insertOne();

}

DeptService.java

package com.strex.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strex.entity.DeptEntity;
import com.strex.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

@Component
public class DeptService {

    @Autowired
    private DeptMapper deptMapper;

    public void insert() {
        System.out.println("调用service成功");
//        userMapper.insertOne();
    }


    public DeptEntity selectById(Integer id) {
        DeptEntity deptEntity = deptMapper.selectById(id);
        return deptEntity;
    }

    //    多条件查询
    public List<DeptEntity> findList(DeptEntity deptEntity) {
        QueryWrapper<DeptEntity> queryWrapper = new QueryWrapper<>();
        String dname = deptEntity.getDname();
        if (!StringUtils.isEmpty(dname)) {
//            上面的空判断可以不用,而是通过eq的第一个参数,condition boolen
            queryWrapper.eq("dname", dname);
//            queryWrapper.ge() 小于
//            queryWrapper.orderBy()
            //            queryWrapper.gt() 大于
//            queryWrapper.between() 介于。。。之间
        }

//        上面的判断另一种写法
        queryWrapper.eq(!StringUtils.isEmpty(deptEntity.getDb_source()), "db_source", deptEntity.getDb_source());
        return deptMapper.selectList(queryWrapper);

    }

    public String insertDept(DeptEntity deptEntity) {
        return deptMapper.insert(deptEntity) > 0 ? "success" : "fail";
    }

    public String updateDept(DeptEntity deptEntity) {
        return deptMapper.updateById(deptEntity) > 0 ? "success" : "fail";
    }

}

MyApplication.java

package com.strex;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class MyApplication {

    public static void main(String[] args) {
        //这里就只需要让工程运行起来就行了
        SpringApplication.run(MyApplication.class);
    }

}

pom.xml


<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.0modelVersion>
    <packaging>jarpackaging>

    <groupId>springbootdemogroupId>
    <artifactId>SpringbootdemoartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.8version>
    parent>

    <dependencies>
        <dependency>
            
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.3.2version>
        dependency>

        <dependency>
            
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.28version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>


    dependencies>


project>

application.yml

#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#  spring.datasource.url=jdbc:mysql://107.182.184.210:3306/DB01?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
#  spring.datasource.username='strex'
spring:
  datasource:
    password: 51**
    username: 'st**'
    url: jdbc:mysql://***IP***:3306/DB01?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
logging:
  level:
    ###打印mybatis日志,sql语句
    com.strex.mapper: debug

3、运行

springboot+mybatisplus基础项目搭建_第2张图片

springboot+mybatisplus基础项目搭建_第3张图片

4、打包

在pom文件中添加打包依赖(这里用打包成jar,由于springboot打包成jar继承了server,不需要额外的tomcat,只需要有java环境就可以运行)。


<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.0modelVersion>
    <packaging>jarpackaging>

    <groupId>springbootdemogroupId>
    <artifactId>SpringbootdemoartifactId>
    <version>1.0-SNAPSHOTversion>

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.6.8version>
    parent>

    <dependencies>
        <dependency>
            
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.5.2version>
        dependency>

        <dependency>
            
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>8.0.30version>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>


    dependencies>

    <build>
        
        <finalName>time-axisfinalName>

        <resources>
            
            
            <resource>
                <directory>src/main/javadirectory>
                <includes>
                    <include>**/*.xmlinclude>
                includes>
            resource>

            
            <resource>
                <directory>src/main/resourcesdirectory>
                <includes>
                    <include>**/*.*include>
                includes>
            resource>

        resources>
        <plugins>
            
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
                <version>2.1.7.RELEASEversion>
            plugin>
        plugins>
    build>
project>

(1)在Maven Lifecycle中双击package打包。

springboot+mybatisplus基础项目搭建_第4张图片

(2)打包成功后会在target文件夹下生成打包后的jar包。

springboot+mybatisplus基础项目搭建_第5张图片

(3)测试运行,在jar包右键就可以测试运行。

springboot+mybatisplus基础项目搭建_第6张图片

(3)也可以在jar包的文件夹右键执行 java -jar ***.jar运行。

springboot+mybatisplus基础项目搭建_第7张图片

(4)运行结果。

springboot+mybatisplus基础项目搭建_第8张图片

你可能感兴趣的:(springboot,spring,boot,java,intellij-idea)