SSM增删改查

项目结构 

AccountController

package com.qcby.controller;

import com.qcby.entity.Account;
import com.qcby.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class AccountController {
    @Autowired
    private AccountService accountService;

    /**
     * 处理超链接发送出来的请求
     * @param model
     * @return
     */
    @RequestMapping(path = "/hello")
    public String sayHello(Model model){
        System.out.println("入门方法执行了2...");
        List<Account> accounts = accountService.findAll();
        for (Account account: accounts) {
            System.out.println(account.toString());
        }
        // 向模型中添加属性msg与值,可以在html页面中取出并渲染
        model.addAttribute("msg","hello,SpringMVC");
        // 配置了视图解析器后,写法
        return "suc";
    }
    /**
     * 使用异常处理器方式
     * @return
     */
    @RequestMapping("/findAll.do")
    public String findAll(){
        System.out.println("执行了...");
        // 模拟异常
        int a = 10/0;
        return "suc";
    }
}

StudentController

package com.qcby.controller;

import com.qcby.entity.Student;
import com.qcby.service.StudentService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author Suhe
 */
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @RequestMapping("/insert")
    @ResponseBody
    public String insert(Student student){
        student.setCreate_time(new Date());
        int account = studentService.insert(student);
        String data;
        if(account == 1){
            data = "\"code\":\"200\",\"msg\":\"success\"";
        } else{
            data = "\"code\":\"404\",\"msg\":\"error\"";
        }
        return data;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public String delete(int id){
        int account = studentService.delete(id);
        String data;
        if(account == 1){
            data = "\"code\":\"200\",\"msg\":\"success\"";
        } else{
            data = "\"code\":\"404\",\"msg\":\"error\"";
        }
        return data;
    }

    @RequestMapping("deletes")
    @ResponseBody
    public String deleteMoreByArray(@RequestParam("ids")Integer[] ids){
        int account = studentService.deleteMoreByArray(ids);
        String data;
        if(account == ids.length){
            data = "\"code\":\"200\",\"msg\":\"success\"";
        } else{
            data = "\"code\":\"404\",\"msg\":\"error\"";
        }
        return data;
    }

    @RequestMapping("update")
    @ResponseBody
    public String update(Student student){
        student.setCreate_time(new Date());
        int account = studentService.update(student);
        String data;
        if(account == 1){
            data = "\"code\":\"200\",\"msg\":\"success\"";
        } else{
            data = "\"code\":\"404\",\"msg\":\"error\"";
        }
        return data;
    }
    @RequestMapping("findAll")
    @ResponseBody
    public List<Student> findAll(@Param("pageSize")Integer pageSize, @Param("pageIndex")Integer pageIndex){
        return studentService.findAll(pageSize,pageIndex);
    }

    @RequestMapping("getStudentByClassName")
    @ResponseBody
    public List<Student> getStudentByClassName(Student student){
        return studentService.findStudent(student);
    }

    @RequestMapping("getStudentBySno")
    @ResponseBody
    public List<Student> getStudentBySno(Student student){
        return studentService.findStudent(student);
    }

    @RequestMapping("likeByName")
    @ResponseBody
    public List<Student> likeByName(Student student){
        return studentService.likeByName(student);
    }
}

StudentDao

package com.qcby.dao;

import com.qcby.entity.Student;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * @author Suhe
 */
public interface StudentDao {

    /**
     * 添加
     * @param student 添加的学生
     * @return int
     */
    int insert(Student student);

    /**
     * 删除
     * @param id ID
     * @return int
     */
    int delete(int id);

    /**
     * 通过数组批量删除
     * @param ids ID
     * @return int
     */
    int deleteMoreByArray(@Param("ids") Integer[] ids);

    int update(Student student);

    List<Student> findAll(@Param("pageSize")Integer pageSize, @Param("pageIndex")Integer pageIndex);

    List<Student> findStudent(Student student);

    List<Student> likeByName(Student student);
}

UserDao

package com.qcby.dao;

import com.qcby.entity.Account;

import java.util.List;

/**
 * @author Suhe
 */
public interface UserDao {
    List<Account> findAll();
}

 

Account

package com.qcby.entity;

public class Account {
    private int id;
    private String name;
    private Double money;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Double getMoney() {
        return money;
    }

    public void setMoney(Double money) {
        this.money = money;
    }

    @Override
    public String toString() {
        return "Account{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", money=" + money +
                '}';
    }
}

Class

package com.qcby.entity;

import java.util.Date;

/**
 * @author Suhe
 */
public class Class {
    private Integer id;
    private String name;
    private String remarks;
    private Date create_time;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRemarks() {
        return rema
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值