Skip to content

Commit 18266cd

Browse files
committed
集成 MyBaits 向数据库插入数据
1 parent bc6c12e commit 18266cd

File tree

6 files changed

+35
-11
lines changed

6 files changed

+35
-11
lines changed

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@
4040
<artifactId>mybatis</artifactId>
4141
<version>3.5.4</version>
4242
</dependency>
43+
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
44+
<dependency>
45+
<groupId>org.mybatis.spring.boot</groupId>
46+
<artifactId>mybatis-spring-boot-starter</artifactId>
47+
<version>2.0.0</version>
48+
</dependency>
4349

4450
<dependency>
4551
<groupId>org.springframework.boot</groupId>

src/main/java/life/majiang/community/controller/AuthorizeController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,14 @@ public String callback(@RequestParam(name = "code") String code,
5454
// 登录跳转
5555
if (githubUser != null) {
5656
User user = new User();
57-
user.setToken(UUID.randomUUID().toString());
57+
user.setToken(UUID.randomUUID().toString()); // 生成唯一的标识码
5858
user.setName(githubUser.getName());
5959
user.setAccountID(String.valueOf(githubUser.getId()));
6060
user.setGmtCreate(System.currentTimeMillis());
6161
user.setGmtModified(user.getGmtCreate());
62-
userMapper.insert(new User());
62+
System.out.println("--->>> usermapper >>>--- " + userMapper);
63+
System.out.println("--->>> insertuser >>>--- " + user);
64+
userMapper.insert(user);
6365

6466
// 登录成功 >>> 写 cookies 和 session
6567
request.getSession().setAttribute("user", githubUser); // session存入 user对象

src/main/java/life/majiang/community/dto/AccessTokenDTO.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package life.majiang.community.dto;
22

3+
/*
4+
* 第三方应用注册信息
5+
*/
36
public class AccessTokenDTO {
4-
private String client_id;
5-
private String client_secret;
6-
private String code;
7-
private String redirect_uri;
8-
private String state;
7+
private String client_id; // 应用标识id
8+
private String client_secret; // 应用密钥
9+
private String code; // 响应状态码
10+
private String redirect_uri; // 连接地址
11+
private String state; // 连接状态
12+
913
// 自动创建get, set 方法 快捷键Alt+Instert
1014
public String getClient_id() {
1115
return client_id;

src/main/java/life/majiang/community/dto/GithubUser.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* 获取Github 用户信息
55
*/
66
public class GithubUser {
7-
private String name;
8-
private Long id;
9-
private String bio;
7+
private String name; // 用户昵称
8+
private Long id; // 用户id
9+
private String bio; // 个人签名
1010

1111
public String getName() {
1212
return name;

src/main/java/life/majiang/community/mapper/UserMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@
99
*/
1010
@Mapper
1111
public interface UserMapper {
12-
@Insert("insert into user (name, account_ID, token, gmt_Create, gmt_Modified) value (#{name}, #{accountID}, #{token}, #{gmtCreate}, #{gmtModified})")
12+
@Insert("insert into user (name, account_ID, token, gmt_Create, gmt_Modified) values (#{name}, #{accountID}, #{token}, #{gmtCreate}, #{gmtModified})")
1313
void insert(User user);
1414
}

src/main/java/life/majiang/community/model/User.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
package life.majiang.community.model;
22

3+
// 数据库表格式
4+
/*create table USER
5+
(
6+
ID INT auto_increment,
7+
ACCOUNT_ID VARCHAR,
8+
NAME VARCHAR(100),
9+
TOKEN CHAR(36),
10+
GMT_CREATE BIGINT,
11+
GMT_MODIFIED BIGINT,
12+
constraint TABLE_NAME_PK
13+
primary key (ID)
14+
);*/
315
public class User {
416
private Integer id; // 用户自增id
517
private String name; // username

0 commit comments

Comments
 (0)