线上实习作业——增删改查

User


package com.zhongruan.bean;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class User {

    public User() {
    }

    public User(int id, String username, String password) {
        this.id = id;
        this.username = username;
        this.password = password;
    }

    public User(String username, String password) {
        this.username = username;
        this.password = password;
    }

    private int id;
    private String username;
    private String 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 + '\'' +
                '}';
    }
}

 

package com.zhongruan.controller;

import com.zhongruan.bean.User;
import com.zhongruan.service.IUserService;
import com.zhongruan.service.impl.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;

 

 

UserController

​​​​​


@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    @RequestMapping("/login.do")
    public ModelAndView login(User user){
        boolean flag = userService.login(user.getUsername(), user.getPassword());
        ModelAndView modelAndView=new ModelAndView();
        if(flag){
            modelAndView.setViewName("main");
        }else {
            modelAndView.setViewName("../failer");
        }
       return modelAndView;

    }

    @RequestMapping("/findAll.do")
    public ModelAndView findAll(){
        List<User> userList = userService.findAll();
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList",userList);
        modelAndView.setViewName("user-list");
        return modelAndView;
    }

    @RequestMapping("/deleteById.do")
    public String delete(int id){
        userService.deleteById(id);
        return "redirect:findAll.do";
    }

    @RequestMapping("/add.do")
    public String add(User user){
        userService.add(user);
        return "redirect:findAll.do";
    }

    @RequestMapping("toUpdate.do")
    public ModelAndView toUpdate(int id){
        User user = userService.selectUserById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("user-update");
        modelAndView.addObject("user",user);
        return modelAndView;
    }

    @RequestMapping("/Update.do")
    public String Update(User user){
        userService.Update(user);
        return "redirect:findAll.do";
    }
}

 

UserDao

 

package com.zhongruan.dao;

import com.zhongruan.bean.User;

import java.util.List;

public interface UserDao {
    User findUserByUserName(String username);

    List<User> findAll();

    void deleteById(int id);

    void add(User user);

    User selectById(int id);

    void Update(User user);
}

 

UserService

 

package com.zhongruan.service.impl;

import com.zhongruan.bean.User;
import com.zhongruan.dao.UserDao;
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 UserDao userDao;

    @Override
    public boolean login(String username, String password) {
        User user = userDao.findUserByUserName(username);
        if (user!=null && user.getPassword().equals(password)){
            return true;
        }
        return false;
    }

    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public void deleteById(int id) {
        userDao.deleteById(id);
    }

    @Override
    public void add(User user) {
        userDao.add(user);
    }

    @Override
    public User selectUserById(int id) {
        return userDao.selectById(id);
    }

    @Override
    public void Update(User user) { userDao.Update(user); }

}

 

IUserService

package com.zhongruan.service;

import com.zhongruan.bean.User;

import java.util.List;

public interface IUserService {
    boolean login(String username,String password);

    List<User> findAll();

    void deleteById(int id);

    void add(User user);

    User selectUserById(int id);

    void Update(User user);
}

UserMapper

<?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.UserDao" >
    <select id="findUserByUserName" parameterType="String" resultType="user">
        select * from wzbc1 where username=#{username}
    </select>

    <select id="findAll" resultType="user">
        select * from wzbc1
    </select>

    <delete id="deleteById" parameterType="int">
        delete from wzbc1 where id = #{id}
    </delete>

    <insert id="add" parameterType="user">
        insert into wzbc1 (id,username,password) values (#{id},#{username},#{password})
    </insert>

    <select id="selectById" parameterType="int" resultType="user">
        select * from wzbc1 where id=#{id}
    </select>

    <update id="Update" parameterType="user">
        update wzbc1 set id=#{id}, username=#{username}, password=#{password} where id= #{id}
   </update>
</mapper>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值