Springboot将数据存储到数据库当中
1.从前端获取数据,同时存储到use当中
public String login(HttpServletRequest request) {
User user = new User();
user.setPhone(request.getParameter("telephone"));
user.setName(request.getParameter("username"));
user.setPassword(request.getParameter("password"));
user.setEmail(request.getParameter("email"));
implementChat.saveUser(user);
System.out.println("LOGIN success");
return "login";
}
2.定义user类,其中@Column(name = "telephone", length = 50),"telephone代表列的名字
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(name = "telephone", length = 50)
private String telephone;
@Column(name = "username", length = 50)
private String username;
@Column(name = "password", length = 50)
private String password;
@Column(name = "email", length = 50)
private String email;
}
3.service层
@Service
@Transactional
public class UserService {
@Autowired
private ChatRespositroy chatRepository;
public void saveUser(User user) {
chatRepository.save(user);
}
4.repostroy层
@Repository
public interface ChatRespositroy extends CrudRepository<User,Integer> {
User findUserById(int id);
List<User> findUserByTelephone(String phone);
List<User> findUserByUsername(String name);
User findUserByPassword(String password);
User findUserByEmail(String email);
}