springboot使用mybatis_使用springboot+mybatis数据库存储服务化
在之前自己写过的接口测试框架中,使用了MySQL记录了各种请求响应以及用例等等的信息,为了提高存储速度,我单独写了一个数据库的存储的服务,部署在内网的服务器上。当有需要记录的信息时,直接把信息发送到这个服务的固定接口中,实现了数据库的异步存储。在学习了springboot和mybatis框架之后,觉得使用mybatis再写一遍这个功能。由于保留了之前的服务的代码,下面只分享一下新的功能的实现代码。
下面是springboot启动类的代码:
package com.fun; import org.mybatis.spring.annotation.MapperScan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration;import org.springframework.context.annotation.Configuration; @Configuration@MapperScan("com.fun.dao")//扫描包下面接口@SpringBootApplication(exclude ={MongoAutoConfiguration.class})public class ApiTestMysqlserviceApplication { public static void main(String[] args) { SpringApplication.run(ApiTestMysqlserviceApplication.class, args); System.out.println("-----------------------------------start-----------------------------------"); }}
下面是userDao和userService的相关代码:
package com.fun.dao; import com.fun.model.RequestBean; public interface UserDao { int insertRequest(RequestBean requestBean);}
package com.fun.user.impl; import com.fun.dao.UserDao;import com.fun.model.RequestBean;import com.fun.model.UserDomain;import com.fun.user.UserService;import org.springframework.stereotype.Service; import java.util.List; @Service(value = "userService")public class UserServiceImpl implements UserService { @Override public int insertRequest(RequestBean requestBean) { return userDao.insertRequest(requestBean); } }
package com.fun.user; import com.fun.model.RequestBean;import com.fun.model.UserDomain; public interface UserService { int insertRequest(RequestBean requestBean);}
@PostMapping("/test") @ResponseBody public ResultUtil test( RequestBean requestBean) { logger.info(requestBean.toString()); int i = userService.insertRequest(requestBean); return ResultUtil.build(i); }
下面是mapper.xml的配置:
<?xml version="1.0" encoding="UTF-8" ?><mapper namespace="com.fun.dao.UserDao"> <sql id="REQUEST_TABLE"> request sql> <insert id="insertRequest" parameterType="com.fun.model.RequestBean"> INSERT INTO <include refid="REQUEST_TABLE"/> <trim prefix="(" suffix=")" suffixOverrides=","> domain,api,type,expend_time,data_size,status,code,method,local_ip,local_name,create_time, trim> <trim prefix="VALUES(" suffix=")" suffixOverrides=","> #{domain},#{api},#{type},#{expend_time},#{data_size},#{status},#{code},#{method},#{local_ip},#{local_name},#{create_time} trim> insert>mapper>
下面是项目的property配置:
spring.datasource.url=jdbc:mysql://****:3306/fan?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=truespring.datasource.username=rootspring.datasource.password=+spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.mapper-locations=classpath:mapper/*.xmlmybatis.type-aliases-package=com.fun.model pagehelper.helperDialect=mysqlpagehelper.reasonable=truepagehelper.supportMethodsArguments=truepagehelper.params=count=countSqlpagehelper.returnPageInfo=check
下图是数据库的存储结果:
点击阅读原文,有兴趣的童鞋可以一起交流
springboot使用mybatis_使用springboot+mybatis数据库存储服务化相关推荐
- SpringBoot+Quartz+数据库存储(附完整代码和数据库脚本)
目录 1.pom文件依赖 2.Druid连接池的Quartz扩展类 3 .application.yml文件 4 .修改quartz.properties配置 5 . 在数据库中创建quartz相关的 ...
- 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法
[Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...
- Springboot疫苗接种管理系统-JAVA.JSP【数据库设计、源码、开题报告】
摘 要 预防预接种工作实行网络信息化管理,是我国免疫规划工作发展的需要.接种信息实行网络信息化不仅是预防接种工作步入了一个新的台阶,更重要的是解决了多年疫苗接种过程种,免疫接种剂次不清,难以全程有效接 ...
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- Springboot毕设项目基于SpringBoot的学生宿舍水电费缴费系统的设计与实现2py81(java+VUE+Mybatis+Maven+Mysql)
Springboot毕设项目基于SpringBoot的学生宿舍水电费缴费系统的设计与实现2py81(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + ...
- SpringBoot系列课程(二)-SpringBoot整合MyBatis
1.SpringBoot和MyBatis整合 1.2. 准备数据 SET FOREIGN_KEY_CHECKS=0;-- ---------------------------- -- Table s ...
- 基于Springboot + Thymeleaf 的招聘网站(源代码+数据库) 026
部分代码地址 https://gitee.com/ynwynwyn/springboot-recruit-public 基于Springboot + Thymeleaf 的招聘网站(源代码+数据库) ...
- SpringBoot员工管理系统(整合Mybatis+mysql)
SpringBoot员工管理系统(整合Mybatis+mysql) 前部分:https://blog.csdn.net/weixin_43501359/article/details/11271466 ...
- springboot毕设项目基于springboot的模拟面试平台 7tch0(java+VUE+Mybatis+Maven+Mysql)
springboot毕设项目基于springboot的模拟面试平台 7tch0(java+VUE+Mybatis+Maven+Mysql) 项目运行 环境配置: Jdk1.8 + Tomcat8.5 ...
最新文章
- 博客园有人随笔总数10000+?
- ZigBee技术的应用和优势
- Redis:10---List对象
- 错过SaaS,就是错过这个时代
- 从.NET中委托写法的演变谈开去(中):Lambda表达式及其优势
- 远场(far-field)语音识别的主流技术有哪些?
- 蓝鲸ERP标准版-进销存-采购管理子系统操作说明1
- dynamipsgui 模拟器模块详细介绍
- LinGo基本用法总结
- Python自制成语接龙小游戏
- SPSS(一)进行单因素方差分析及多重比较检验(图文教程)
- qq邮箱收不到通知邮件的解决方法
- 笃情开源:我和 Apache DolphinScheduler 社区的故事
- 一种基于暗通道先验算法的图像去雾技术研究
- java.lang.IllegalArgumentException: Could not find class异常解决方式
- 安装gtsam遇到的错误
- Spring Data JPA REST Query QueryDSL
- ncist网络空间安全专业护网方向认知实习笔记2021.12 DAY2.1
- Linux花开五洲,争奇斗艳
- JS数组操作 速查手册