23decxf 发表于 2017-12-14 06:30:36

基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD

  完成一个RESTful服务,提供几个访问接口,用来操作较简单的联系人信息,数据保存在Sql Server数据库中。
  1.使用STS创建工程。
  使用STS创建RESTful工程,可以参考:
  《用Spring Tools Suite(STS)开始一个RESTful Web Service》
  《SpringBoot构建RESTful service完成Get和Post》
  新建项目时的Project Name,Group,Atifact,Package这些参数按照实际要求填。
  项目引入依赖web,JPA,SQL Server依赖。

  完成构建后,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.kxh.example</groupId>
  
<artifactId>demo0003</artifactId>
  
<version>0.0.1-SNAPSHOT</version>
  
<packaging>jar</packaging>
  

  
<name>demo0003</name>
  
<description>Demostrate that operate sql server</description>
  

  
<parent>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-parent</artifactId>
  
<version>1.5.6.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-data-jpa</artifactId>
  
</dependency>
  
<dependency>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-web</artifactId>
  
</dependency>
  

  
<dependency>
  
<groupId>com.microsoft.sqlserver</groupId>
  
<artifactId>mssql-jdbc</artifactId>
  
<scope>runtime</scope>
  
</dependency>
  
<dependency>
  
<groupId>org.springframework.boot</groupId>
  
<artifactId>spring-boot-starter-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>
  

  2.构建数据库表。
  在SQL Sever中的目标数据库中建一个contact表。
  表结构:
  id是主键,并且设置其为自增标识,可以自动生成递增的值作为id。

  3.创建包结构
  根包为:com.kxh.example.demo

  4.创建领域对象“联系人”实体。
  在包“com.kxh.example.demo.domain”下创建类“Contact”,并且加上Entity注解。
  为id属性增加Id注解表示这是个主键。
  为id属性增加GeneratedValue,并设置id值生成的策略是自增。
  

package com.kxh.example.demo.domain;  

  

import javax.persistence.Entity;  

import javax.persistence.GeneratedValue;  

import javax.persistence.GenerationType;  

import javax.persistence.Id;  

  
@Entity
  

public>
@Id  
@GeneratedValue(strategy
= GenerationType.IDENTITY)  

private long>
  

private String name;  

  

private String phone;  

  

private String mail;  

  

public Contact() {  

super();  
}
  

  

public Contact(String name, String phone, String mail) {  

super();  

  

this.name = name;  

this.phone = phone;  

this.mail = mail;  
}
  

  

public long getId() {  

return this.id;  
}
  

  

public void setId(long value) {  

this.id = value;  
}
  

  

public String getName() {  

return this.name;  
}
  

  

public void setName(String value) {  

this.name = value;  
}
  

  

public String getPhone() {  

return phone;  
}
  

  

public void setPhone(String value) {  

this.phone = value;  
}
  

  

public String getMail() {  

return this.mail;  
}
  

  

public void setMail(String value) {  

this.mail = value;  
}
  
}
  

  5.创建数据操作对象。
  在包“com.kxh.example.demo.dao”下创建接口“ContactsRepository”,从“JpaRepository”继承,可以完成最常用的CRUD。
  该接口定义了根据name查一个联系人。也定义了可以通过对name的模糊查询联系人,findBy的写法可以参考官方文档以完成更多的查询。
  

package com.kxh.example.demo.dao;  

  

import java.util.List;  

import org.springframework.data.jpa.repository.JpaRepository;  

  

import com.kxh.example.demo.domain.Contact;  

  

public interface ContactsRepository extends JpaRepository<Contact, Long> {  
Contact findByName(String name);
  

  
List
<Contact> findByNameLike(String name);  
}
  

  6.创建RestController向外提供服务
  在包“com.kxh.example.demo.controller”先创建类“ContactsController”。
  写上RestController注解,进行根url路径的映射。
  将ContactsRepository定义为类成员变量,并打上Autowired注解,由框架自动进行实例化。
  

package com.kxh.example.demo.controller;  

  

import java.util.ArrayList;  

import java.util.List;  

  

import org.springframework.beans.factory.annotation.Autowired;  

import org.springframework.web.bind.annotation.RequestBody;  

import org.springframework.web.bind.annotation.RequestMapping;  

import org.springframework.web.bind.annotation.RequestMethod;  

import org.springframework.web.bind.annotation.RestController;  

  

import com.kxh.example.demo.dao.ContactsRepository;  

import com.kxh.example.demo.domain.Contact;  

import com.kxh.example.demo.service.ContactsService;  

  
@RestController
  
@RequestMapping(
"/contacts")  

public>
  
@Autowired
  
ContactsRepository contactsRepository;
  

  

//新增  
@RequestMapping(value="/save/new", method=RequestMethod.POST)
  
public Contact saveNewContact(@RequestBody Contact contact) {return this.contactsRepository.save(contact);
  
}
  

  
//更新
  
@RequestMapping(value="/save/updated", method=RequestMethod.PUT)
  
public Contact saveUpdatedContact(@RequestBody Contact contact) {
  
Contact contactExisted = this.contactsRepository.findByName(contact.getName());
  
contactExisted.setPhone(contact.getPhone());
  
contactExisted.setMail(contact.getMail());
  
return this.contactsRepository.save(contactExisted);
  
}
  

  
//删除
  
@RequestMapping(value="/remove", method=RequestMethod.DELETE)

  
public void removeContact(long>  
this.contactsRepository.delete(id);
  
}
  

  
//查询,通过name查询一条
  
@RequestMapping(value="/query/byname", method=RequestMethod.GET)
  
public Contact findContactByName(String name) {
  
Contact contact = this.contactsRepository.findByName(name);
  
return contact;
  
}
  

  
//查询,通过like name查询多条
  
@RequestMapping(value="/query/likename", method=RequestMethod.GET)
  
public List<Contact> findContactLikeName(String name) {
  
String nameWhere = org.apache.commons.lang.StringUtils.join(new String[]{"%", name, "%"}, "");
  
List<Contact> contacts = this.contactsRepository.findByNameLike(nameWhere);
  
return contacts;
  
}
  
}
  

  7.完成,运行。
  这里使用curl进行调试。
  新建
  执行curl的命令:
  

curl -i -H "Content-Type:application/json;charset:utf-8" -X POST-d "{\"name\":\"Aissen\",\"phone\":\"13237123112\",\"mail\":\"aissen@ss.com\"}" http://localhost:8080/contacts/save/new  

  执行后数据表中会增加一条记录。

  更新
  执行命令,修改联系人电话和邮箱。
  

curl -i -H "Content-Type:application/json" -X PUT-d "{\"name\":\"Aissen\",\"phone\":\"13231444444\",\"mail\":\"Aissen111@cc.com\"}" http://localhost:8080/contacts/save/updated  

  执行后,记录被修改。

  查询单个
  在浏览器中访问查询rul。
  

http://localhost:8080/contacts/query/byname?name=Tom  

  结果:

  查询多个
  在浏览器中访问查询rul。
  

http://localhost:8080/contacts/query/likename?name=to  

  结果:

  删除
  执行命令,参数id设为需要删除的联系人的id。
  

curl -i -X DELETE http://localhost:8080/contacts/remove?id=6  

  代码
  End
页: [1]
查看完整版本: 基于Spring Boot,使用JPA操作Sql Server数据库完成CRUD