- 浏览: 2551665 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
Spring Boot and RESTful API(8)MySQL Database
Official Web Site - MyIbatis and Spring Boot
https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start
pom.xml to support the dependency.
<properties>
<java.version>1.8</java.version>
<orika.version>1.5.1</orika.version>
<myibatis.version>3.4.3</myibatis.version>
<myibatisstarter.version>1.3.0</myibatisstarter.version>
</properties>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${myibatisstarter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${myibatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Configuration in Properties file application.yaml
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxxx:7778/jobs2careers?useUnicode=true&characterEncoding=utf-8
username: writer
password: xxxxxx
mybatis:
type-aliases-package: com.sillycat.jobsmonitorapi.domain
POJO class to host the data structure CampaignJobCount.java
package com.sillycat.jobsmonitorapi.domain;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang3.builder.ToStringBuilder;
public class CampaignJobCount implements Serializable {
private static final long serialVersionUID = -5665717574357320248L;
private Integer campaignID;
private Integer liveOld;
private Integer liveNew;
private Integer pausedOld;
private Integer pausedNew;
private Integer dailyCappedOld;
private Integer dailyCappedNew;
private Date countTime;
…snip...
}
Mapper interface with Annotation to Query Data CampaignJobCountMapper.java
package com.sillycat.jobsmonitorapi.repository;
import java.util.Date;
import java.util.List;
import com.sillycat.jobsmonitorapi.domain.CampaignJobCount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CampaignJobCountMapper {
@Select("SELECT * FROM campaign_job_count_solr_new WHERE campaign_id = #{campaignID}")
@Results({ @Result(property = "campaignID", column = "campaign_id"),
@Result(property = "liveOld", column = "count_live_old"),
@Result(property = "liveNew", column = "count_live_new"),
@Result(property = "pausedOld", column = "count_paused_old"),
@Result(property = "pausedNew", column = "count_paused_new"),
@Result(property = "dailyCappedOld", column = "count_daily_capped_old"),
@Result(property = "dailyCappedNew", column = "count_daily_capped_new"),
@Result(property = "countTime", column = "count_time", javaType = Date.class) })
public List<CampaignJobCount> loadCampaignJobCountByCampaignID(@Param("campaignID") Integer campaignID);
@Select("SELECT * FROM campaign_job_count_solr_new WHERE campaign_id = #{campaignID} AND count_time > #{queryBackTime}")
@Results({ @Result(property = "campaignID", column = "campaign_id"),
@Result(property = "liveOld", column = "count_live_old"),
@Result(property = "liveNew", column = "count_live_new"),
@Result(property = "pausedOld", column = "count_paused_old"),
@Result(property = "pausedNew", column = "count_paused_new"),
@Result(property = "dailyCappedOld", column = "count_daily_capped_old"),
@Result(property = "dailyCappedNew", column = "count_daily_capped_new"),
@Result(property = "countTime", column = "count_time", javaType = Date.class) })
public List<CampaignJobCount> loadCampaignJobCountByCampaignIDAndDate(@Param("campaignID") Integer campaignID,
@Param("queryBackTime") Date queryBackTime);
}
Unit Test to cover that MySQL Interface Mapper, CampaignJobCountMapperTest.java
package com.sillycat.jobsmonitorapi.repository;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.http.util.Asserts;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.sillycat.jobsmonitorapi.domain.CampaignJobCount;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CampaignJobCountMapperTest {
@Autowired
private CampaignJobCountMapper campaignJobCountMapper;
@Test
public void testQuery() throws Exception {
List<CampaignJobCount> jobcounts = campaignJobCountMapper.loadCampaignJobCountByCampaignID(0);
Asserts.notNull(jobcounts, "result1 is not null");
System.out.println(jobcounts.size());
System.out.println(jobcounts.get(0));
List<CampaignJobCount> jobcountsToday = campaignJobCountMapper.loadCampaignJobCountByCampaignIDAndDate(0,
yesterday());
Asserts.notNull(jobcountsToday, "result2 is not null");
System.out.println(jobcountsToday.size());
System.out.println(jobcountsToday.get(0));
}
private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
}
Run the Unit Test
>mvn -Dtest=CampaignJobCountMapperTest test
References:
http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/
http://blog.netgloo.com/2014/08/17/use-mysql-database-in-a-spring-boot-web-application-through-hibernate/
http://www.ityouknow.com/springboot/2016/11/06/springboot(%E5%85%AD)-%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E4%BD%BF%E7%94%A8mybatis.html
https://aisensiy.github.io/2017/05/04/spring-mvc-and-mybatis/
http://blog.didispace.com/springbootmybatis/
https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
Official Web Site - MyIbatis and Spring Boot
https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start
pom.xml to support the dependency.
<properties>
<java.version>1.8</java.version>
<orika.version>1.5.1</orika.version>
<myibatis.version>3.4.3</myibatis.version>
<myibatisstarter.version>1.3.0</myibatisstarter.version>
</properties>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${myibatisstarter.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${myibatis.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
Configuration in Properties file application.yaml
spring:
datasource:
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://xxxxxx:7778/jobs2careers?useUnicode=true&characterEncoding=utf-8
username: writer
password: xxxxxx
mybatis:
type-aliases-package: com.sillycat.jobsmonitorapi.domain
POJO class to host the data structure CampaignJobCount.java
package com.sillycat.jobsmonitorapi.domain;
import java.io.Serializable;
import java.util.Date;
import org.apache.commons.lang3.builder.ToStringBuilder;
public class CampaignJobCount implements Serializable {
private static final long serialVersionUID = -5665717574357320248L;
private Integer campaignID;
private Integer liveOld;
private Integer liveNew;
private Integer pausedOld;
private Integer pausedNew;
private Integer dailyCappedOld;
private Integer dailyCappedNew;
private Date countTime;
…snip...
}
Mapper interface with Annotation to Query Data CampaignJobCountMapper.java
package com.sillycat.jobsmonitorapi.repository;
import java.util.Date;
import java.util.List;
import com.sillycat.jobsmonitorapi.domain.CampaignJobCount;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
@Mapper
public interface CampaignJobCountMapper {
@Select("SELECT * FROM campaign_job_count_solr_new WHERE campaign_id = #{campaignID}")
@Results({ @Result(property = "campaignID", column = "campaign_id"),
@Result(property = "liveOld", column = "count_live_old"),
@Result(property = "liveNew", column = "count_live_new"),
@Result(property = "pausedOld", column = "count_paused_old"),
@Result(property = "pausedNew", column = "count_paused_new"),
@Result(property = "dailyCappedOld", column = "count_daily_capped_old"),
@Result(property = "dailyCappedNew", column = "count_daily_capped_new"),
@Result(property = "countTime", column = "count_time", javaType = Date.class) })
public List<CampaignJobCount> loadCampaignJobCountByCampaignID(@Param("campaignID") Integer campaignID);
@Select("SELECT * FROM campaign_job_count_solr_new WHERE campaign_id = #{campaignID} AND count_time > #{queryBackTime}")
@Results({ @Result(property = "campaignID", column = "campaign_id"),
@Result(property = "liveOld", column = "count_live_old"),
@Result(property = "liveNew", column = "count_live_new"),
@Result(property = "pausedOld", column = "count_paused_old"),
@Result(property = "pausedNew", column = "count_paused_new"),
@Result(property = "dailyCappedOld", column = "count_daily_capped_old"),
@Result(property = "dailyCappedNew", column = "count_daily_capped_new"),
@Result(property = "countTime", column = "count_time", javaType = Date.class) })
public List<CampaignJobCount> loadCampaignJobCountByCampaignIDAndDate(@Param("campaignID") Integer campaignID,
@Param("queryBackTime") Date queryBackTime);
}
Unit Test to cover that MySQL Interface Mapper, CampaignJobCountMapperTest.java
package com.sillycat.jobsmonitorapi.repository;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.http.util.Asserts;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import com.sillycat.jobsmonitorapi.domain.CampaignJobCount;
@RunWith(SpringRunner.class)
@SpringBootTest
public class CampaignJobCountMapperTest {
@Autowired
private CampaignJobCountMapper campaignJobCountMapper;
@Test
public void testQuery() throws Exception {
List<CampaignJobCount> jobcounts = campaignJobCountMapper.loadCampaignJobCountByCampaignID(0);
Asserts.notNull(jobcounts, "result1 is not null");
System.out.println(jobcounts.size());
System.out.println(jobcounts.get(0));
List<CampaignJobCount> jobcountsToday = campaignJobCountMapper.loadCampaignJobCountByCampaignIDAndDate(0,
yesterday());
Asserts.notNull(jobcountsToday, "result2 is not null");
System.out.println(jobcountsToday.size());
System.out.println(jobcountsToday.get(0));
}
private Date yesterday() {
final Calendar cal = Calendar.getInstance();
cal.add(Calendar.DATE, -1);
return cal.getTime();
}
}
Run the Unit Test
>mvn -Dtest=CampaignJobCountMapperTest test
References:
http://blog.netgloo.com/2014/10/27/using-mysql-in-spring-boot-via-spring-data-jpa-and-hibernate/
http://blog.netgloo.com/2014/08/17/use-mysql-database-in-a-spring-boot-web-application-through-hibernate/
http://www.ityouknow.com/springboot/2016/11/06/springboot(%E5%85%AD)-%E5%A6%82%E4%BD%95%E4%BC%98%E9%9B%85%E7%9A%84%E4%BD%BF%E7%94%A8mybatis.html
https://aisensiy.github.io/2017/05/04/spring-mvc-and-mybatis/
http://blog.didispace.com/springbootmybatis/
https://github.com/mybatis/spring-boot-starter/wiki/Quick-Start
http://www.mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
发表评论
-
Update Site will come soon
2021-06-02 04:10 1677I am still keep notes my tech n ... -
Stop Update Here
2020-04-28 09:00 316I will stop update here, and mo ... -
NodeJS12 and Zlib
2020-04-01 07:44 475NodeJS12 and Zlib It works as ... -
Docker Swarm 2020(2)Docker Swarm and Portainer
2020-03-31 23:18 368Docker Swarm 2020(2)Docker Swar ... -
Docker Swarm 2020(1)Simply Install and Use Swarm
2020-03-31 07:58 369Docker Swarm 2020(1)Simply Inst ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 336Traefik 2020(1)Introduction and ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 430Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 435Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 374Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 455VPN Server 2020(2)Docker on Cen ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 384Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 478NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 423Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 247GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 450GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 317Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 293Serverless with NodeJS and Tenc ...
相关推荐
1. **Java后端开发**:在Java中,我们通常使用Spring Boot框架来构建RESTful API,因为它提供了丰富的功能和简洁的配置。Spring MVC模块是Spring Boot中的关键部分,用于处理HTTP请求。我们可以定义控制器类,使用`@...
Spring Boot,MySQL,JPA,Hibernate Rest API教程使用Spring Boot,Mysql,JPA和Hibernate为简单的记笔记应用程序构建Restful CRUD API。要求Java-1.8.x Maven-3.xx MySQL的-5.xx设定步骤1.克隆应用程序git clone ...
在博客系统中,MySQL用于存储博客文章、用户信息等数据,通过JDBC(Java Database Connectivity)接口与Spring Boot进行交互,执行CRUD操作。 5. **Redis** Redis是一个高性能的键值存储系统,常被用于缓存和消息...
可以创建RESTful API接口,通过HTTP请求测试数据的增删改查操作,验证Spring Boot、MySQL和MyBatis的整合是否成功。 通过"spring-boot-mybatis-demo-master"这个项目,你可以学习到如何将这些组件整合到一起,形成...
在本实习实训管理系统中,MySQL用于存储学生、教师、实训课程、实习记录等各种实体的数据,通过JDBC(Java Database Connectivity)驱动与Spring Boot应用进行连接。数据库设计应包括多个表,如用户表、课程表、实习...
8. **Testing**: 微服务的测试包括单元测试、集成测试和端到端测试,Spring Boot提供了丰富的测试支持,如`@SpringBootTest`注解用于启动整个应用进行集成测试。 9. **Logging and Monitoring**: 日志管理和监控...
在Spring Boot的`pom.xml`文件中,引入Spring Boot的`spring-boot-starter-quartz`和`spring-boot-starter-data-jpa`依赖,同时也要添加MySQL的JDBC驱动依赖,以便与数据库交互。 ```xml <groupId>org.spring...
结合Spring Boot实现RESTful API** 为了提供接口服务,我们可以创建Spring MVC控制器,将上述功能包装成RESTful API。例如,创建一个用于启动流程的API: ```java @RestController @RequestMapping("/api/process...
在这个民宿管理系统中,MySQL用于存储房源信息、预订记录、用户数据等关键信息,通过JDBC(Java Database Connectivity)接口与Spring Boot应用进行通信。数据库的设计应遵循规范化原则,以提高数据一致性并降低数据...
- **Spring MVC 简介**:Spring Boot 支持 Spring MVC 框架,这是一个基于 Model-View-Controller 架构模式的轻量级框架,非常适合用于构建 RESTful 服务。 - **创建 RESTful 控制器**:在 Spring Boot 中,可以通过...
对于数据库支持,Spring Boot可以轻松地与多种数据库如MySQL、PostgreSQL、MongoDB等配合使用,提供了JPA(Java Persistence API)和R2DBC(Reactive Relational Database Connectivity)等库来处理数据操作。...
本示例将详细介绍如何在Spring Boot项目中利用JDBC(Java Database Connectivity)连接MySQL数据库,展示一个简单的数据库操作DEMO,包括连接数据库、访问数据库中的"mrchen"数据库下的"student"表格。 首先,我们...
在这个项目中,MySQL用于存储用户信息、考试题目、答案、成绩等数据,通过JDBC(Java Database Connectivity)接口与Spring Boot应用程序进行通信。 3. **数据库设计**: 在建筑造价师资格考试网站中,数据库设计...
在本文中,我们将深入探讨如何使用Spring Boot框架与MySQL数据库进行集成,主要通过Java Persistence API (JPA) 来实现数据的增删改查操作。首先,我们需要理解Spring Boot的核心特性,它简化了传统Spring应用的初始...
通过以上步骤,我们可以创建一个基本的Spring Boot应用,它能处理HTTP请求,与MySQL数据库交互,并且具备与钉钉API集成的基础。随着项目的进展,你可以添加更多的功能,如错误处理、安全性设置、日志记录、单元测试...
We'll start by creating a web application using Spring MVC, Spring Data, the World Bank API for some statistics on different countries, and MySQL database. Moving ahead, you'll build a RESTful web ...
在本项目中,"Spring-Boot-Auth" 是一个基于Spring Boot实现的Restful会员登录API,它专注于提供用户注册和登录功能。Spring Boot是Java生态中的一个热门框架,它简化了新Spring应用的初始搭建以及开发过程。下面将...
Spring Boot,MySQL,JPA,Hibernate Rest API教程使用Spring Boot,Mysql,JPA和Hibernate为简单的记笔记应用程序构建Restful CRUD API。要求Java-1.8.x Maven-3.xx MySQL的-5.xx设定步骤1.克隆应用程序git clone ...
本练习仅侧重于REST API,并使用spring boot和mybatis以及MySQL Database进行构建。 特征 注册/登录/生成JWT令牌 提取特定用户的卡片列表 提取特定卡的卡详细信息 激活卡 停用卡 更改卡的每日限额 数据库 该项目...