從事一段時間Android的我,通常都是調(diào)用別人的接口來進行相關(guān)的開發(fā),但是一直以來對于接口問題和后端開發(fā)的同學爭吵不休,無法確定責任到底在誰,最終在這種情況下,我重新回到自己之前的Java開發(fā),經(jīng)過了兩個小時的努力,完成了一個以springboot 為框架,其中集成了jpa,MySQL的方式來完成。廢話不多說了,直接上代碼!
其中項目的依賴文件為 :
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
成都創(chuàng)新互聯(lián)是專業(yè)的迎澤網(wǎng)站建設(shè)公司,迎澤接單;提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作,網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行迎澤網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
,
application.properties 的配置文件為:
spring.datasource.url=jdbc:mysql://localhost:3306/zhiteng?characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#配置JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
bean 文件為:
@Entity
public class User {@Column
br/>@Column
br/>@GeneratedValue()
br/>@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Email
public String Email;@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Column
br/>@Column
}
JPA :
public interface UserJpa extends JpaRepository<User, String> {
long countUserByPhoneNum(String phoneNum);
}
Controller:@RestController
br/>@RestController
br/>@Autowired
@RequestMapping(value = register)
public Result register(@RequestParam("userName") String userName, @RequestParam("password") String password, String address, int age, String name,
String userAccount, String Email, @RequestParam("userPhone") String userPhone, String nikeName) {
Result result = new Result();
long tag = userJpa.countUserByPhoneNum(userPhone);
if (tag > 0) {
result.code = 201;
result.msg = "用戶已存在,可以直接登錄";
} else {
User user = new User();
user.userID = SerialUtils.getUserId();
user.name = name;
if (StringUtils.isEmpty(userAccount)) {
user.userAccount = userName;
} else {
user.userAccount = userAccount;
}
user.phoneNum = userPhone;
user.password = MyMD5Util.getMD5(password);
if (StringUtils.isEmpty(nikeName)) {
user.nikeName = "zhiteng" + userPhone.substring(userPhone.length() - 4);
} else {
user.nikeName = nikeName;
}
user.userName = userName;
if (StringUtils.isEmpty(address)) {
user.address = "陜西,渭南";
} else {
user.address = address;
}
user.age = age;
user.Email = Email;
user.userType = UserType.common;
user.userRule = UserRule.user;
User u = userJpa.save(user);
if (null == u) {
result.code = 202;
result.data = user;
result.msg = "注冊失敗";
} else {
result.code = 200;
result.data = user;
result.msg = "注冊成功";
}
}
return result;
}
}
對于Controller中的各類注解解釋如下:@RestController將返回數(shù)據(jù)自動格式化為json文件
br/>@RestController將返回數(shù)據(jù)自動格式化為json文件
@RequestParam 必傳參數(shù),
其他為非必傳參數(shù)
當前文章:Springboot集成JPA完成接口demo
文章來源:http://jinyejixie.com/article32/poedpc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、外貿(mào)建站、靜態(tài)網(wǎng)站、微信小程序、App設(shè)計、自適應(yīng)網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)