springboot后台
<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>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
application.properties配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/mimi-boot
spring.datasource.username=root
spring.datasource.password=123456
数据库新建user表
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT '用户id',
`username` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '用户名',
`password` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '密码',
`nickname` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '昵称',
`address` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '地址',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
用户登录
User对象
@Entity
@Table(name = "user")
@Getter
@Setter
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String nickname;
private String address;
}
登录-持久层
@Repository
public interface UserRepository extends JpaRepository<User,Integer> {
}
登录-业务层
登录-控制层
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
private UserRepository userRepository;
@PostMapping("/login")
public User login(@RequestBody User user){
return userRepository.findByUsernameAndPassword(user.getUsername(), user.getPassword());
}
@GetMapping("/{id}")
public User getUserById(@PathVariable Integer id){
Optional<User> user = userRepository.findById(id);
return user.orElseGet(null);
}
}
小程序页面
npm创建Vant Weapp(官方参考文档) 添加组件
"usingComponents": {
"van-button": "@vant/weapp/button/index",
"van-field": "@vant/weapp/field/index",
"van-icon": "@vant/weapp/icon/index"
}
数据请求request.js
let ajaxTimes = 0;
export const request = (params) => {
ajaxTimes ++;
wx.showLoading({
title: '加载中',
mask: true
})
const baseUrl = 'http://localhost:8080';
return new Promise((resolve, reject) => {
wx.request({
...params,
url: baseUrl + params.url,
success: (result) => {
resolve(result.data);
},
fail: (err) => {
reject(err);
},
complete: () => {
ajaxTimes --;
if(ajaxTimes === 0) {
wx.hideLoading();
}
},
})
})
}
|