您好,登录后才能下订单哦!
从事一段时间Android的我,通常都是调用别人的接口来进行相关的开发,但是一直以来对于接口问题和后端开发的同学争吵不休,无法确定责任到底在谁,最终在这种情况下,我重新回到自己之前的Java开发,经过了两个小时的努力,完成了一个以springboot 为框架,其中集成了jpa,mysql的方式来完成。废话不多说了,直接上代码!
其中项目的依赖文件为 :
<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>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将返回数据自动格式化为json文件
br/>@RestController将返回数据自动格式化为json文件
@RequestParam 必传参数,
其他为非必传参数
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。