從事一段時(shí)間Android的我,通常都是調(diào)用別人的接口來(lái)進(jìn)行相關(guān)的開(kāi)發(fā),但是一直以來(lái)對(duì)于接口問(wèn)題和后端開(kāi)發(fā)的同學(xué)爭(zhēng)吵不休,無(wú)法確定責(zé)任到底在誰(shuí),最終在這種情況下,我重新回到自己之前的Java開(kāi)發(fā),經(jīng)過(guò)了兩個(gè)小時(shí)的努力,完成了一個(gè)以springboot 為框架,其中集成了jpa,mysql的方式來(lái)完成。廢話(huà)不多說(shuō)了,直接上代碼!
其中項(xiàng)目的依賴(lài)文件為 :
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
org.springframework.boot
spring-boot-starter-test
test
,
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
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 = "用戶(hù)已存在,可以直接登錄";
} 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 = "注冊(cè)失敗";
} else {
result.code = 200;
result.data = user;
result.msg = "注冊(cè)成功";
}
}
return result;
}
}
對(duì)于Controller中的各類(lèi)注解解釋如下:@RestController將返回?cái)?shù)據(jù)自動(dòng)格式化為json文件
br/>@RestController將返回?cái)?shù)據(jù)自動(dòng)格式化為json文件
@RequestParam 必傳參數(shù),
其他為非必傳參數(shù)
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線(xiàn),公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性?xún)r(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專(zhuān)為企業(yè)上云打造定制,能夠滿(mǎn)足用戶(hù)豐富、多元化的應(yīng)用場(chǎng)景需求。