Explorar o código

mybatis plus 插件测试

zhang %!s(int64=2) %!d(string=hai) anos
pai
achega
5fe5afc8d5

+ 1 - 3
src/main/java/com/api/controller/ApiRegisterController.java

@@ -30,7 +30,7 @@ import java.util.Date;
30 30
  * @author Mark sunlightcs@gmail.com
31 31
  */
32 32
 @RestController
33
-@RequestMapping("/api")
33
+@RequestMapping("/apiRegister")
34 34
 @Api(tags="注册接口")
35 35
 public class ApiRegisterController {
36 36
     @Autowired
@@ -41,14 +41,12 @@ public class ApiRegisterController {
41 41
     public R register(@RequestBody RegisterForm form){
42 42
         //表单校验
43 43
         ValidatorUtils.validateEntity(form);
44
-
45 44
         UserEntity user = new UserEntity();
46 45
         user.setMobile(form.getMobile());
47 46
         user.setUsername(form.getMobile());
48 47
         user.setPassword(DigestUtils.sha256Hex(form.getPassword()));
49 48
         user.setCreateTime(new Date());
50 49
         userService.save(user);
51
-
52 50
         return R.ok();
53 51
     }
54 52
 }

+ 18 - 0
src/main/java/com/api/controller/ApiTestController.java

@@ -9,10 +9,14 @@
9 9
 package com.api.controller;
10 10
 
11 11
 import com.api.dao.TokenDao;
12
+import com.api.service.TokenService;
12 13
 import com.common.utils.R;
13 14
 import com.api.annotation.Login;
14 15
 import com.api.annotation.LoginUser;
15 16
 import com.api.entity.UserEntity;
17
+import com.doudian.open.api.token_create.TokenCreateRequest;
18
+import com.doudian.open.core.DoudianOpClient;
19
+import com.doudian.open.core.GlobalConfig;
16 20
 import io.swagger.annotations.Api;
17 21
 import io.swagger.annotations.ApiOperation;
18 22
 import org.springframework.beans.factory.BeanFactory;
@@ -42,6 +46,8 @@ public class ApiTestController extends AbsctactController {
42 46
     BeanFactory beanFactory;
43 47
     @Autowired
44 48
     TokenDao tokenDao;
49
+    @Autowired
50
+    TokenService tokenService;
45 51
     @Login
46 52
     @GetMapping("userInfo")
47 53
     @ApiOperation(value="获取用户信息", response=UserEntity.class)
@@ -59,11 +65,23 @@ public class ApiTestController extends AbsctactController {
59 65
     @GetMapping("notToken")
60 66
     @ApiOperation("忽略Token验证测试")
61 67
     public R notToken(HttpServletRequest request, HttpSession httpSession){
68
+        GlobalConfig.initAppKey("7090080248619877901");
69
+        GlobalConfig.initAppSecret("a4d9d3e6-aa69-4f0c-bffa-be4039e74262");
62 70
         Object beanNamesForType = applicationContext.getBean(TokenDao.class);
71
+        TokenCreateRequest tokenCreateRequest = new TokenCreateRequest();
63 72
         TokenDao bean = beanFactory.getBean(TokenDao.class);
64 73
         TokenDao tokenDao = (TokenDao) beanNamesForType;
65 74
 //        return R.ok().put("msg", "无需token也能访问。。。");
66 75
         return R.ok().put( "无需token也能访问。。。");
67 76
     }
68 77
 
78
+//    @GetMapping("notToken")
79
+//    @ApiOperation("忽略Token验证测试")
80
+//    public R createToken(HttpServletRequest request, HttpSession httpSession){
81
+//        tokenService.testCreateToken(request);
82
+////        return R.ok().put("msg", "无需token也能访问。。。");
83
+//        return R.ok().put( "无需token也能访问。。。");
84
+//    }
85
+
86
+
69 87
 }

+ 14 - 0
src/main/java/com/api/dao/AccountMapper.java

@@ -0,0 +1,14 @@
1
+package com.api.dao;
2
+
3
+import com.api.entity.AccountEntity;
4
+import com.baomidou.mybatisplus.core.mapper.BaseMapper;
5
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6
+import org.apache.ibatis.annotations.Mapper;
7
+import org.apache.ibatis.annotations.Param;
8
+
9
+@Mapper
10
+public interface AccountMapper extends BaseMapper<AccountEntity> {
11
+    Page<AccountEntity> getPageVO(
12
+            @Param("page") Page<AccountEntity> page,
13
+            @Param("dep_id") Integer depId);
14
+}

+ 18 - 0
src/main/java/com/api/entity/AccountEntity.java

@@ -0,0 +1,18 @@
1
+package com.api.entity;
2
+
3
+import com.baomidou.mybatisplus.annotation.TableField;
4
+import com.baomidou.mybatisplus.annotation.TableId;
5
+import com.baomidou.mybatisplus.annotation.TableName;
6
+import lombok.Data;
7
+
8
+@Data
9
+@TableName("account")
10
+public class AccountEntity {
11
+    @TableId("id")
12
+    private Integer id;
13
+    // tableField 表示将数据库中的字段映射为实体中的属性
14
+    @TableField("user_name")
15
+    private String userName;
16
+    private Double balance;
17
+    private Integer depId;
18
+}

+ 7 - 0
src/main/java/com/api/service/AccountService.java

@@ -0,0 +1,7 @@
1
+package com.api.service;
2
+
3
+import com.api.entity.AccountEntity;
4
+import com.baomidou.mybatisplus.extension.service.IService;
5
+
6
+public interface AccountService extends IService<AccountEntity> {
7
+}

+ 4 - 0
src/main/java/com/api/service/TokenService.java

@@ -11,6 +11,8 @@ package com.api.service;
11 11
 import com.baomidou.mybatisplus.extension.service.IService;
12 12
 import com.api.entity.TokenEntity;
13 13
 
14
+import javax.servlet.http.HttpServletRequest;
15
+
14 16
 /**
15 17
  * 用户Token
16 18
  *
@@ -33,4 +35,6 @@ public interface TokenService extends IService<TokenEntity> {
33 35
 	 */
34 36
 	void expireToken(long userId);
35 37
 
38
+	void testCreateToken(HttpServletRequest httpServletRequest);
39
+
36 40
 }

+ 11 - 0
src/main/java/com/api/service/impl/AccountServiceImpl.java

@@ -0,0 +1,11 @@
1
+package com.api.service.impl;
2
+
3
+import com.api.dao.AccountMapper;
4
+import com.api.entity.AccountEntity;
5
+import com.api.service.AccountService;
6
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
7
+import org.springframework.stereotype.Service;
8
+
9
+@Service
10
+public class AccountServiceImpl extends ServiceImpl<AccountMapper, AccountEntity> implements AccountService {
11
+}

+ 25 - 0
src/main/java/com/api/service/impl/TokenServiceImpl.java

@@ -13,8 +13,16 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
13 13
 import com.api.dao.TokenDao;
14 14
 import com.api.entity.TokenEntity;
15 15
 import com.api.service.TokenService;
16
+import com.doudian.open.core.DoudianOpSpi;
17
+import com.doudian.open.core.DoudianOpSpiBizHandler;
18
+import com.doudian.open.core.DoudianOpSpiContext;
19
+import com.doudian.open.core.GlobalConfig;
20
+import com.doudian.open.spi.demo_spi.DemoSpiRequest;
21
+import com.doudian.open.spi.demo_spi.data.DemoSpiData;
22
+import com.doudian.open.spi.demo_spi.param.DemoSpiParam;
16 23
 import org.springframework.stereotype.Service;
17 24
 
25
+import javax.servlet.http.HttpServletRequest;
18 26
 import java.util.Date;
19 27
 import java.util.UUID;
20 28
 
@@ -63,6 +71,23 @@ public class TokenServiceImpl extends ServiceImpl<TokenDao, TokenEntity> impleme
63 71
 		this.saveOrUpdate(tokenEntity);
64 72
 	}
65 73
 
74
+	@Override
75
+	public void testCreateToken(HttpServletRequest httpServletRequest) {
76
+		GlobalConfig.initAppKey("7090080248619877901");
77
+		GlobalConfig.initAppSecret("a4d9d3e6-aa69-4f0c-bffa-be4039e74262");
78
+		String s = DoudianOpSpi.config(DemoSpiRequest.class, this.bizHandler, httpServletRequest).responseJson();
79
+
80
+	}
81
+	private final DoudianOpSpiBizHandler bizHandler = new DoudianOpSpiBizHandler() {
82
+		public void handle(DoudianOpSpiContext context) {
83
+			DemoSpiParam param = (DemoSpiParam)context.getParam();
84
+			DemoSpiData data = (DemoSpiData)context.getData();
85
+			context.wrapSuccess();
86
+			context.wrapError(100002L, "系统错误");
87
+		}
88
+	};
89
+
90
+
66 91
 	private String generateToken(){
67 92
 		return UUID.randomUUID().toString().replace("-", "");
68 93
 	}

+ 3 - 3
src/main/resources/application-dev.yml

@@ -5,9 +5,9 @@ spring:
5 5
     type: com.alibaba.druid.pool.DruidDataSource
6 6
     druid:
7 7
       driver-class-name: com.mysql.cj.jdbc.Driver
8
-      url: jdbc:mysql://47.110.156.18:3306/sczn_eshop_test?zeroDateTimeBehavior=convertToNull&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
9
-      username: sczn
10
-      password: sczn159357
8
+      url: jdbc:mysql://localhost:3306/test?zeroDateTimeBehavior=convertToNull&useUnicode=true&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
9
+      username: root
10
+      password:
11 11
       initial-size: 10
12 12
       max-active: 100
13 13
       min-idle: 10

+ 3 - 2
src/main/resources/application.yml

@@ -35,12 +35,11 @@ renren:
35 35
   redis:
36 36
     open: false  # 是否开启redis缓存  true开启   false关闭
37 37
 
38
-
39 38
 #mybatis
40 39
 mybatis-plus:
41 40
   mapper-locations: classpath*:/mapper/**/*.xml
42 41
   #实体扫描,多个package用逗号或者分号分隔
43
-  typeAliasesPackage: com.api.*.entity
42
+  typeAliasesPackage: com.api.entity
44 43
   global-config:
45 44
     #数据库相关配置
46 45
     db-config:
@@ -59,5 +58,7 @@ mybatis-plus:
59 58
     cache-enabled: false
60 59
     call-setters-on-nulls: true
61 60
     jdbc-type-for-null: 'null'
61
+    log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl
62
+
62 63
 
63 64
 

+ 1 - 1
src/main/resources/logback-spring.xml

@@ -8,7 +8,7 @@
8 8
     <springProfile name="dev,test">
9 9
         <logger name="org.springframework.web" level="INFO"/>
10 10
         <logger name="org.springboot.sample" level="INFO" />
11
-        <logger name="io.renren" level="DEBUG" />
11
+        <logger name="com.api.dao" level="DEBUG" />
12 12
     </springProfile>
13 13
 
14 14
     <!-- 生产环境 -->

+ 8 - 0
src/main/resources/mapper/AccountMapper.xml

@@ -0,0 +1,8 @@
1
+<?xml version="1.0" encoding="UTF-8"?>
2
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3
+
4
+<mapper namespace="com.api.dao.AccountMapper">
5
+    <select id="getPageVO" resultType="AccountEntity">
6
+        select id,user_name,balance,dep_id from account where dep_id = #{dep_id}
7
+    </select>
8
+</mapper>

+ 59 - 0
src/main/test/java/com/api/MybatisPlusTest.java

@@ -0,0 +1,59 @@
1
+package com.api;
2
+
3
+import com.api.dao.AccountMapper;
4
+import com.api.entity.AccountEntity;
5
+import com.api.service.AccountService;
6
+import com.api.service.UserService;
7
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
8
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
9
+import com.baomidou.mybatisplus.core.metadata.IPage;
10
+import com.baomidou.mybatisplus.core.toolkit.StringUtils;
11
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
12
+import org.junit.Test;
13
+import org.junit.runner.RunWith;
14
+import org.springframework.beans.factory.annotation.Autowired;
15
+import org.springframework.boot.test.context.SpringBootTest;
16
+import org.springframework.test.context.junit4.SpringRunner;
17
+
18
+import java.util.List;
19
+
20
+@RunWith(SpringRunner.class)
21
+@SpringBootTest
22
+public class MybatisPlusTest {
23
+    @Autowired
24
+    AccountMapper accountMapper;
25
+    @Autowired
26
+    AccountService accountService;
27
+    @Test
28
+    public void test1() {
29
+        QueryWrapper<AccountEntity> accountEntityQueryWrapper = new QueryWrapper<>();
30
+        accountEntityQueryWrapper.eq("id",16);
31
+        AccountEntity accountEntity = accountMapper.selectOne(accountEntityQueryWrapper);
32
+    }
33
+    @Test
34
+    public void test2() {
35
+//        StringUtils.isNotBlank()
36
+        // 使用lambdaWrapper
37
+        LambdaQueryWrapper<AccountEntity> accountEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
38
+        accountEntityLambdaQueryWrapper.eq(AccountEntity::getDepId,1);
39
+        List<AccountEntity> list = accountService.list(accountEntityLambdaQueryWrapper);
40
+    }
41
+    @Test
42
+    public void testPage() {
43
+        Page<AccountEntity> accountEntityPage = new Page<>();
44
+        accountEntityPage.setCurrent(2);
45
+        accountEntityPage.setSize(2);
46
+        LambdaQueryWrapper<AccountEntity> accountEntityLambdaQueryWrapper = new LambdaQueryWrapper<>();
47
+        accountEntityLambdaQueryWrapper.eq(AccountEntity::getDepId,1);
48
+        IPage<AccountEntity> page = accountService.page(accountEntityPage, accountEntityLambdaQueryWrapper);
49
+    }
50
+    @Test
51
+    public void testMapperPage() {
52
+        Page<AccountEntity> accountEntityPage = new Page<>();
53
+        accountEntityPage.setCurrent(2);
54
+        accountEntityPage.setSize(2);
55
+        accountEntityPage = accountMapper.getPageVO(accountEntityPage, 1);
56
+    }
57
+
58
+
59
+}