当导入依赖中有重复的依赖可以使用exclusion来去除选择的依赖
<dependencies>
<!--SpringBoot框架web项目起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--SpringBoot框架测试起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
yaml配置文件(端口号,项目路径)
server:
port: 8082
servlet:
context-path: /springboot04
取yaml中的数组元素
school:
list:
- name: runoob
websit: https://www.runoob.com/
- name: abc
websit: http://www.abc.com
@Component
public class School {
private String name ;
private String websit ;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getWebsit() {
return websit;
}
public void setWebsit(String websit) {
this.websit = websit;
}
}
@Component
@ConfigurationProperties(prefix = "school")
public class MySchool {
private List<School> list;
public List<School> getList() {
return list;
}
public void setList(List<School> list) {
this.list = list;
}
}
SpringBoot中使用thymeleaf
thymeleaf:
prefix: classpath:/templates/ #前缀
suffix: .html #后缀
cache: false #是否缓存(开发阶段建议关闭,线上开启)
encoding: UTF-8 #编码
mode: HTML5
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model) {
System.out.println("hello");
model.addAttribute("username" , "xiaoming");
return "index";
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>我是一个index.html网页</h1>
<div th:text="${username}"></div>
</body>
</html>
SpringBoot中使用Mybatis
<!--MySQL驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<version>8.0.28</version>-->
</dependency>
<!--MyBatis整合SpringBoot框架的起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.0</version>
<!--<exclusions>-->
<!-- <exclusion>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter</artifactId>-->
<!-- </exclusion>-->
<!--</exclusions>-->
</dependency>
若Mapper文件为让在resources中,需手动指定文件夹(在pom.xml的build中配置)
<!--手动指定文件夹为resources-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
若放在resources的mapper文件夹中,则在yaml配置中加
mybatis:
mapper-locations: classpath:mapper/*.xml
起别名(com.pei.springboot.model.Student简写成Student)
<resultMap id="BaseResultMap" type="com.pei.springboot.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
# 起别名
mybatis.type-aliases-package=com.pei.springboot.model
SpringBoot+Thymeleaf+Mybatis小练习
创建springboot_mybatis项目,选择Java17开发编译环境
选择依赖 Spring Web / MySQL Driver / Mybatis Framework / Thymeleaf
检查pom.xml中依赖版本和加入分页插件所需的依赖
编写application.yml配置文件
server:
port: 8081
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.springboot_mybatis.pojo
thymeleaf:
prefix: classpath:/templates/ #前缀
suffix: .html #后缀
cache: false #是否缓存(开发阶段建议关闭,线上开启)
encoding: UTF-8 #编码
mode: HTML5
# PageHelper 插件配置
pagehelper:
helperDialect: mysql # 设置数据库方言为 MySQL
reasonable: true # 启用合理化查询,根据查询数据量自动进行分页
supportMethodsArguments: true # 支持通过 Mapper 方法参数进行分页
params: count=countSql # 指定分页插件的特殊行为参数,这里是指定 countSql
page: # 分页配置
size: 10 # 每页显示的数据条数
offset: 0 # 数据偏移量,默认为0
#日志
logging:
level:
org.mybatis.spring.SqlSessionTemplate: debug
com.example.springboot_mybatis.mapper: debug
在springboot_mybatis文件夹下创建controller / mapper / pojo / service等文件夹
创建pojo类
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private Integer age;
}
创建StudentMapper接口
public interface StudentMapper {
int save(Student student);
int update(Student student);
int delete(Integer id);
Student findById(Integer id);
List<Student> findAll();
}
创建StudentMapper.xml文件
<?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.springboot_mybatis.mapper.StudentMapper">
<insert id="save">
insert into t_student(id,name,age) values(#{id},#{name},#{age})
</insert>
<update id="update">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>
<delete id="delete">
delete from t_student where id=#{id}
</delete>
<select id="findById" resultType="com.example.springboot_mybatis.pojo.Student">
select * from t_student where id=#{id}
</select>
<select id="findAll" resultType="com.example.springboot_mybatis.pojo.Student">
select * from t_student
</select>
</mapper>
创建StudentService接口
public interface StudentService {
public void save(Student student);
public void update(Student student);
public void delete(Integer id);
public Student findById(Integer id);
public List<Student> findAll();
public PageInfo<Student> page(Integer pageNum, Integer pageSize);
}
创建StudentServiceImpl实现类
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Override
public void save(Student student) {
studentMapper.save(student);
}
@Override
public void update(Student student) {
studentMapper.update(student);
}
@Override
public void delete(Integer id) {
studentMapper.delete(id);
}
@Override
public Student findById(Integer id) {
return studentMapper.findById(id);
}
@Override
public List<Student> findAll() {
return studentMapper.findAll();
}
@Override
public PageInfo<Student> page(Integer pageNum, Integer pageSize) {
PageHelper.startPage(pageNum,pageSize);
List<Student> studentList = studentMapper.findAll();
PageInfo<Student> studentPageInfo = new PageInfo<>(studentList);
return studentPageInfo;
}
}
创建StudentController类
@Controller
@RequestMapping("/student")
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/findAll")
public String findAll(Model model) {
List<Student> studentList = studentService.findAll();
model.addAttribute("studentList", studentList);
return "index";
}
}
在templates文件夹中创建index.html页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<base th:href="@{/}">
</head>
<body>
<table>
<tr th:each="student:${studentList}">
<td th:text="${student.id}"></td>
<td th:text="${student.name}"></td>
<td th:text="${student.age}"></td>
</tr>
</table>
</body>
</html>
测试findAll方法
使用测试类测试分页方法,编写SpringbootMybatisApplicationTests类
@SpringBootTest
class SpringbootMybatisApplicationTests {
@Autowired
private StudentService studentService;
@Test
public void pageTest(){
PageInfo<Student> studentPageInfo = studentService.page(1, 5);
List<Student> list = studentPageInfo.getList();
for (Student student : list) {
System.out.println(student);
}
}
}