Skip to content

Commit c99403c

Browse files
author
曾梓健
committed
整合mybatis
1 parent b61b8be commit c99403c

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

springboot-dataaccess/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<artifactId>druid-spring-boot-starter</artifactId>
4242
<version>1.1.0</version>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.mybatis.spring.boot</groupId>
46+
<artifactId>mybatis-spring-boot-starter</artifactId>
47+
<version>1.3.2</version>
48+
</dependency>
4449
</dependencies>
4550

4651
<build>

springboot-dataaccess/src/main/java/com/hand/springbootdataaccess/SpringbootDataaccessApplication.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.hand.springbootdataaccess;
22

3+
import org.mybatis.spring.annotation.MapperScan;
34
import org.springframework.boot.SpringApplication;
45
import org.springframework.boot.autoconfigure.SpringBootApplication;
56

67
@SpringBootApplication
8+
@MapperScan(value = "com.hand.springbootdataaccess.mapper")
79
public class SpringbootDataaccessApplication {
810

911
public static void main(String[] args) {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hand.springbootdataaccess.mapper;
2+
3+
import java.util.List;
4+
5+
import com.hand.springbootdataaccess.pojo.Report;
6+
import org.apache.ibatis.annotations.Select;
7+
8+
/**
9+
* @author zijian.zeng@hand-china.com
10+
* @since 2023-01-29
11+
*/
12+
public interface ReportMapper {
13+
14+
@Select("select * from report;")
15+
List<Report> findAllReport();
16+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.hand.springbootdataaccess.pojo;
2+
3+
/**
4+
* @author zijian.zeng@hand-china.com
5+
* @since 2023-01-29
6+
*/
7+
public class Report {
8+
9+
private Long id;
10+
11+
private String content;
12+
13+
private int type;
14+
15+
private int status;
16+
17+
public int getStatus() {
18+
return status;
19+
}
20+
21+
public void setStatus(int status) {
22+
this.status = status;
23+
}
24+
25+
public Long getId() {
26+
return id;
27+
}
28+
29+
public void setId(Long id) {
30+
this.id = id;
31+
}
32+
33+
public String getContent() {
34+
return content;
35+
}
36+
37+
public void setContent(String content) {
38+
this.content = content;
39+
}
40+
41+
public int getType() {
42+
return type;
43+
}
44+
45+
public void setType(int type) {
46+
this.type = type;
47+
}
48+
49+
@Override
50+
public String toString() {
51+
return "Report{" +
52+
"id=" + id +
53+
", content='" + content + '\'' +
54+
", type=" + type +
55+
", status=" + status +
56+
'}';
57+
}
58+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.hand.springbootdataaccess.service;
2+
3+
import java.util.List;
4+
5+
import com.hand.springbootdataaccess.mapper.ReportMapper;
6+
import com.hand.springbootdataaccess.pojo.Report;
7+
import org.apache.commons.logging.Log;
8+
import org.apache.commons.logging.LogFactory;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.stereotype.Service;
11+
12+
/**
13+
* @author zijian.zeng@hand-china.com
14+
* @since 2023-01-29
15+
*/
16+
@Service
17+
public class ReportService {
18+
19+
Log log = LogFactory.getLog(ReportService.class);
20+
21+
22+
@Autowired
23+
private ReportMapper reportMapper;
24+
25+
26+
public List<Report> findAllReport() {
27+
28+
List<Report> allReport = reportMapper.findAllReport();
29+
log.info("查询到的信息: " + allReport);
30+
return allReport;
31+
}
32+
33+
34+
}

springboot-dataaccess/src/test/java/com/hand/springbootdataaccess/SpringbootDataaccessApplicationTests.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,12 @@
44
import java.sql.ResultSet;
55
import java.sql.SQLException;
66
import java.sql.Statement;
7+
import java.util.List;
78
import javax.sql.DataSource;
89

10+
import com.hand.springbootdataaccess.pojo.Report;
911
import com.hand.springbootdataaccess.pojo.SimpleBean;
12+
import com.hand.springbootdataaccess.service.ReportService;
1013
import org.junit.jupiter.api.Test;
1114
import org.junit.runner.RunWith;
1215
import org.springframework.beans.factory.annotation.Autowired;
@@ -23,6 +26,9 @@ class SpringbootDataaccessApplicationTests {
2326
@Autowired
2427
private SimpleBean simpleBean;
2528

29+
@Autowired
30+
private ReportService reportService;
31+
2632
@Test
2733
void contextLoads() throws SQLException {
2834
Connection connection = dataSource.getConnection();
@@ -35,4 +41,9 @@ void contextLoads() throws SQLException {
3541

3642
}
3743

44+
@Test
45+
void findReport() {
46+
reportService.findAllReport();
47+
}
48+
3849
}

0 commit comments

Comments
 (0)