springboot+mybatis-plus项目创建整个完成过程和注意事项

1:application.yml文件配置

server:
  port: 8088
  servlet:
    context-path: /test
spring:
  datasource:
    name: text  #????
    url: jdbc:mysql://localhost:3306/dsdd?serverTimezone=GMT&useUnicode=true&characterEncoding=utf-8&useSSL=true
    username: root  #??
    password: root  #??
    #要添加mysql-connector-java依赖
    driver-class-name: com.mysql.cj.jdbc.Driver  #????com.mysql.jdbc.Driver


mybatis-plus:
  #这个作用是扫描xml文件生效可以和mapper接口文件使用,
  #如果不加这个,就无法使用xml里面的sql语句
  #启动类加了@MapperScan是扫描指定包下mapper接口生效,如果不用@MapperScan可以在每一个mapper接口添加@Mapper注解
  mapper-locations: classpath*:com/example/poi/mapper/**/xml/*Mapper.xml
  global-config:
    # 关闭MP3.0自带的banner
    banner: false
    db-config:
      #主键类型
      id-type: ASSIGN_ID
      # 默认数据库表下划线命名
      table-underline: true
  configuration:
    # 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    # 返回类型为Map,显示null对应的字段
    call-setters-on-nulls: true

logging:
  level:
  	# 日记级别,设置的越小捕抓的信息越多 trace<debug<info<warn<error
    #root 键指定了根日志记录器的级别为 INFO
    root: INFO
    #com.baomidou.mybatisplus 包下的日志记录器级别为 DEBUG
    com.baomidou.mybatisplus: INFO
  file:
  	#保留了最近 30 天的日志
    max-history: 30
    #每个日记大小,当日记达到100M但还没到达下一天就生成一个新的文件按i序号
    max-size: 100MB
    #保存日记实际路径,确保指定的路径对应的目录已存在,并具有具有的写入权限
    pattern: /home/app/logs//mybatis-%d{yyyy-MM-dd}-%i.log
    #设置日志文件夹存储总大小的上限,超过就删除旧的
    total-size-cap: 1GB

2:pom.xml文件依赖管理,web服务必须按以下依赖

<?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 https://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>2.2.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>poi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>poi</name>
    <description>poi</description>
    <properties>
        <java.version>8</java.version>
    </properties>
    <dependencies>
    
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</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>
            </plugin>
        </plugins>

        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

    </build>

</project>

注意:1-使用web服务controller访问必须导入以下依赖

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

注意:2-使用mybatis-plus必须导入以下依赖(不要使用spring的mybatis,不然和spring本身数据库管理冲突,还有就是注意使用springboot版本和mybatis版本问题,建议都是使用mybatis-plus,不然使用mybatis可能出现sqlFactory找不到等问题)

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.5.1</version>
        </dependency>

注意:3-打开项目的target目录,观察里面是否有对应的××Mapper.xml文件,若没有,则在pom.xml文件中加入如下配置

	<build>
		<resources>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.yml</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
        </resources>

3:项目结构图

在这里插入图片描述

4:启动类

package com.example.poi;

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

@SpringBootApplication
@MapperScan(basePackages = "com.example.poi.mapper")
public class PoiApplication {
    public static void main(String[] args) {
        SpringApplication.run(PoiApplication.class, args);
    }
}

5:controller类

package com.example.poi.controller;

import com.example.poi.service.ITest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author xu
 * @create 2023/7/5 01
 */

@RestController
@RequestMapping("/customDemo")
public class DemoTwoController {

    @Resource
    ITest iTest;

    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) {

        String age = "20";
        String phone = iTest.getPhone(age);
    }
}

6:service和impl类

package com.example.poi.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.example.poi.entity.EntityDemo;

/**
 * @Author xu
 * @create 2023/7/14 22
 */
public interface ITest extends IService<EntityDemo> {

    String getPhone(String age);

}

package com.example.poi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.poi.entity.EntityDemo;
import com.example.poi.mapper.TestMapper;
import com.example.poi.service.ITest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author xu
 * @create 2023/7/14 22
 */
@Service
public class TestImpl extends ServiceImpl<TestMapper, EntityDemo> implements ITest {

    @Resource
    TestMapper testMapper;

    @Override
    public String getPhone(String age) {

        EntityDemo list = testMapper.selectName();

        this.baseMapper.selectOne(new LambdaQueryWrapper<EntityDemo>().eq(EntityDemo::getAge, age));


        return "entityDemo.getPhone()";
    }
}

7:mapper类和xml文件

package com.example.poi.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.poi.entity.EntityDemo;

/**
 * @Author xu
 * @create 2023/7/14 22
 */
//@Mapper
public interface TestMapper  extends BaseMapper<EntityDemo> {


    //@Select("select * from entity_demo where age='20'")
    EntityDemo selectName();
}

<?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.example.poi.mapper.TestMapper">

    <select id="selectName" resultType="com.example.poi.entity.EntityDemo">
        select * from entity_demo where age='20'
    </select>
</mapper>

8:注册mybati-plus分页拦截器(不注册这个,分页是失效)

//拦截器
@Configuration  //第一步:配置类  交给Spring管理  确保在启动类的包或子包下,才能被扫描到
public class NPCConfig {
//        第二步:做对应Bean
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
//        第三步:创建拦截器(这只是一个壳子)
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        第四步:添加内部拦截器  (分页的)
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
//        可以添加多个内部拦截器
        return interceptor;
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
创建一个spring boot + mybatis plus项目,你需要按照以下步骤进行操作: 1. 首先,确保你的pom.xml文件中添加了必要的依赖。你需要添加mybatis plus的依赖,可以在pom.xml文件中添加以下代码段: ``` <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.4.1</version> <optional>true</optional> </dependency> ``` 同时,你还需要添加spring-jdbc、mybatis-plus-generator和freemarker的依赖。可以在pom.xml文件中添加以下代码段: ``` <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.2.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.30</version> </dependency> ``` 2. 接下来,你需要创建一个Spring Boot项目。你可以使用你喜欢的IDE,如IntelliJ IDEA或Eclipse,创建一个新的Spring Boot项目。 3. 在项目的配置文件(application.properties或application.yml)中,配置数据库连接信息。你需要提供数据库的URL、用户名和密码等信息。 4. 创建实体类和Mapper接口。你可以使用mybatis plus的代码生成器来自动生成实体类和Mapper接口。你可以在项目创建一个代码生成器的类,使用mybatis plus提供的相关API来生成代码。 5. 编写业务逻辑代码。根据你的需求,编写相应的Service和Controller类来处理业务逻辑。 6. 运行项目。你可以使用IDE中提供的运行功能,或者使用命令行工具来运行项目。 通过以上步骤,你就可以成功创建一个spring boot + mybatis plus项目了。记得根据你的具体需求进行相应的配置和编码。祝你成功! #### 引用[.reference_title] - *1* *3* [搭建spring boot + mybatis plus项目](https://blog.csdn.net/weixin_41968788/article/details/106542661)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [新建一个Spring Boot+MyBatis-Plus项目](https://blog.csdn.net/hyh17808770899/article/details/122076377)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值