User
package com.zhongruan.bean;
public class User {
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 + '\'' +
'}';
}
}
Usercontroller
package com.zhongruan.controller;
import com.zhongruan.bean.User;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@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 "failure.jsp";
}
}
@RequestMapping("/delete.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:/findAll.do";
}
}
IUserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserDao {
List findAll();
User selectByUserName(String name);
void deleteById(int id);
void update(User user);
}
UserService
package com.zhongruan.service.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.IUserDao;
import com.zhongruan.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@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);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
IUserService
package com.zhongruan.service;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserService {
List findAll();
Boolean login(String username,String password);
void deleteById(int id);
void update(User user);
}
UserMapper1.xml
<?xml version="1.0" encoding="UTF-8"?><select id="findAll" resultType="com.zhongruan.bean.User">
select *from tb_user;
</select>
<select id="selectByUserName" parameterType="String" 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>
<update id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id};
</update>
<!--<insert id="insert" parameterType="com.zhongruan.bean.User">-->
<!--insert into tb_user(username, password) values(#{username},#{password});-->
<!--</insert>-->
allUser.jsp
index.jsp