SpringBoot学习:SpringBoot整合Mybatis实现数据库连接以及基本的增删改查操作并测试
程序员文章站
2022-03-02 22:23:14
...
代码目录
本文将按照pom.xml文件----resources包----java包的顺序来进行记录,并在关键部分加入解释,主要是起一个记录和总结的作用,防止自己写完的东西忘记。
1. Pom.xml
<?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>
<!--SpringBoot中关键的部分,parent。在parent中,提前会指定下面引入依赖的版本,
提前完成一些自动化配置,java的版本号等等-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!-- 自己的项目介绍 -->
<groupId>com.xxxx</groupId>
<artifactId>springboot01</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot01</name>
<description>Demo project for Spring Boot</description>
<!-- 自己设定的一些与parent不一样的包的版本 -->
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!-- 一个javaweb项目所需要的自动配置 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--一个springboot项目所需要的自动配置-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--mybatis分页插件,用来管理分页-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.13</version>
</dependency>
<!--一个mysql项目所需要的自动配置-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--c3p0数据库连接池-->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-freemarker</artifactId>-->
<!-- </dependency>-->
<!--thymeleaf页面模版,用来生成html-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--测试组件-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--lang3工具类,本项目中目前使用了他的StringUtils.isBlank函数来判断输入的用户名或密码是否为空-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. Resources
2.1 application.yml
##配置文件
#
#spring:
# profiles:
# active: dev #选择使用不同的配置文件(当前选择的是application-dev.yml)
# thymeleaf: #thymeleaf模版相关配置,文件类型为.html
# encoding: utf-8
# cache: false
# prefix: classpath:/html/
## freemarker: #freemarker模版相关配置,文件类型为.ftl
## charset: utf-8
## suffix: .ftl
## content-type: text/html
## template-loader-path: classpath:/views/ # freemarker文件存放路径,默认是放在templates文件夹里的
#logging:
# pattern:
# console: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger- %msg%n" #定制log不同的输出格式
# level: debug
# file:
# path: "."
# name: "springboot.log" # 日志输出的文件
server:
port: 9999
spring:
#数据库配置
datasource:
type: com.mchange.v2.c3p0.ComboPooledDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://192.168.1.157:3306/syt?UseUnicode=true&characterEncodeing=utf8&ServerTimezone=GMT%2B8
username: 略
password: 略
#mybatis配置
mybatis:
mapper-locations: classpath:/mappers/*.xml #mappers目录下的全部.xml文件
type-aliases-package: com.xxxx.springboot.vo #设置mybatis扫描的包,这样配置parameterType或是resultType的时候就可以不用写全类名了
configuration:
#下划线转驼峰配置
map-underscore-to-camel-case: true
pagehelper:
helper-dialect: mysql #设置分页数据库类型
logging:
level:
com:
xxxx:
springboot:
dao: debug #用来在测试阶段显示dao层的sql语句
2.2 views包和html包
二者都是用模版创建html页面,实现index页面功能,两者都是简单的页面,需结合java/Controller包中indexController代码一起看
2.2.1 views/index.ftl:
hello world!
2.2.2 html/index2.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1 th:text="${msg}"> </h1>
</body>
</html>
2.3 mappers
2.3.1 UserMapper.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对应的dao层的接口-->
<mapper namespace="com.xxxx.springboot.dao.UserDao">
<!--
以这个select方法为例: parameterType为输入参数类型 resultType为返回类型
这里拿到id,username,userpwd之后会自动封装到User对象里。
-->
<select id="queryUserByUserName" parameterType="string"
resultType="com.xxxx.springboot.vo.User">
select
id,user_name,user_pwd
from t_user
where user_name=#{userName}
</select>
<select id="queryById" parameterType="int" resultType="com.xxxx.springboot.vo.User">
select
id,user_name,user_pwd
from t_user
where id=#{id}
</select>
<insert id="save" parameterType="com.xxxx.springboot.vo.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user(id,user_name,user_pwd) values(#{id},#{userName},#{userPwd})
</insert>
<update id="update" parameterType="com.xxxx.springboot.vo.User">
update t_user set user_name =#{userName},user_pwd=#{userPwd} where id = #{id}
</update>
<select id="selectByParams" parameterType="com.xxxx.springboot.query.UserQuery" resultType="com.xxxx.springboot.vo.User">
select * from t_user
<where>
<if test="null != userName and userName !=''">
and user_name like concat('%',#{userName},'%')
</if>
</where>
</select>
<delete id="delete" parameterType="int">
delete from t_user where id =#{id}
</delete>
</mapper>