赞
踩
在pom.xml中引入依赖
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.4</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
在项目中我们引入了druid用来管理连接数据库的线程池,在pom.xml中引入druid依赖
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid-spring-boot-starter</artifactId>
- <version>1.1.9</version>
- </dependency>
关于druid还可以进行程序对数据库监控的目的,输入服务地址:http://localhost:8084/druid/datasource.html
因为我这边为了方便的区分开发环境与测试环境,所以在创建了线上与线下的两个不同的配置文件
分别为:
application-dev.yml(开发环境配置)
application-prod.yml(生产环境配置)
然后在application.yml中方便的进行开发环境与生产环境的切换
开发环境配置
server: port: 8084 sim: config: #定时5分钟同步中移动接口数据 sync300Interval: 300000 #定时1天同步中移动接口数据 sync86400Interval: 86400000 spring: datasource: name: mysql_sim type: com.alibaba.druid.pool.DruidDataSource #druid相关配置 druid: #监控统计拦截的filters filters: stat driver-class-name: com.mysql.cj.jdbc.Driver #基本属性 url: jdbc:mysql://192.168.1.39:3306/simmanager?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=UTC username: xxx password: 123456 #配置初始化大小/最小/最大 initial-size: 1 min-idle: 1 max-active: 20 #获取连接等待超时时间 max-wait: 60000 #间隔多久进行一次检测,检测需要关闭的空闲连接 time-between-eviction-runs-millis: 60000 #一个连接在池中最小生存的时间 min-evictable-idle-time-millis: 300000 validation-query: SELECT 'x' test-while-idle: true test-on-borrow: false test-on-return: false #打开PSCache,并指定每个连接上PSCache的大小。oracle设为true,mysql设为false。分库分表较多推荐设置为false pool-prepared-statements: false max-pool-prepared-statement-per-connection-size: 20 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.iot.sim.model #MyBatis分页查询插件 pagehelper: helperDialect: mysql reasonable: true supportMethodsArguments: true params: count=countSql
以上准备工作完成后,后续我们开始编码实施
首先在启动类里面增加注解@MapperScan("com.iot.simmanager.dao")
作用:指定要变成实现类的接口所在的包,然后包下面的所有接口在编译之后都会生成相应的实现类
写个获取公司信息的实现示例
model包下增加AdminAgent类
- package com.iot.simmanager.model;
-
- import lombok.Data;
-
- import java.io.Serializable;
- import java.math.BigInteger;
-
- /**
- * @author lenny
- */
- @Data
- public class AdminAgent implements Serializable {
- private BigInteger Id;
- private String AgentName;
- private String AgentCode;
- private String Address;
- private String Bank;
- private String BankNo;
- private String Contacts;
- private String Phone;
- private String Email;
- private String Fax;
- private String PostCode;
- private String AgentGUID;
- private String Describe;
- private String CreateTime;
- private BigInteger Creator;
- }
这里用到了@Data注解,需要在pom.xml下进行依赖引入
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.12</version>
- </dependency>
dao包下增加AdminAgentDao接口
- package com.iot.simmanager.dao;
-
- import com.iot.simmanager.model.AdminAgent;
-
- import java.util.List;
-
- /**
- * @author lenny
- */
- public interface AdminAgentDao {
- /**
- * 查询公司列表
- * @return
- */
- List<AdminAgent> queryAdminAgentList();
- }
service包下增加AdminAgentService
- package com.iot.simmanager.service;
-
- import com.iot.simmanager.dao.AdminAgentDao;
- import com.iot.simmanager.model.AdminAgent;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.util.List;
-
- /**
- * @author lenny
- */
- @Service
- public class AdminAgentService {
- @Autowired
- private AdminAgentDao adminAgentDao;
-
- /**
- * 查询公司列表
- * @return
- */
- public List<AdminAgent> queryAdminAgentList()
- {
- List<AdminAgent> adminAgentList=adminAgentDao.queryAdminAgentList();
- return adminAgentList;
- }
- }
最后再controller包增加AdminAgentController
- package com.iot.simmanager.controller;
-
- import com.iot.simmanager.model.AdminAgent;
- import com.iot.simmanager.service.AdminAgentService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RestController;
-
- import java.util.List;
-
- /**
- * @author lenny
- */
- @RestController
- @RequestMapping("/agent")
- public class AdminAgentController {
- @Autowired
- private AdminAgentService adminAgentService;
-
- @PostMapping(value = "queryAdminAgentList")
- public List<AdminAgent> queryAdminAgentList(){
- try {
- List<AdminAgent> adminAgentList = adminAgentService.queryAdminAgentList();
- return adminAgentList;
- } catch (Exception e) {
- e.printStackTrace();
- return null;
- }
- }
- }
使用Postman进行接口测试
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。