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
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
mapper-locations: classpath*:com/example/poi/mapper/**/xml/*Mapper.xml
global-config:
banner: false
db-config:
id-type: ASSIGN_ID
table-underline: true
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
call-setters-on-nulls: true
logging:
level:
root: INFO
com.baomidou.mybatisplus: INFO
file:
max-history: 30
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/>
</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;
@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;
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;
@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;
public interface TestMapper extends BaseMapper<EntityDemo> {
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
public class NPCConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}