为了账号安全,请及时绑定邮箱和手机立即绑定

springboot+mybatis实现用户增删改查

标签:
MySQL SpringBoot

1.user实体类

@Data
public class User {
    private Integer userId;
    private String userName;
    private String passWord;
}

public interface UserDao {

    List<User> queryUser();

    void deleteUser(int userId);

    void insertUser(User user);

    User getUserId(int userId);

    void updateUser(User user);

    List<User> getUserName(String userName);

    User userLogin(String userName,String passWord);

    int countUser();

}

<?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.ecjtu.demo.dao.UserDao">
    <select id="queryUser" resultType="com.ecjtu.demo.entity.User">
        select * FROM user ORDER BY userId ASC
    </select>

    <select id="userLogin" resultType="com.ecjtu.demo.entity.User">
         SELECT *
        FROM USER
        WHERE
        userName = #{userName} AND passWord = #{passWord}
    </select>

    <delete id="deleteUser">
        DELETE FROM
        USER
        WHERE
        userId =
        #{userId}
    </delete>
    <insert id="insertUser" useGeneratedKeys="true" keyProperty="userId"
            keyColumn="userId" parameterType="com.ecjtu.demo.entity.User">
        INSERT INTO
        USER (userName,passWord)
        VALUES
        (#{userName},#{passWord})
    </insert>

    <select id="getUserId" resultType="com.ecjtu.demo.entity.User">
        SELECT *
        FROM USER
        WHERE
        userId=#{userId}
    </select>
    <update id="updateUser" parameterType="com.ecjtu.demo.entity.User">
        update USER
        <set>
            <if test="userName != null">userName=#{userName},</if>
            <if test="passWord != null">passWord=#{passWord}</if>
        </set>
        where userId=#{userId}
    </update>

    <select id="getUserName" resultType="com.ecjtu.demo.entity.User">
        SELECT *
        FROM USER
        WHERE
        userName=#{userName}
    </select>

    <select id="countUser" resultType="int" >
        SELECT count(*)
        FROM USER
    </select>
</mapper>

public interface UserService
{
    /**
     * 得到用户列表
     * @return
     */
    List<User> getListUser();

    /**
     * 删除用户
     * @param userId
     * @return
     */
    void deleteUser(int userId);

    void insertUser(User user);

    User getUserId(int userId);

    void updateUser(User user);

    List<User> getUserName (String userName);

    User Login(String userName,String passWord);

    int countUser();

    List<User> findUserByPage();
}

@Service
public class UserServiceImpl implements UserService{

    @Autowired
    private UserDao userDao;

    public List<User> getListUser(){
        return userDao.queryUser();
    }

    public void deleteUser(int userId){
        userDao.deleteUser(userId);
    }

    public void insertUser(User user) {
         userDao.insertUser(user);
    }

    public User getUserId(int userId){
        return userDao.getUserId(userId);
    }

    public void updateUser(User user){
         userDao.updateUser(user);
    }

    public List<User> getUserName (String userName){
        return userDao.getUserName(userName);
    }

    public int countUser(){return userDao.countUser();}

    public User Login(String userName,String passWord){
        return userDao.userLogin(userName,passWord);
    }

    public List<User> findUserByPage(){
        List<User> allUser = userDao.queryUser();//全部商品
        return allUser;
    }

}

@RequestMapping("/show")
public ModelAndView getIndex(Map<String, Object> map) {
    List<User> list = new ArrayList<User>();     // 获取区域列表
    list = userServiceImpl.getListUser();
    map.put("list", list);
    return new ModelAndView("index3", map);
}/**
 * 根据用户名得到用户信息
 */
@RequestMapping(value = "/query")
public ModelAndView getUserName(@RequestParam("userName") String userName) {
    Map<String, Object> mm = new HashMap<String, Object>();
    List<User> list = new ArrayList<User>();
    list = userServiceImpl.getUserName(userName);
    mm.put("list", list);
    return new ModelAndView("show", mm);
}

/**
 * 根据id获取用户信息
 */
@RequestMapping(value = "/update", method = RequestMethod.GET)
public ModelAndView getUserById(int userId) {
    Map<String, Object> mm = new HashMap<String, Object>();
    User user = userServiceImpl.getUserId(userId);
    mm.put("user", user);
    return new ModelAndView("form", mm);
}

/**
 * 更新用户
 */
@RequestMapping(value = "/updateUser")
public void updateUser(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord, @RequestParam("userId") int userId) {
    User user = userServiceImpl.getUserId(userId);
    user.setUserName(userName);
    user.setPassWord(passWord);
    userServiceImpl.updateUser(user);
}

/**
 * 删除用户
 */
@RequestMapping(value = "/delete", method = RequestMethod.GET)
public void delete(Integer userId) {
    userServiceImpl.deleteUser(userId);
}

/**
 * 添加用户
 */
@ResponseBody
@RequestMapping(value = "/save", method = RequestMethod.POST)
public void save(String userName, String passWord) {
    User user = new User();
    user.setUserName(userName);
    user.setPassWord(passWord);
    userServiceImpl.insertUser(user);
    
}
7.index3.ftl文件
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>layout 后台大布局 - Layui</title>
    <link rel="stylesheet" href="../static/layui/css/layui.css">
    <link rel="stylesheet" href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css">
    <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
        $(document).ready(function () {
            $("#addUser").click(function () {
                $("#formAdd").toggle();
            })
            $("#queryUser").click(function () {
                $("#formAdd2").toggle();
            })

        })
    </script>
</head>
<body class="layui-layout-body">
<div class="layui-layout layui-layout-admin">
    <#include "nav3.ftl">
    <#include "left2.ftl">
        <div class="layui-body">
        <!-- 内容主体区域 -->
        <div style="padding: 15px;">
            <button id="addUser" type="button" class="btn btn-primary">新增用户</button>
            <button id="queryUser" type="button" class="btn btn-primary">搜索用户</button>
            <div id="formAdd" style="display: none">
                <form role="form" method="post" action="/superadmin/save">
                    <div class="form-group">
                        <label>用户名</label>
                        <input name="userName" type="text" class="form-control" />
                    </div>
                    <div class="form-group">
                        <label>密码</label>
                        <input name="passWord" type="number" class="form-control" />
                    </div>
                    <button type="submit" class="btn btn-default">提交</button>
                </form>
            </div>

            <!--搜索用户-->
            <div id="formAdd2" style="display: none">
                <form role="form" method="post" action="/superadmin/query">
                    <div class="form-group">
                        <label>根据用户名搜索</label>
                        <input name="userName" type="text" class="form-control" />
                    </div>
                    <button type="submit" class="btn btn-default">提交</button>
                </form>
            </div>
            <p>用户表</p>
            <table class="table table-bordered table-condensed table-striped table-hover">
                <thead>
                <tr>
                    <th>用户id</th>
                    <th>用户姓名</th>
                    <th>用户密码</th>
                    <th colspan="2">操作</th>
                </tr>
                </thead>
                <tbody>
                <#list list as li>
                <tr>
                    <td>${li.userId}</td>
                    <td>${li.userName}</td>
                    <td>${li.passWord}</td>
                    <td><a href="/superadmin/update?userId=${li.userId}">修改</a></td>
                    <td><a href="/superadmin/delete?userId=${li.userId}">删除</a></td>
                </tr>
                </#list>
                </tbody>
            </table>
        </div>
    </div>

    <div class="layui-footer">
        <!-- 底部固定区域 -->
        © layui.com - 底部固定区域
    </div>
</div>
<script class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="../static/layui/layui.js"></script>
<script>
    //JavaScript代码区域
    layui.use('element', function(){
        var element = layui.element;

    });
</script>
</body>
</html>

 

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消