文章目录
配置文件配置
本文实现的是一个简单的自定义登录页,所以只需要配置Mysql即可
Maven配置
因为需要使用到Mysql和MyBatis所以需要添加相关的依赖
<dependencies>
<!-- 添加Spring Security依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!--添加mybatis依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
<!--添加Mysql连接依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
</dependencies>
Mysql相关的配置
在application.properties中配置Mysql()
// An highlighted block
spring.datasource.url=jdbc:mysql://localhost:3306/blogrepository?useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
通过上面的配置即可配置好Mysql,需要注意的时,新版本的Mysql在连接时需要指定UTC时区,通过serverTimeZone=GMT来指定。否则会出现下面的异常报错:
Java.sql.SQLException: The server time zone value ‘…???’ is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
数据接口的实现
数据表
id | username | passwd |
---|---|---|
1 | root | root |
数据表对应的实体类
@Component
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
private String username;
private String passwd;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
数据访问类
通过Mybatis的注解来实现数据库的访问
@Mapper
@Repository
public interface UserMapper {
@Select("select id,username,passwd from user"