结构如下:
在entity定义实体类:
User包含自增的Integer类型的id,String类型的userName,String类型的password。
@Entity
public class User {
@Id //声明此属性为主键
private Integer id;
private String userName;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}
public void setId(Integer id) {
this.id = id;
}
public Integer getId() {
return id;
}
}
在Dao层定义对应的数据库操作接口:
@Repository:
@Repository是属于Spring的注解。它用来标注访问层的类(Dao层),它表示一个仓库,主要用于封装对于数据库的访问。其实现方式与@Component注解相同,只是为了明确类的作用而设立。
即@Repository是@Component注解的一个派生品,与@Service和@Controller都可以理解为@Component注解的扩展。他们的作用都是在类上实例化bean,并把当前类对象的实现类交给spring容器进行管理。
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public List<User> add(List<User> users){
String sql="insert into user(user_name,password) value (?,?)";
try{
for (User user:users)
jdbcTemplate.update(sql,user.getUserName(),user.getPassword());
return users;
}catch (DataAccessException e){
e.printStackTrace();
return null;
}
}
//delete<list>
public String deleteUsers(Integer[] ids){
String sql="delete from user where id=?";
try{
for(int i:ids){
jdbcTemplate.update(sql,i);
}
return "ok";
}catch (DataAccessException e){
e.printStackTrace();
return "wrong";
}
}
//update
public String update(List<User> users){
String sql="update user set user_name=?,password=? where id=?";
try{
for (User user:users)
jdbcTemplate.update(sql,user.getUserName(),user.getPassword(),user.getId());
return "ok";
}catch (DataAccessException e){
e.printStackTrace();
return "wrong";
}
}
}
在Service层实现业务逻辑,并调用Dao层的操作:
@ service用于标记当前类是service类,加上该注解会自动将当前类注入到spring容器中,不需要再在applicationContext.xml定义bean去实例了,@Autowired注入dao。直接调用dao中方法。
@Service
public class UserService {
@Autowired
private UserDao userDao;
public List<User> add(List<User> users){
return userDao.add(users);
}
public String deleteUsers(Integer[] ids){
return userDao.deleteUsers(ids);
}
public String update(List<User> users){
return userDao.update(users);
}
}
在Controller层封装请求处理逻辑,并调用Service层的操作:
@RestController 注解包含了 @Controller 和 @ResponseBody注解。其中,@Controller 注解用来声明一个类为控制器,@ResponseBody 注解用来告诉 Spring Boot,返回值需要转换为 JSON 或 XML 格式。
@RestController
public class UserController {
@Autowired
private UserService userService;
//insert
@PostMapping("/add")
public List<User> add(@RequestBody List<User> users){
return userService.add(users);
}
//delete
@DeleteMapping("/delete")
public String deleteUsers(@RequestParam("id") Integer[] ids){
return userService.deleteUsers(ids);
}
@PostMapping("/update")
public String update(@RequestBody List<User> users){
return userService.update(users);
}
}
在配置文件连接数据库:
以端口为8080,url为jdbc:mysql://localhost:3309/javaee?serverTimezone=UTC&useSSL=false为例
server.port=8080
spring.datasource.url= jdbc:mysql://localhost:3309/javaee?serverTimezone=UTC&useSSL=false
spring.datasource.username=填入对应username
spring.datasource.password=填入对应password
spring.datasource.driver-class-name=com.mysql.jdbc.Driver