用户管理功能spring jdbc
时间: 2023-06-25 19:07:23 浏览: 163
Spring的连接数据库以及JDBC模板(实例讲解)
用户管理功能可以使用Spring JDBC来实现。下面是一个简单的示例:
首先,需要配置数据源和JdbcTemplate:
```java
@Configuration
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydatabase");
dataSource.setUsername("root");
dataSource.setPassword("password");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
}
```
然后,需要创建一个User类和UserMapper类:
```java
public class User {
private Long id;
private String username;
private String password;
private String email;
// getters and setters
}
public class UserMapper implements RowMapper<User> {
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setId(rs.getLong("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
return user;
}
}
```
接下来,可以创建一个UserService类来实现用户管理功能:
```java
@Service
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
public void addUser(User user) {
String sql = "INSERT INTO users (username, password, email) VALUES (?, ?, ?)";
jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail());
}
public void updateUser(User user) {
String sql = "UPDATE users SET username=?, password=?, email=? WHERE id=?";
jdbcTemplate.update(sql, user.getUsername(), user.getPassword(), user.getEmail(), user.getId());
}
public void deleteUser(Long id) {
String sql = "DELETE FROM users WHERE id=?";
jdbcTemplate.update(sql, id);
}
public User getUserById(Long id) {
String sql = "SELECT * FROM users WHERE id=?";
return jdbcTemplate.queryForObject(sql, new Object[]{id}, new UserMapper());
}
public List<User> getAllUsers() {
String sql = "SELECT * FROM users";
return jdbcTemplate.query(sql, new UserMapper());
}
}
```
最后,可以在Controller中调用UserService来实现具体的用户管理功能:
```java
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService;
@PostMapping
public ResponseEntity<Void> addUser(@RequestBody User user) {
userService.addUser(user);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
@PutMapping("/{id}")
public ResponseEntity<Void> updateUser(@PathVariable Long id, @RequestBody User user) {
user.setId(id);
userService.updateUser(user);
return ResponseEntity.ok().build();
}
@DeleteMapping("/{id}")
public ResponseEntity<Void> deleteUser(@PathVariable Long id) {
userService.deleteUser(id);
return ResponseEntity.ok().build();
}
@GetMapping("/{id}")
public ResponseEntity<User> getUserById(@PathVariable Long id) {
User user = userService.getUserById(id);
return ResponseEntity.ok(user);
}
@GetMapping
public ResponseEntity<List<User>> getAllUsers() {
List<User> users = userService.getAllUsers();
return ResponseEntity.ok(users);
}
}
```
阅读全文