Skip to content

Commit 4a74258

Browse files
committed
feat(整合Mybatis):在eureka-customer服务中整合Mybatis并添加简单的测试
1 parent 556fbd2 commit 4a74258

File tree

13 files changed

+192
-16
lines changed

13 files changed

+192
-16
lines changed

eureka-customer/pom.xml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,20 @@
6868
<artifactId>spring-cloud-starter-config</artifactId>
6969
</dependency>
7070

71+
<!-- 引入Mybatis依赖 -->
72+
<dependency>
73+
<groupId>org.mybatis.spring.boot</groupId>
74+
<artifactId>mybatis-spring-boot-starter</artifactId>
75+
<version>1.3.2</version>
76+
</dependency>
77+
78+
<!-- 引入Mybatis-Plus依赖 -->
79+
<!--<dependency>-->
80+
<!--<groupId>com.baomidou</groupId>-->
81+
<!--<artifactId>mybatis-plus-boot-starter</artifactId>-->
82+
<!--<version>3.0.4</version>-->
83+
<!--</dependency>-->
84+
7185
<!-- 引入MySQL连接依赖 -->
7286
<dependency>
7387
<groupId>mysql</groupId>
@@ -86,6 +100,14 @@
86100
<version>2.7.0</version>
87101
</dependency>
88102

103+
<!-- 引入lombok依赖 -->
104+
<dependency>
105+
<groupId>org.projectlombok</groupId>
106+
<artifactId>lombok</artifactId>
107+
<version>1.18.0</version>
108+
<scope>provided</scope>
109+
</dependency>
110+
89111
<dependency>
90112
<groupId>io.springfox</groupId>
91113
<artifactId>springfox-swagger-ui</artifactId>

eureka-customer/src/main/java/com/coderqian/eurekacustomer/common/BaseResult.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.coderqian.eurekacustomer.common;
22

3+
import com.coderqian.eurekacustomer.common.constant.Code;
4+
35
/**
46
* @author qianliqing
57
* @date 2018-09-28 下午9:05
@@ -25,6 +27,17 @@ public BaseResult(String msg) {
2527
this.message = msg;
2628
}
2729

30+
public BaseResult(Code code) {
31+
this.code = code.getCode();
32+
this.message = code.getMsg();
33+
}
34+
35+
public BaseResult(Code code, T data) {
36+
this.code = code.getCode();
37+
this.message = code.getMsg();
38+
this.data = data;
39+
}
40+
2841
public BaseResult(int code, String message) {
2942
this.code = code;
3043
this.message = message;
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.coderqian.eurekacustomer.common;
2+
3+
import com.coderqian.eurekacustomer.common.constant.Code;
4+
5+
/**
6+
* @author qianliqing
7+
* @date 2018-10-02 下午2:52
8+
9+
*/
10+
11+
@SuppressWarnings("unchecked")
12+
public class BaseResultFactory {
13+
14+
/**
15+
* 构建成功并返回的数据
16+
*
17+
* @param data 返回数据
18+
* @return
19+
*/
20+
public static BaseResult createSuccessResult(Object data) {
21+
return new BaseResult(Code.SUCCESS, data);
22+
}
23+
24+
/**
25+
* 成功但不返回的数据
26+
*
27+
* @return
28+
*/
29+
public static BaseResult createEmptyResult() {
30+
return new BaseResult(Code.SUCCESS);
31+
}
32+
}

eureka-customer/src/main/java/com/coderqian/eurekacustomer/common/exception/BaseException.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,27 @@
1212

1313
public abstract class BaseException extends RuntimeException {
1414

15-
private int codeInt;
15+
private int code;
1616
private String msg;
1717
private BaseResult baseResult;
1818

1919
BaseException() {
2020
super();
2121
}
2222

23-
BaseException(int codeInt, String msg) {
23+
BaseException(int code, String msg) {
2424
super(msg);
25-
this.codeInt = codeInt;
25+
this.code = code;
2626
this.msg = msg;
27-
this.baseResult = new BaseResult(codeInt, msg);
27+
this.baseResult = new BaseResult(code, msg);
2828
}
2929

30-
public int getCodeInt() {
31-
return codeInt;
30+
public int getCode() {
31+
return code;
3232
}
3333

34-
public void setCodeInt(int codeInt) {
35-
this.codeInt = codeInt;
34+
public void setCode(int code) {
35+
this.code = code;
3636
}
3737

3838
public String getMsg() {

eureka-customer/src/main/java/com/coderqian/eurekacustomer/controller/TestController.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,6 @@ public class TestController {
2727
@Autowired
2828
private TestService testService;
2929

30-
@Value("${profile}")
31-
private String profile;
32-
3330
@ApiOperation(value = "返回用户输入的结果", notes = "返回用户输入的结果")
3431
@RequestMapping(value = "/result", method = RequestMethod.GET)
3532
public String test(@RequestParam(value = "text") String text) {
@@ -48,9 +45,9 @@ public BaseResult testBaseResult(@RequestParam(value = "text") String text) {
4845
return testService.testBaseResult(text);
4946
}
5047

51-
@ApiOperation(value = "测试读取配置文件中的值", notes = "测试读取配置文件中的值")
52-
@RequestMapping(value = "/config", method = RequestMethod.GET)
53-
public String testConfig() {
54-
return profile;
48+
@ApiOperation(value = "根据用户id获取用户信息", notes = "根据用户id获取用户信息")
49+
@RequestMapping(value = "/find", method = RequestMethod.GET)
50+
public BaseResult testMybatis(@RequestParam("id") String id) {
51+
return testService.testMybatis(id);
5552
}
5653
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.coderqian.eurekacustomer.entity;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Data;
5+
import lombok.NoArgsConstructor;
6+
import org.apache.ibatis.type.Alias;
7+
8+
import java.io.Serializable;
9+
10+
/**
11+
* @author qianliqing
12+
* @date 2018-10-02 下午2:33
13+
14+
*/
15+
16+
@Data
17+
@NoArgsConstructor
18+
@AllArgsConstructor
19+
public class User implements Serializable {
20+
21+
private static final long serialVersionUID = 1L;
22+
23+
private String id;
24+
25+
private String name;
26+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.coderqian.eurekacustomer.mapper;
2+
3+
import com.coderqian.eurekacustomer.entity.User;
4+
import org.apache.ibatis.annotations.Mapper;
5+
6+
/**
7+
* @author qianliqing
8+
* @date 2018-10-02 下午2:31
9+
10+
*/
11+
12+
@Mapper
13+
public interface UserMapper {
14+
15+
/**
16+
* 根据用户id查询
17+
*
18+
* @param id 用户id
19+
* @return
20+
*/
21+
User findUserById(String id);
22+
}

eureka-customer/src/main/java/com/coderqian/eurekacustomer/service/TestService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,12 @@ public interface TestService {
3333
* @return
3434
*/
3535
BaseResult testBaseResult(String text);
36+
37+
/**
38+
* 测试mybatis接口
39+
*
40+
* @param id 用户id
41+
* @return
42+
*/
43+
BaseResult testMybatis(String id);
3644
}

eureka-customer/src/main/java/com/coderqian/eurekacustomer/service/impl/TestServiceImpl.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.coderqian.eurekacustomer.service.impl;
22

33
import com.coderqian.eurekacustomer.common.BaseResult;
4+
import com.coderqian.eurekacustomer.common.BaseResultFactory;
45
import com.coderqian.eurekacustomer.common.constant.Code;
56
import com.coderqian.eurekacustomer.common.exception.BusinessException;
7+
import com.coderqian.eurekacustomer.mapper.UserMapper;
68
import com.coderqian.eurekacustomer.service.TestService;
9+
import org.springframework.beans.factory.annotation.Autowired;
710
import org.springframework.stereotype.Service;
811

912
/**
@@ -15,6 +18,9 @@
1518
@Service
1619
public class TestServiceImpl implements TestService {
1720

21+
@Autowired
22+
private UserMapper userMapper;
23+
1824
@Override
1925
public String test(String text) {
2026
return text;
@@ -29,4 +35,9 @@ public void testException(String text) throws BusinessException {
2935
public BaseResult testBaseResult(String text) {
3036
return new BaseResult(text);
3137
}
38+
39+
@Override
40+
public BaseResult testMybatis(String id) {
41+
return BaseResultFactory.createSuccessResult(userMapper.findUserById(id));
42+
}
3243
}

eureka-customer/src/main/resources/application.properties

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,7 @@ spring.datasource.maxWait=60000
2121
# 配置监控统计拦截的filters,用于监控SQL,wall用于防火墙
2222
spring.datasource.filters=stat,wall
2323

24-
logging.config=classpath:log4j2.xml
24+
logging.config=classpath:log4j2.xml
25+
26+
mybatis.mapper-locations=classpath:mapper/*.xml
27+
mybatis.config-location=classpath:config/mybatis-config.xml

0 commit comments

Comments
 (0)