SpringMVC增删改查

举一个使用用户表的例子,我这里使用的是注解方式去写的

1.先编写实体类(这里就不放上实例了)

2.在编写mapper接口

<!--查询所有-->
List<User>getSelect();
 /**
     * 新增
     * @return
     */
    int addUser(User user);

    /**
     * 修改
     * @return
     */
    int updUser(User user);

    /**
     * 删除
     * @return
     */
    int delUser(int id);

<!--根据id查询-->
    User getById(int id);

3.然后我们编写mapper.xml,这里需要注意的是namespace里填写的一定是mapper接口的路径

<?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.mapper.UserMapper">
    <!--查询所有-->
    <select id="getSelect" resultType="com.domain.User">
        select * from User
    </select>
    <!--新增-->
    <insert id="addUser">
        INSERT INTO user(name,pwd) VALUES (#{name},#{pwd})
    </insert>
    <!--修改-->
    <update id="updUser">
        UPDATE user SET name=#{name}, pwd=#{pwd} WHERE id=#{id}
    </update>
    <!--删除-->
    <delete id="delUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
    <select id="getById" resultType="com.domain.User" parameterType="java.lang.Integer">
        select * from User where id=#{id}
    </select>
</mapper>

4.编写Service接口和实现类

public interface UserService {
    /**
     * 查询所有
     * @return
     */
    List<User> getSelect();

    /**
     * 新增
     * @return
     */
    int addUser(User user);

    /**
     * 修改
     * @return
     */
    int updUser(User user);

    /**
     * 删除
     * @return
     */
    int delUser(int id);

    User getById(int id);
}
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
    @Qualifier("userMapper")
    @Autowired
    private UserMapper userMapper;
    /**
     * 查询所有
     * @return
     */
    @Override
    public List<User> getSelect() {
        return userMapper.getSelect();
    }

    /**
     * 新增
     * @return
     */
    @Override
    public int addUser(User user) {
        return userMapper.addUser(user);
    }

    /**
     * 修改
     * @return
     */
    @Override
    public int updUser(User user) {
        return userMapper.updUser(user);
    }

    /**
     * 删除
     * @return
     */
    @Override
    public int delUser(int id) {
        return userMapper.delUser(id);
    }

    @Override
    public User getById(int id) {
        return userMapper.getById(id);
    }
}

5.编写controller层

controller层是用来接收前台数据和返回页面请求信息的,Controller层是不允许直接操作数据库的

@Controller
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 查询所有
     * @param modelAndView
     * @return
     */
    @RequestMapping("/getall")
    public ModelAndView getAll(ModelAndView modelAndView){
        List<User>getall=userService.getSelect();
        modelAndView.addObject("getall",getall);
        modelAndView.setViewName("userAll");
        return modelAndView;
    }

    /**
     * 根据id删除
     * @param id
     * @return
     */
    @RequestMapping("/getdel")
    public ModelAndView getdel(@RequestParam("id") int id,ModelAndView modelAndView){
        int count=userService.delUser(id);
        if (count>0){
            modelAndView.addObject("user",count);
            modelAndView.setViewName("userAll");
        }
        return modelAndView;
    }

    /**
     * 新增
     * @param
     * @return
     */
    @RequestMapping("/getadd")
    public ModelAndView getadd(@RequestParam(value = "name") String name,@RequestParam(value = "pwd")String pwd,ModelAndView modelAndView){
        User user=new User(0,name,pwd);
        userService.addUser(user);
        modelAndView.setViewName("userAdd");
        return modelAndView;
    }

    /**
     * 修改
     * @param
     * @return
     */
    @RequestMapping("/getupd")
    public ModelAndView getupd(@RequestParam(value = "id") int id,@RequestParam(value = "name") String name,@RequestParam(value = "pwd")String pwd,ModelAndView modelAndView){
        User user=new User(id,name,pwd);
        userService.updUser(user);
        modelAndView.setViewName("userUpd");
        return modelAndView;
    }

    @RequestMapping("/getById")
    public ModelAndView getById(@RequestParam("id") int id,ModelAndView modelAndView){
        User user=userService.getById(id);
        if (user!=null){
            modelAndView.addObject("user",user);
            modelAndView.setViewName("userUpd");
            return modelAndView;
        }
        modelAndView.setViewName("404");
        return modelAndView;
    }
}

6.编写jsp页面

这里主页会用到 c:forEach,所以我们要先导号两个jar包

<dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-spec</artifactId>
            <version>1.2.5</version>
        </dependency>
        <dependency>
            <groupId>org.apache.taglibs</groupId>
            <artifactId>taglibs-standard-impl</artifactId>
            <version>1.2.5</version>
        </dependency>

导好后就可以开始编写了

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>查询所有</title>
</head>
<body>
    <table border="1px">
      <tr>
        <th>id</th>
        <th>用户名</th>
        <th>用户密码</th>
          <th>操作</th>
      </tr>
      <c:forEach items="${getall}" var="user">
          <tr>
              <th>${user.id}</th>
              <th>${user.name}</th>
              <th>${user.pwd}</th>
              <th>
                  <a href="getdel?id=${user.id}">删除</a>
                  <a href="getById?id=${user.id}">修改</a>
              </th>
          </tr>
      </c:forEach>
    </table>
        <a href="jsp/userAdd.jsp">新增用户</a>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>新增</title>
</head>
<body>
    <form action="http://localhost:8080/springMVC_war_exploded/getadd" method="get">
        用户名:<input type="text" name="name">
        密码:<input type="text" name="pwd">
            <input type="submit" value="新增">
    </form>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
</head>
<body>
    <form action="http://localhost:8080/springMVC_war_exploded/getupd" method="get">
      id:<input type="text" name="id" value="${user.id}" readonly>
      用户名:<input type="text" name="name" value="${user.name}">
      密码:<input type="text" name="pwd" value="${user.pwd}">
      <input type="submit" value="修改">
    </form>
</body>
</html>

到这里我们就已经写好了整个增删改查的步骤

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值