ssm
结构
User见前篇
控制层
package com.zhongruan.controller;
import com.github.pagehelper.PageInfo;
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(int pageNum,int size){
List<User> users=userService.findAll(pageNum, size);
PageInfo<User> pageInfo=new PageInfo<>(users);
ModelAndView modelAndView=new ModelAndView();
pageInfo.setList(users);
modelAndView.setViewName("allUser.jsp");
modelAndView.addObject("pageInfo",pageInfo);
return modelAndView;
}
@RequestMapping("/login.do")
public String login(User user){
Boolean flag = userService.login(user.getUsername(),user.getPassword());
if(flag){
return "redirect:/findAll.do?pageNum=1&size=5";
}else {
return "failure.jsp";
}
}
@RequestMapping("/delete.do")
public String deleteById(int id){
userService.deleteById(id);
return "redirect:/findAll.do";
}
@RequestMapping("/update.do")
public String update(int id,String username,String password){
userService.update(id,username,password);
return "redirect:/findAll.do";
}
@RequestMapping("/add.do")
public String add(String username,String password){
userService.add(username,password);
return "redirect:/findAll.do";
}
@RequestMapping("/toupdate.do")
public ModelAndView toupdate(User user) {
int id = user.getId();
User users = userService.findById(id);
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("updateUser.jsp");
modelAndView.addObject("users", users);
return modelAndView;
}
}
IUserDao
package com.zhongruan.dao;
import com.zhongruan.bean.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface IUserDao {
User selectByUserName(String username);
List<User> findAll();
void deleteById(int id);
void add(@Param("username") String username, @Param("password")String password);
void update(@Param("id") int id,@Param("username")String username, @Param("password")String password);
User findById(int id);
}
业务层
package com.zhongruan.service.impl;
import com.github.pagehelper.PageHelper;
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.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
@Service
public class UserService implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAll(int pageNum,int size){
PageHelper.startPage(pageNum, size);
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 add(String username,String password) {
userDao.add(username,password);
}
@Override
public void update(int id,String username,String password) {
userDao.update(id,username,password);
}
@Override
public User findById(int id) {
return userDao.findById(id);
}
@Override
public int size() {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public Iterator<User> iterator() {
return null;
}
@Override
public Object[] toArray() {
return new Object[0];
}
@Override
public <T> T[] toArray(T[] a) {
return null;
}
@Override
public boolean add(User user) {
return false;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> c) {
return false;
}
@Override
public boolean addAll(Collection<? extends User> c) {
return false;
}
@Override
public boolean addAll(int index, Collection<? extends User> c) {
return false;
}
@Override
public boolean removeAll(Collection<?> c) {
return false;
}
@Override
public boolean retainAll(Collection<?> c) {
return false;
}
@Override
public void clear() {
}
@Override
public User get(int index) {
return null;
}
@Override
public User set(int index, User element) {
return null;
}
@Override
public void add(int index, User element) {
}
@Override
public User remove(int index) {
return null;
}
@Override
public int indexOf(Object o) {
return 0;
}
@Override
public int lastIndexOf(Object o) {
return 0;
}
@Override
public ListIterator<User> listIterator() {
return null;
}
@Override
public ListIterator<User> listIterator(int index) {
return null;
}
@Override
public List<User> subList(int fromIndex, int toIndex) {
return null;
}
}
IUserService
package com.zhongruan.service;
import com.zhongruan.bean.User;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
public interface IUserService extends List<User> {
List<User> findAll(@RequestParam int pageNum, @RequestParam int size);
Boolean login(String username, String password);
void deleteById(int id);
void add(String username,String password);
void update(int id,String username,String password);
User findById(int id);
}
UserMapper1
<?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.IUserDao">
<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>
<select id="add" parameterType="String">
insert into tb_user(username,password) values (#{username},#{password})
</select>
<select id="update" parameterType="user">
update tb_user set username=#{username},password=#{password} where id=#{id}
</select>
<select id="findById" parameterType="int" resultType="com.zhongruan.bean.User">
select * from tb_user where id=#{id}
</select>
</mapper>