目录
完整项目下载地址:
创建项目:https://start.spring.io/
eclipse中import编译后的文件夹
导入静态文件(css,js,img,bootstrap)和页面(index.html):https://pan.baidu.com/s/1UmKwjfMLCqpL4L4dZ_KhTg
提取码:k2rw
创建如下几个包:
将application.properties编码格式设为utf-8:
将index.html的顶部的换成:
总的目录结构:
UserController.java:
package com.sikiedu.userdemo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.sikiedu.userdemo.domain.User;
import com.sikiedu.userdemo.service.UserService;
@RestController
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/")
public ModelAndView index(){
return new ModelAndView("/index.html");
}
//注册
@RequestMapping("/register.action")
public ModelAndView register(User user){
userService.save(user);
return new ModelAndView("redirect:/");
}
//登录
@RequestMapping("/login.action")
public ModelAndView login(User user){
User userlogin= userService.findByUsernameAndPassword(user.getUsername(),user.getPassword());
if(userlogin==null){
return new ModelAndView("redirect:/");
}else{
return new ModelAndView("/welcome.html");
}
}
@RequestMapping("/show.action")
public ModelAndView show(Model model){
List userList= userService.findAll();
model.addAttribute("userList", userList);
return new ModelAndView("/show-all-user.html", "userModel", model);
}
@RequestMapping("/toEdit/{id}")
public ModelAndView toEdit(@PathVariable Integer id,Model model){
User user= userService.findById(id);
model.addAttribute("user", user);
return new ModelAndView("/edit-user.html","userModel",model);
}
@RequestMapping("/edit.action")
public ModelAndView edit(User user){
userService.save(user);
return new ModelAndView("redirect:/show.action");
}
}
User.java:
package com.sikiedu.userdemo.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
private String telephone;
public User() {
super();
}
public User(Integer id, String username, String password, String telephone) {
super();
this.id = id;
this.username = username;
this.password = password;
this.telephone = telephone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
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;
}
public String getTelephone() {
return telephone;
}
public void setTelephone(String telephone) {
this.telephone = telephone;
}
}
UserRepository.java:
package com.sikiedu.userdemo.repository;
import org.springframework.data.repository.CrudRepository;
import com.sikiedu.userdemo.domain.User;
public interface UserRepository extends CrudRepository {
User findByUsernameAndPassword(String username, String password);
}
UserService.java:
package com.sikiedu.userdemo.service;
import java.util.List;
import com.sikiedu.userdemo.domain.User;
public interface UserService {
void save(User user);
User findByUsernameAndPassword(String username, String password);
List findAll();
User findById(Integer id);
}
UserServicelmpl.java:
package com.sikiedu.userdemo.service;
import java.util.List;
import org.springframework.stereotype.Service;
import com.sikiedu.userdemo.domain.User;
import com.sikiedu.userdemo.repository.UserRepository;
@Service
public class UserServicelmpl implements UserService {
private UserRepository userRepository;
public void save(User user) {
userRepository.save(user);
}
@Override
public User findByUsernameAndPassword(String username, String password) {
return userRepository.findByUsernameAndPassword(username,password);
}
@Override
public List findAll() {
return (List) userRepository.findAll();
}
@Override
public User findById(Integer id) {
return userRepository.findById(id).get();
}
}
edit-user.html:
Insert title here
welcome.html:
Insert title here
welcome~~~
show-all-user.html:
Insert title here
用户名
密码
电话
编辑
asd
ad
adsf
修改
index.html:
登陆
注册
会员登录
还没有账号
立即注册
手机号/账号登陆
使用第三方直接登陆
application.properties:
#Thymeleaf 编码
spring.thymeleaf.encoding=UTF-8
#热部署静态文件
spring.thymeleaf.cache=false
#使用HTML5标准
spring.thymeleaf.mode=HTML5
#使用h2控制台
spring.h2.console.enabled=true
#datasource 我这里连接数据库报错了,没找到原因
spring.datasource.url=jdbc:mysql://localhost:/3306/springboot_user
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#jpa
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
https://pan.baidu.com/s/1Khpu020Mzfga5ApIeN9GVw 提取码:fkei