欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring Boot实战之Rest接口开发及数据库基本操作

程序员文章站 2024-02-06 10:40:34
本文介绍了spring boot实战之rest接口开发及数据库基本操作,分享给大家 1、修改pom.xml,添加依赖库,本文使用的是mysql

本文介绍了spring boot实战之rest接口开发及数据库基本操作,分享给大家

1、修改pom.xml,添加依赖库,本文使用的是mysql

 <dependency> 
<groupid>org.springframework.boot</groupid> 
<artifactid>spring-boot-starter-data-jpa</artifactid> 
</dependency> 
<dependency> 
<groupid>mysql</groupid> 
<artifactid>mysql-connector-java</artifactid> 
</dependency> 

2、修改配置文件application.properties,配置数据源及java持久层api相关信息

spring.datasource.url = jdbc:mysql://127.0.0.1:3306/springlearn 
spring.datasource.username = root 
spring.datasource.password = root 
spring.datasource.driverclassname = com.mysql.jdbc.driver 
 
 
# 配置数据库 
spring.jpa.database = mysql 
# 查询时是否显示日志 
spring.jpa.show-sql = true 
# hibernate ddl auto (create, create-drop, update) 
spring.jpa.hibernate.ddl-auto = update 
# naming strategy 
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.improvednamingstrategy 
# stripped before adding them to the entity manager) 
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.mysql5dialect 

3、添加数据模型 userinfo.java

package com.xiaofangtech.sunt.bean; 
 
import javax.persistence.entity; 
import javax.persistence.generatedvalue; 
import javax.persistence.generationtype; 
import javax.persistence.id; 
import javax.persistence.table; 
import javax.validation.constraints.notnull; 
 
@entity 
@table(name="t_user") 
public class userinfo { 
  @id 
  @generatedvalue(strategy = generationtype.auto) 
  private int id; 
  @notnull 
  private string name; 
   
  private string password; 
   
  private string salt; 
   
  private string role; 
 
  public int getid() { 
    return id; 
  } 
 
  public void setid(int id) { 
    this.id = id; 
  } 
 
  public string getname() { 
    return name; 
  } 
 
  public void setname(string name) { 
    this.name = name; 
  } 
 
  public string getpassword() { 
    return password; 
  } 
 
  public void setpassword(string password) { 
    this.password = password; 
  } 
 
  public string getsalt() { 
    return salt; 
  } 
 
  public void setsalt(string salt) { 
    this.salt = salt; 
  } 
 
  public string getrole() { 
    return role; 
  } 
 
  public void setrole(string role) { 
    this.role = role; 
  } 
} 

4、添加数据访问接口类 userinforepository.java

package com.xiaofangtech.sunt.repository; 
 
import java.util.list; 
 
import org.springframework.data.jpa.repository.query; 
import org.springframework.data.repository.crudrepository; 
 
import com.xiaofangtech.sunt.bean.userinfo; 
 
public interface userinforepository extends crudrepository<userinfo, integer>{ 
  userinfo finduserinfobyid(int id); 
  list<userinfo> finduserinfobyrole(string role); 
   
  @query(value = "select * from t_user limit ?1", nativequery =true) 
  list<userinfo> findallusersbycount(int count); 
} 

5、添加usercontroller.java,添加用户信息的增删改查操作

package com.xiaofangtech.sunt.controller; 
 
import java.util.list; 
 
import org.springframework.beans.factory.annotation.autowired; 
import org.springframework.data.jpa.repository.modifying; 
import org.springframework.web.bind.annotation.requestbody; 
import org.springframework.web.bind.annotation.requestmapping; 
import org.springframework.web.bind.annotation.restcontroller; 
 
import com.xiaofangtech.sunt.bean.userinfo; 
import com.xiaofangtech.sunt.repository.userinforepository; 
import com.xiaofangtech.sunt.utils.resultmsg; 
import com.xiaofangtech.sunt.utils.resultstatuscode; 
 
@restcontroller 
@requestmapping("user") 
public class usercontroller { 
  @autowired 
  private userinforepository userrepositoy; 
   
  @requestmapping("getuser") 
  public object getuser(int id) 
  { 
    userinfo userentity = userrepositoy.finduserinfobyid(id); 
    resultmsg resultmsg = new resultmsg(resultstatuscode.ok.geterrcode(), resultstatuscode.ok.geterrmsg(), userentity); 
    return resultmsg; 
  } 
   
  @requestmapping("getusers") 
  public object getusers(string role) 
  { 
    list<userinfo> userentities = userrepositoy.finduserinfobyrole(role); 
    resultmsg resultmsg = new resultmsg(resultstatuscode.ok.geterrcode(), resultstatuscode.ok.geterrmsg(), userentities); 
    return resultmsg; 
  } 
   
  @modifying 
  @requestmapping("adduser") 
  public object adduser(@requestbody userinfo userentity) 
  { 
    userrepositoy.save(userentity); 
    resultmsg resultmsg = new resultmsg(resultstatuscode.ok.geterrcode(), resultstatuscode.ok.geterrmsg(), userentity); 
    return resultmsg; 
  } 
   
  @modifying 
  @requestmapping("updateuser") 
  public object updateuser(@requestbody userinfo userentity) 
  { 
    userinfo user = userrepositoy.finduserinfobyid(userentity.getid()); 
    if (user != null) 
    { 
      user.setname(userentity.getname()); 
      userrepositoy.save(user); 
    } 
    resultmsg resultmsg = new resultmsg(resultstatuscode.ok.geterrcode(), resultstatuscode.ok.geterrmsg(), null); 
    return resultmsg; 
  } 
   
  @modifying 
  @requestmapping("deleteuser") 
  public object deleteuser(int id) 
  { 
    userrepositoy.delete(id); 
    resultmsg resultmsg = new resultmsg(resultstatuscode.ok.geterrcode(), resultstatuscode.ok.geterrmsg(), null); 
    return resultmsg; 
  } 
} 

6、封装返回的结果

添加resultmsg.java

package com.xiaofangtech.sunt.utils; 
 
public class resultmsg { 
  private int errcode; 
  private string errmsg; 
  private object p2pdata; 
   
  public resultmsg(int errcode, string errmsg, object p2pdata) 
  { 
    this.errcode = errcode; 
    this.errmsg = errmsg; 
    this.p2pdata = p2pdata; 
  } 
  public int geterrcode() { 
    return errcode; 
  } 
  public void seterrcode(int errcode) { 
    this.errcode = errcode; 
  } 
  public string geterrmsg() { 
    return errmsg; 
  } 
  public void seterrmsg(string errmsg) { 
    this.errmsg = errmsg; 
  } 
  public object getp2pdata() { 
    return p2pdata; 
  } 
  public void setp2pdata(object p2pdata) { 
    this.p2pdata = p2pdata; 
  } 
} 

添加枚举类resultstatuscode.java

package com.xiaofangtech.sunt.utils; 
 
public enum resultstatuscode { 
  ok(0, "ok"), 
  system_err(30001, "system error"); 
   
  private int errcode; 
  private string errmsg; 
  public int geterrcode() { 
    return errcode; 
  } 
 
  public void seterrcode(int errcode) { 
    this.errcode = errcode; 
  } 
 
  public string geterrmsg() { 
    return errmsg; 
  } 
 
  public void seterrmsg(string errmsg) { 
    this.errmsg = errmsg; 
  } 
  private resultstatuscode(int errode, string errmsg) 
  { 
    this.errcode = errode; 
    this.errmsg = errmsg; 
  } 
} 

7、工程整体结构

详解Spring Boot实战之Rest接口开发及数据库基本操作

8、运行测试,本文中测试使用的是user表,其中包含一些密码等信息未做处理,这个读者自行进行jsonignore处理

提供以下5个接口

http://localhost:8080/user/adduser

http://localhost:8080/user/updateuser

http://localhost:8080/user/getuser?id=13

http://localhost:8080/user/getusers?role=manager

http://localhost:8080/user/deleteuser?id=13

测试运行结果

adduser接口

详解Spring Boot实战之Rest接口开发及数据库基本操作

详解Spring Boot实战之Rest接口开发及数据库基本操作

updateuser接口

详解Spring Boot实战之Rest接口开发及数据库基本操作

详解Spring Boot实战之Rest接口开发及数据库基本操作

getuser接口

详解Spring Boot实战之Rest接口开发及数据库基本操作

详解Spring Boot实战之Rest接口开发及数据库基本操作

9、调用以上接口时执行数据库操作时,会在内部转化为以下sql语句

hibernate: insert into t_user (name, password, role, salt) values (?, ?, ?, ?)


hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.id=?
hibernate: update t_user set name=?, password=?, role=?, salt=? where id=?


hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.id=?


hibernate: select userinfo0_.id as id1_0_, userinfo0_.name as name2_0_, userinfo0_.password as password3_0_, userinfo0_.role as role4_0_, userinfo0_.salt as salt5_0_ from t_user userinfo0_ where userinfo0_.role=?


hibernate: select userinfo0_.id as id1_0_0_, userinfo0_.name as name2_0_0_, userinfo0_.password as password3_0_0_, userinfo0_.role as role4_0_0_, userinfo0_.salt as salt5_0_0_ from t_user userinfo0_ where userinfo0_.id=?
hibernate: delete from t_user where id=?


10、数据库操作

jpa模块支持将查询字符串定义在方法名称中

如上例中

根据id值查询userinfo实例

userinfo finduserinfobyid(int id);

根据role查询userinfo实例

list<userinfo> finduserinfobyrole(string role);

也可以直接使用原生的数据库语句

如下使用@query注解

@query(value = "select * from t_user limit ?1", nativequery =true)
list<userinfo> findallusersbycount(int count);

11、在方法名中添加查询字符串参考

详解Spring Boot实战之Rest接口开发及数据库基本操作

本文源码下载:http://xiazai.jb51.net/201707/yuanma/springrest_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。