结构
User见前篇
控制层
@Controller
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("findAll.do")
public ModelAndView findAll(){
List<User> users=userService.findAll();
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("allUser.jsp");
modelAndView.addObject("users",users);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user){
Boolean flag=userService.login(user.getUsername(),user.getPassword());
if (flag){
return "redirect:/findAll.do";
}else {
return "fail.jsp";
}
}
@RequestMapping("/delete.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:/findAll.do";
}
}
IUserDao
public interface IUserDao {
List<User> findAll();
User selectByUserName(String name);
void deleteById(int id);
}
IUserService
public interface IUserService {
List<User> findAll();
Boolean login(String username,String password);
void deleteById(int id);
}
UserService
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public Boolean login(String username, String password) {
User user=userDao.selectByUserName(username);
if (user !=null && password.equals(user.getPassword())){
return true;
}
return false;
}
@Override
public void deleteById(int id) {
userDao.deleteById(id);
}
}
UserMapper1
<mapper namespace="com.zhongruan.dao.IUserDao">
<select id="findAll" resultType="com.zhongruan.bean.User">
select * from tb_user
</select>
<select id="selectByUserName" resultType="com.zhongruan.bean.User">
select * from tb_user where username=#{username}
</select>
<delete id="deleteById" parameterType="int">
delete from tb_user where id=#{id}
</delete>
</mapper>
db中密码改为自己的密码
allUser界面用自己的美化将表单等的页面做修改