编写一个springboot项目,连接数据库study,实现登录注册功能、数据查询功能
时间: 2023-06-23 13:10:21 浏览: 174
好的,以下是一个简单的示例项目:
1. 首先,在pom.xml中添加以下依赖:
```xml
<dependencies>
<!--Spring Boot Web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--Spring Boot JDBC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!--MySQL JDBC-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
```
2. 创建一个数据表user,用于存储用户数据:
```mysql
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
```
3. 配置数据库连接信息,打开application.properties文件,添加以下内容:
```
# 数据库连接信息
spring.datasource.url=jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
```
4. 创建User实体类,用于封装用户数据:
```java
public class User {
private Integer id;
private String username;
private String password;
private String email;
// 省略getter和setter方法
}
```
5. 创建UserDao接口,定义增删改查方法:
```java
public interface UserDao {
int insert(User user);
int deleteById(Integer id);
int update(User user);
User selectById(Integer id);
List<User> selectAll();
}
```
6. 创建UserDaoImpl类,实现UserDao接口:
```java
@Repository
public class UserDaoImpl implements UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public int insert(User user) {
String sql = "insert into user(username, password, email) values(?, ?, ?)";
return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail());
}
@Override
public int deleteById(Integer id) {
String sql = "delete from user where id = ?";
return jdbcTemplate.update(sql, id);
}
@Override
public int update(User user) {
String sql = "update user set username = ?, password = ?, email = ? where id = ?";
return jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getId());
}
@Override
public User selectById(Integer id) {
String sql = "select * from user where id = ?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new BeanPropertyRowMapper<>(User.class));
}
@Override
public List<User> selectAll() {
String sql = "select * from user";
return jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(User.class));
}
}
```
7. 创建UserController类,实现登录注册和数据查询功能:
```java
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserDao userDao;
/**
* 注册用户
*
* @param user 用户信息
* @return 注册结果
*/
@PostMapping("/register")
public String register(User user) {
if (StringUtils.isEmpty(user.getUsername()) || StringUtils.isEmpty(user.getPassword()) || StringUtils.isEmpty(user.getEmail())) {
return "用户名、密码、邮箱不能为空";
}
User dbUser = userDao.selectById(user.getId());
if (dbUser != null) {
return "用户已存在";
}
userDao.insert(user);
return "注册成功";
}
/**
* 用户登录
*
* @param username 用户名
* @param password 密码
* @return 登录结果
*/
@PostMapping("/login")
public String login(String username, String password) {
if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
return "用户名、密码不能为空";
}
List<User> userList = userDao.selectAll();
for (User user : userList) {
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
return "登录成功";
}
}
return "用户名或密码错误";
}
/**
* 查询用户信息
*
* @param id 用户ID
* @return 用户信息
*/
@GetMapping("/{id}")
public User selectById(@PathVariable Integer id) {
return userDao.selectById(id);
}
/**
* 查询所有用户信息
*
* @return 所有用户信息
*/
@GetMapping("/all")
public List<User> selectAll() {
return userDao.selectAll();
}
}
```
以上就是一个简单的springboot项目,实现了登录注册和数据查询功能。
阅读全文