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:项目结构图
文章来源地址https://www.toymoban.com/news/detail-593097.html
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;
}
}
文章来源:https://www.toymoban.com/news/detail-593097.html
到了这里,关于springboot项目创建整个完成过程和注意事项的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!