还是先创建一个新的项目和如下这些文件(可以不建HelloController)
并且导入这些jar包和创建这些jsp文件
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<User> findAll();
User selectByUserName(String name);
void deleteById(int id);
void findUserById(User user);
void insert(User user);
}
IUserService内的代码:
package com.zhongruan.service;
import com.zhongruan.bean.User;
import java.util.List;
public interface IUserService {
List<User> findAll();
Boolean login(String username,String password);
void deleteById(int id);
void findUserById(User user);
void insert(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 findUserById(User user) {
}
@Override
public void insert(User user) {
}
}
UserMapper.xml里写增删改查的sql语句
<?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" 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>
<delete id="updateById" parameterType="com.zhongruan.bean.User">
update tb_user set username=#{username},password=#{password} where id=#{id}
</delete>
<delete id="insert" parameterType="com.zhongruan.bean.User">
insert into tb_user (username,password) value (#{username},#{password})
</delete>
</mapper>
要记得把一些.jsp文件里的转跳页面改成/login.do