Spring boot的简单rest服务(非xml方式配置)

背景

简单使用spring boot配置一个rest服务,数据库主要使用mysql没有使用spring演示时候使用的h2(一种内存数据库)和lombok(经过注解的方式生成getter和setter方法,由于要在IDEA上面安装插件,就放弃了)。html

项目结构

spring boot项目结构

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.example</groupId>
	<artifactId>zyldemo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>zyldemo</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

		<!-- Use MySQL Connector-J -->

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>

<!--		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.security</groupId>
			<artifactId>spring-security-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

这里暂时不考虑spring-security。java

/resources/application.properties

server.port=8080
server.servlet.contextPath=/zyldemo
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://localhost:3306/zyldb?useSSL=true
spring.datasource.username=root
spring.datasource.password=zyl

这个部分主要是在配置mysql数据库,值得注意的是这里添加了useSSL=true设置,启用SSL验证。mysql

**Note:**这里spring.jpa.hibernate.ddl-auto=update使用update,不使用create。create致使每次启动spring boot都会清空数据库数据。web

model层

Customer.java

package com.example.zyldemo.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Customer {
    @Id
    @GeneratedValue
    private Long id;

    private String name;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这里没有使用lombok,因此看起来这个bean类可能会有点长,不过如今ide工具基本上都可以自动生成setter和getter方法。若是不喜欢看这么长类的同窗能够考虑使用使用。spring

dao层

好,咱们如今来看看持久层。sql

CustomerRepository.java

package com.example.zyldemo.dao;

import com.example.zyldemo.model.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

最开始接触这个dao层的时候,要本身手动一个接口一个实现的写dao,如今的spring boot已经帮咱们定义和实现了,人生真是幸福。数据库

service层

如今,来看一看服务层,首先,咱们先定义一下服务层的接口。apache

ICustomerService.java

package com.example.zyldemo.service;

import com.example.zyldemo.model.Customer;

public interface ICustomerService {

    public boolean add(Customer customer);
}

这里主要定义一个对象保存到mysql数据库中去。而后,咱们再来看一看这个在服务层怎么实现。springboot

CustomerService.java

package com.example.zyldemo.service;

import com.example.zyldemo.dao.CustomerRepository;
import com.example.zyldemo.model.Customer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CustomerService implements ICustomerService {

    @Autowired
    private CustomerRepository customerRepository;

    @Override
    public boolean add(Customer customer) {
        customerRepository.save(customer);
        return true;
    }
}

继承以前的接口ICustomerService,而后,注入dao层,再实现接口便可。app

Controller层

重点的控制层来了。

CustomerController.java

package com.example.zyldemo.controller;

import com.example.zyldemo.model.Customer;
import com.example.zyldemo.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/customer")
public class CustomerController {
    @Autowired
    private ICustomerService customerService;

    @GetMapping(path = "/add")
    public String add(@RequestParam String name){
        Customer customer = new Customer();
        customer.setName(name);
        customerService.add(customer);
        return "Saved";
    }
}

这里主要是注入服务层,而后,利用服务层实例,将get请求过来的数据保存到数据库中。

尝试一波

发送get请求 再看mysql数据库里面的状况:

参考

相关文章
相关标签/搜索