目录结构
maven依赖
<?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.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.itwp</groupId>
<artifactId>hello-springboot</artifactId>
<version>1.0.0-SNAPSHOT</version>
<name>hello-springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>net.sourceforge.nekohtml</groupId>
<artifactId>nekohtml</artifactId>
</dependency>
<!-- 主要增加 HikariCP 依赖 -->
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>${hikaricp.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<!-- 排除 tomcat-jdbc 以使用 HikariCP -->
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!--国产-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
</project>
application.yml
spring:
application:
name: hello-springboot
thymeleaf:
cache: false
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/myshop?serverTimezone=UTC&&useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
hikari:
minimum-idle: 5
idle-timeout: 600000
maximum-pool-size: 10
auto-commit: true
pool-name: MyHikariCP
max-lifetime: 1800000
connection-timeout: 30000
connection-test-query: SELECT 1
mybatis:
type-aliases-package: com.itwp.springboot.entity
mapper-locations: classpath:mapper/*.xml
server:
port: 8088
servlet:
context-path: /boot
logging:
file: logs/imcoding.log
level.org.springframework.web: DEBUG
代码生成
HelloSpringbootApplication代码注意MapperScan
package com.itwp.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan(basePackages = "com.itwp.springboot.mapper")
public class HelloSpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(HelloSpringbootApplication.class, args);
}
}
HelloSpringbootApplicationTests测试代码
package com.itwp.springboot;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itwp.springboot.domain.TbUser;
import com.itwp.springboot.mapper.TbUserMapper;
import org.junit.Before;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.net.URL;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@Transactional
@Rollback
class HelloSpringbootApplicationTests {
@Autowired
private TbUserMapper tbUserMapper;
@Test
void contextLoads() {
System.out.println("wupengya");
}
/**
* 查询全部的用户信息
*/
@Test
public void testSelectAll(){
List<TbUser> tbUsers = tbUserMapper.selectAll();
tbUsers.forEach(tbUser -> {
System.out.println(tbUser);
});
}
/**
* 更新
*/
@Test
public void testUpdate() {
TbUser tbUser = tbUserMapper.selectByPrimaryKey(37L);
tbUser.setPhone("18277308888");
tbUserMapper.updateByPrimaryKey(tbUser);
}
/**
* 删除
*/
@Test
public void testDelete() {
tbUserMapper.deleteByPrimaryKey(37L);
}
/**
* 分页查询
*/
@Test
public void testPage1(){
Example example = new Example(TbUser.class);
example.createCriteria().andLike("username","z%");
PageHelper.startPage(1,5);
PageInfo<TbUser> pageInfo = new PageInfo<>(tbUserMapper.selectByExample(example));
System.out.println(pageInfo.getTotal());
System.out.println(pageInfo.getPages());
for (TbUser tbUser : pageInfo.getList()) {
System.out.println(tbUser);
}
}
/**
* 分页查询
*/
@Test
public void testPage2(){
PageHelper.startPage(1,5);
PageInfo<TbUser> pageInfo = new PageInfo<>(tbUserMapper.selectAll());
System.out.println(pageInfo);
System.out.println(pageInfo.getPages());
for (TbUser tbUser : pageInfo.getList()) {
System.out.println(tbUser);
}
}
}