项目结构
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