`
sillycat
  • 浏览: 2551665 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Spring Boot and RESTful API(8)MySQL Database

 
阅读更多
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/

分享到:
评论

相关推荐

    restful案例 java mysql

    1. **Java后端开发**:在Java中,我们通常使用Spring Boot框架来构建RESTful API,因为它提供了丰富的功能和简洁的配置。Spring MVC模块是Spring Boot中的关键部分,用于处理HTTP请求。我们可以定义控制器类,使用`@...

    spring-boot-mysql-rest-api-tutorial:使用Spring Boot,Mysql,JPA和Hibernate构建Restful CRUD API

    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 ...

    spring boot vue 博客

    在博客系统中,MySQL用于存储博客文章、用户信息等数据,通过JDBC(Java Database Connectivity)接口与Spring Boot进行交互,执行CRUD操作。 5. **Redis** Redis是一个高性能的键值存储系统,常被用于缓存和消息...

    spring-boot+mysql的演示zip代码

    可以创建RESTful API接口,通过HTTP请求测试数据的增删改查操作,验证Spring Boot、MySQL和MyBatis的整合是否成功。 通过"spring-boot-mybatis-demo-master"这个项目,你可以学习到如何将这些组件整合到一起,形成...

    基于Spring Boot MySQL实现高校实习实训管理系统【优质毕业设计、课程设计项目】.zip

    在本实习实训管理系统中,MySQL用于存储学生、教师、实训课程、实习记录等各种实体的数据,通过JDBC(Java Database Connectivity)驱动与Spring Boot应用进行连接。数据库设计应包括多个表,如用户表、课程表、实习...

    microservices spring boot sample java.rar

    8. **Testing**: 微服务的测试包括单元测试、集成测试和端到端测试,Spring Boot提供了丰富的测试支持,如`@SpringBootTest`注解用于启动整个应用进行集成测试。 9. **Logging and Monitoring**: 日志管理和监控...

    Springboot整合Quartz实现定时任务数据库动态配置

    在Spring Boot的`pom.xml`文件中,引入Spring Boot的`spring-boot-starter-quartz`和`spring-boot-starter-data-jpa`依赖,同时也要添加MySQL的JDBC驱动依赖,以便与数据库交互。 ```xml &lt;groupId&gt;org.spring...

    spring boot整合camunda实例

    结合Spring Boot实现RESTful API** 为了提供接口服务,我们可以创建Spring MVC控制器,将上述功能包装成RESTful API。例如,创建一个用于启动流程的API: ```java @RestController @RequestMapping("/api/process...

    基于Spring Boot MySQL实现民宿管理系统,小程序应用【优质毕业设计、课程设计项目】.zip

    在这个民宿管理系统中,MySQL用于存储房源信息、预订记录、用户数据等关键信息,通过JDBC(Java Database Connectivity)接口与Spring Boot应用进行通信。数据库的设计应遵循规范化原则,以提高数据一致性并降低数据...

    Spring Boot 开源电子书1

    - **Spring MVC 简介**:Spring Boot 支持 Spring MVC 框架,这是一个基于 Model-View-Controller 架构模式的轻量级框架,非常适合用于构建 RESTful 服务。 - **创建 RESTful 控制器**:在 Spring Boot 中,可以通过...

    spring-boot-2.1.0.M1.zip

    对于数据库支持,Spring Boot可以轻松地与多种数据库如MySQL、PostgreSQL、MongoDB等配合使用,提供了JPA(Java Persistence API)和R2DBC(Reactive Relational Database Connectivity)等库来处理数据操作。...

    Java spring boot链接mql数据库 JDBC

    本示例将详细介绍如何在Spring Boot项目中利用JDBC(Java Database Connectivity)连接MySQL数据库,展示一个简单的数据库操作DEMO,包括连接数据库、访问数据库中的"mrchen"数据库下的"student"表格。 首先,我们...

    基于Spring Boot MySQL实现建筑造价师资格考试网站【优质毕业设计、课程设计项目】.zip

    在这个项目中,MySQL用于存储用户信息、考试题目、答案、成绩等数据,通过JDBC(Java Database Connectivity)接口与Spring Boot应用程序进行通信。 3. **数据库设计**: 在建筑造价师资格考试网站中,数据库设计...

    spring boot连接数据库(jpa)

    在本文中,我们将深入探讨如何使用Spring Boot框架与MySQL数据库进行集成,主要通过Java Persistence API (JPA) 来实现数据的增删改查操作。首先,我们需要理解Spring Boot的核心特性,它简化了传统Spring应用的初始...

    spring boot创建钉钉后端服务核心要点.doc

    通过以上步骤,我们可以创建一个基本的Spring Boot应用,它能处理HTTP请求,与MySQL数据库交互,并且具备与钉钉API集成的基础。随着项目的进展,你可以添加更多的功能,如错误处理、安全性设置、日志记录、单元测试...

    Packt.Spring.5.0.Projects.rar

    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:实施Restful Membership登录API

    在本项目中,"Spring-Boot-Auth" 是一个基于Spring Boot实现的Restful会员登录API,它专注于提供用户注册和登录功能。Spring Boot是Java生态中的一个热门框架,它简化了新Spring应用的初始搭建以及开发过程。下面将...

    spring-boot-mysql

    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 ...

    spring-boot-card-management:使用Spring Boot 2和Mybatis的Cards Management API微服务

    本练习仅侧重于REST API,并使用spring boot和mybatis以及MySQL Database进行构建。 特征 注册/登录/生成JWT令牌 提取特定用户的卡片列表 提取特定卡的卡详细信息 激活卡 停用卡 更改卡的每日限额 数据库 该项目...

Global site tag (gtag.js) - Google Analytics