目录
步骤
1、建立项目
2、导入依赖包
3、配置数据库
4、配置各种配置文件
5、导入页面
bean包下User类
public class User {
public User() {
}
public User(int id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public User(String username, String password) {
this.username = username;
this.password = password;
}
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int 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;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
controller包下UserController类
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user) {
boolean flag = userService.login(user.getUsername(), user.getPassword());
ModelAndView modelAndView = new ModelAndView();
if (flag) {
modelAndView.setViewName("main");
} else {
modelAndView.setViewName("../failer");
}
return modelAndView;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll() {
List<User> userList = userService.findAll();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userList", userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
@RequestMapping("/deleteById.do")
public String delete(int id) {
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/add.do")
public String add(User user) {
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id) {
User user = userService.selectUserById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user-update");
modelAndView.addObject("user", user);
return modelAndView;
}
@RequestMapping("/update.do")
public String update(User user) {
userService.update(user);
return "redirect:findAll.do";
}
}
dao包下UserDao类
public interface UserDao {
User findUserByUserName(String username);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectById(int id);
void update(User user);
}
service包下UserServiceImpl类和UserService接口
@Service
public class UserService implements IUserService {
@Autowired
private UserDao userDao;
@Override
public boolean login(String username, String password) {
User user=userDao.findUserByUserName(username);
if (user!=null && user.getPassword().equals(password)){
return true;
}
return false;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
public interface IUserService {
boolean login(String username,String password);
List<User> findAll();
void deleteById(int id);
void add(User user);
User selectUserById(int id);
void update(User user);
}
mapper
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.zhongruan.dao.UserDao" >
<select id="findUserByUserName" parameterType="String" resultType="user">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
<insert id="add" parameterType="user">
insert into tb_user (username,password) values (#{username},#{password})
</insert>
<select id="selectById" parameterType="int" resultType="user">
select * from tb_user where id=#{id}
</select>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</update>
</mapper>
结果展示
登陆界面
用户管理界面
数据库
添加用户
修改用户