本文介紹了Spring Boot實(shí)戰(zhàn)之Rest接口開(kāi)發(fā)及數(shù)據(jù)庫(kù)基本操作,分享給大家
在鳩江等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站建設(shè)、網(wǎng)站制作 網(wǎng)站設(shè)計(jì)制作按需網(wǎng)站建設(shè),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),高端網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷(xiāo)推廣,成都外貿(mào)網(wǎng)站建設(shè)公司,鳩江網(wǎng)站建設(shè)費(fèi)用合理。
1、修改pom.xml,添加依賴(lài)庫(kù),本文使用的是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,配置數(shù)據(jù)源及java持久層API相關(guān)信息
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 # 配置數(shù)據(jù)庫(kù) spring.jpa.database = MYSQL # 查詢(xún)時(shí)是否顯示日志 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、添加數(shù)據(jù)模型 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、添加數(shù)據(jù)訪問(wèn)接口類(lèi) 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、封裝返回的結(jié)果
添加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; } }
添加枚舉類(lèi)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、工程整體結(jié)構(gòu)
8、運(yùn)行測(cè)試,本文中測(cè)試使用的是user表,其中包含一些密碼等信息未做處理,這個(gè)讀者自行進(jìn)行JsonIgnore處理
提供以下5個(gè)接口
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
測(cè)試運(yùn)行結(jié)果
adduser接口
updateuser接口
getuser接口
9、調(diào)用以上接口時(shí)執(zhí)行數(shù)據(jù)庫(kù)操作時(shí),會(huì)在內(nèi)部轉(zhuǎn)化為以下SQL語(yǔ)句
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、數(shù)據(jù)庫(kù)操作
JPA模塊支持將查詢(xún)字符串定義在方法名稱(chēng)中
如上例中
根據(jù)id值查詢(xún)UserInfo實(shí)例
UserInfo findUserInfoById(int id);
根據(jù)role查詢(xún)UserInfo實(shí)例
List<UserInfo> findUserInfoByRole(String role);
也可以直接使用原生的數(shù)據(jù)庫(kù)語(yǔ)句
如下使用@Query注解
@Query(value = "select * from t_user limit ?1", nativeQuery =true) List<UserInfo> findAllUsersByCount(int count);
11、在方法名中添加查詢(xún)字符串參考
本文源碼下載:http://xiazai.jb51.net/201707/yuanma/SpringRest_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
本文題目:詳解SpringBoot實(shí)戰(zhàn)之Rest接口開(kāi)發(fā)及數(shù)據(jù)庫(kù)基本操作
地址分享:http://vcdvsql.cn/article34/iihdpe.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、軟件開(kāi)發(fā)、網(wǎng)站建設(shè)、網(wǎng)站內(nèi)鏈、小程序開(kāi)發(fā)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)