1、DAO层接口和实现
在classInfoDao.java里写增删改接口
package com.gychen.dao;
import com.gychen.po.ClassInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;
import java.util.List;
@Component("classDao")
public interface ClassInfoDao {
/**
* 查询所有图书类型信息
*/
// @Select("select * from class_info")
// List queryClassInfoAll();
List queryClassInfoAll(@Param(value = "name")String name);
/**
* 添加图书类型
*/
void addBookType(ClassInfo classInfo);
/**
* 修改 根据id查询记录信息
*/
ClassInfo queryBookTypeById(Integer id);
/**
* 修改图书类型信息
*/
void updateBookType(ClassInfo classInfo);
/**
* 删除 根据一个或多个id删除
*/
void deleteBookTypeByIds(List id);
}
在ClassInfoDao.xml里实现
增删改语句
select * from class_info
and name like '%${name}%'
insert into class_info (name,remarks) values (#{name},#{remarks})
select * from class_info where id=#{id}
update class_info set name=#{name},remarks=#{remarks} where id=#{id}
delete from class_info where id in
#{ids}
2、service层接口和实现
package com.gychen.service;
import com.github.pagehelper.PageInfo;
import com.gychen.po.ClassInfo;
import java.util.List;
public interface ClassInfoService {
/**
* 查询所有的图书类型信息
*/
PageInfo queryClassInfoAll(String name,int page,int limit);
/**
* 添加图书类型
*/
void addBookType(ClassInfo classInfo);
/**
* 修改 根据id查询记录信息
*/
ClassInfo queryBookTypeById(Integer id);
/**
* 修改图书类型信息
*/
void updateBookType(ClassInfo classInfo);
/**
* 删除 根据一个或多个id删除
*/
void deleteBookTypeByIds(List ids);
}
在ClassInfoServiceImpl里实现
增删改语句
package com.gychen.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gychen.dao.ClassInfoDao;
import com.gychen.po.ClassInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service("classInfoService")
public class ClassInfoServiceImpl implements ClassInfoService {
@Autowired
private ClassInfoDao classDao;
@Override
public PageInfo queryClassInfoAll(String name,int page, int limit) {
// 传入参数,每页条数,当前页
PageHelper.startPage(page,limit);
List list = classDao.queryClassInfoAll(name);
// 通过包装获取分页需要的其他值
PageInfo pageInfo = new PageInfo<>(list);
return pageInfo;
}
@Override
public void addBookType(ClassInfo classInfo) {
classDao.addBookType(classInfo);
}
@Override
public ClassInfo queryBookTypeById(Integer id) {
return classDao.queryBookTypeById(id);
}
@Override
public void updateBookType(ClassInfo classInfo) {
classDao.updateBookType(classInfo);
}
@Override
public void deleteBookTypeByIds(List ids) {
classDao.deleteBookTypeByIds(ids);
}
}
3、Controller层实现
package com.gychen.controller;
import com.github.pagehelper.PageInfo;
import com.gychen.po.ClassInfo;
import com.gychen.service.ClassInfoService;
import com.gychen.util.RestResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
@Controller
public class TypeController {
@Autowired
private ClassInfoService classInfoService; // 这里的classInfoService要和ClassInfoServiceImpl中的注解名字相同
@RequestMapping("/typeIndex")
public String typeIndex(){
return "type/typeIndex";
}
/**
* 添加页面的转换
*/
@GetMapping("/typeAdd")
public String typeAdd(){
return "type/addType";
}
/**
* 获取type数据信息 分页
*/
@RequestMapping("/typeAll")
@ResponseBody
public RestResponse typeAll(String name, @RequestParam(defaultValue = "1") Integer page,
@RequestParam(defaultValue = "15") Integer limit){
PageInfo pageInfo = classInfoService.queryClassInfoAll(name, page, limit);
return RestResponse.ok(pageInfo.getList());
}
/**
* 类型添加
*/
@ResponseBody
@RequestMapping("/addBookType")
public RestResponse addBookType(ClassInfo classInfo){
classInfoService.addBookType(classInfo);
return RestResponse.ok();
}
/**
* 根据id查询
*/
@GetMapping("/queryBookTypeById")
public String queryBookTypeById(Integer id, Model model){
ClassInfo classInfo = classInfoService.queryBookTypeById(id);
model.addAttribute("info",classInfo);
return "type/updateType";
}
/**
* 修改提交功能
*/
@ResponseBody
@RequestMapping("/updateBookType")
public RestResponse updateBookType(@RequestBody ClassInfo classInfo){
classInfoService.updateBookType(classInfo);
return RestResponse.ok();
}
/**
* 删除类型
*/
@ResponseBody
@RequestMapping("/deleteType")
public RestResponse deleteType(String ids){
List list = Arrays.asList(ids.split(","));
classInfoService.deleteBookTypeByIds(list);
return RestResponse.ok();
}
}