整合mybatius

整合Mybatius

1.依赖项

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>

2.配置连接驱动 数据库 以及账户密码

1
2
3
4
5
6
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/infytime
username: root
password: root

3.测试demo

Dao层

1
2
3
4
5
6
7
8
9
10
11
12
package com.godbutton.imp;

import com.godbutton.entity.Book;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface BookDao {
@Select("select * from book where id=#{id}")
public Book findBook(Integer id);
}

Entity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.godbutton.entity;

public class Book {
private int id;
private String name;
private String author;
private String publish;
private Integer page;
private Float price;
private Integer bookCaseId;
private Integer abled;

@Override
public String toString() {
return id +":" +name;
}

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getPublish() {
return publish;
}

public void setPublish(String publish) {
this.publish = publish;
}

public Integer getPage() {
return page;
}

public void setPage(Integer page) {
this.page = page;
}

public Float getPrice() {
return price;
}

public void setPrice(Float price) {
this.price = price;
}

public Integer getBookCaseId() {
return bookCaseId;
}

public void setBookCaseId(Integer bookCaseId) {
this.bookCaseId = bookCaseId;
}

public Integer getAbled() {
return abled;
}

public void setAbled(Integer abled) {
this.abled = abled;
}
}

Control+Server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.godbutton.controller;
import com.godbutton.imp.BookDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/book/{id}")
public class BookController {
@Autowired
private BookDao bookDao;
@RequestMapping()
public Object getBook(@PathVariable("id") Integer id ) {
return bookDao.findBook(id);
}
}


整合mybatius
https://blog.dayday.cyou/2023/02/20/整合mybatius/
作者
godbutton
发布于
2023年2月20日
许可协议