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數據庫裏面的狀況:

參考

相關文章
相關標籤/搜索