bean.User
package com.zhongruan.bean;
public class User {
private int id;
private String username;
private String password;
public User(){
}
public User(String username,String password){
this.username = username;
this.password = password;
}
public User(int id, String username, String password){
this.id = id;
this.username = username;
this.password = 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
package com.zhongruan.controller;
import com.zhongruan.bean.User;
import com.zhongruan.service.UserService;
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
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login.do")
public ModelAndView login(User user){
int id = userService.login(user.getUsername(),user.getPassword());
ModelAndView mv = new ModelAndView();
if (id!=-1){
mv.setViewName("main");
}else {
mv.setViewName("../failer");
}
return mv;
}
@RequestMapping("/findAll.do")
public ModelAndView findAll(){
List<User> users = userService.findAll();
ModelAndView mv = new ModelAndView();
mv.addObject("users",users);
mv.setViewName("user-list");
return mv;
}
@RequestMapping("/add.do")
public String add(User user){
userService.add(user);
return "redirect:findAll.do";
}
@RequestMapping("/deleteById.do")
public String delete(int id){
userService.deleteById(id);
return "redirect:findAll.do";
}
@RequestMapping("/toUpdate.do")
public ModelAndView toUpdate(int id){
User user = userService.selectUserById(id);
ModelAndView mv = new ModelAndView();
mv.setViewName("user-update");
mv.addObject("user",user);
return mv;
}
@RequestMapping("/update.do")
public String update(User user){
userService.update(user);
return "redirect:findAll.do";
}
}
dao.UserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import java.util.List;
public interface UserDao {
User findByUserName(String username);
List<User> findAll();
void add(User user);
void deleteById(int id);
User selectById(int id);
void update(User user);
}
service.impl.UserServiceImpl
package com.zhongruan.service.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int login(String username, String password) {
User user = userDao.findByUserName(username);
if(user!=null&&user.getPassword().equals(password)){
return user.getId();
}
return -1;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void deleteById(int id){
userDao.deleteById(id);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
service.impl.UserServiceImpl
package com.zhongruan.service.impl;
import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
import com.zhongruan.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public int login(String username, String password) {
User user = userDao.findByUserName(username);
if(user!=null&&user.getPassword().equals(password)){
return user.getId();
}
return -1;
}
@Override
public List<User> findAll() {
return userDao.findAll();
}
@Override
public void add(User user) {
userDao.add(user);
}
@Override
public void deleteById(int id){
userDao.deleteById(id);
}
@Override
public User selectUserById(int id) {
return userDao.selectById(id);
}
@Override
public void update(User user) {
userDao.update(user);
}
}
service.UserService
package com.zhongruan.service;
import com.zhongruan.bean.User;
import java.util.List;
public interface UserService {
int login(String username,String password);
List<User> findAll();
void add(User user);
void deleteById(int id);
User selectUserById(int id);
void update(User user);
}
资源文件
mapper.UserMapper.xml
<select id="findByUserName" parameterType="String" resultType="user">
select * from tb_user where username=#{username}
</select>
<select id="findAll" resultType="user">
select * from tb_user
</select>
<insert id="add" parameterType="user">
insert into tb_user (username,password) value (#{username},#{password})
</insert>
<delete id="deleteById" parameterType="int">
delete from tb_user where id = #{id}
</delete>
<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>