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<ClassInfo> queryClassInfoAll();
List<ClassInfo> queryClassInfoAll(@Param(value = "name")String name);
/**
* 添加图书类型
*/
void addBookType(ClassInfo classInfo);
/**
* 修改 根据id查询记录信息
*/
ClassInfo queryBookTypeById(Integer id);
/**
* 修改图书类型信息
*/
void updateBookType(ClassInfo classInfo);
/**
* 删除 根据一个或多个id删除
*/
void deleteBookTypeByIds(List<Integer> id);
}
- 在ClassInfoDao.xml里
实现
增删改语句
<?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.gychen.dao.ClassInfoDao" >
<!-- 查询全部信息 -->
<select id="queryClassInfoAll" resultType="com.gychen.po.ClassInfo">
select * from class_info
<where>
<if test="name!=null">
and name like '%${name}%'
</if>
</where>
</select>
<!-- 增加图书类型 -->
<insert id="addBookType" parameterType="classInfo">
insert into class_info (name,remarks) values (#{name},#{remarks})
</insert>
<!-- 根据id查询类型信息 -->
<select id="queryBookTypeById" parameterType="int" resultType="classInfo">
select * from class_info where id=#{id}
</select>
<!-- 修改图书类型信息 -->
<update id="updateBookType" parameterType="classInfo">
update class_info set name=#{name},remarks=#{remarks} where id=#{id}
</update>
<!-- 根据ids 删除图书类型信息 -->
<delete id="deleteBookTypeByIds" parameterType="list">
delete from class_info where id in
<foreach collection="list" item="ids" open="(" separator="," close=")">
#{ids}
</foreach>
</delete>
</mapper>
2、service层接口和实现
package com.gychen.service;
import com.github.pagehelper.PageInfo;
import com.gychen.po.ClassInfo;
import java.util.List;
public interface ClassInfoService {
/**
* 查询所有的图书类型信息
*/
PageInfo<ClassInfo> queryClassInfoAll(String name,int page,int limit);
/**
* 添加图书类型
*/
void addBookType(ClassInfo classInfo);
/**
* 修改 根据id查询记录信息
*/
ClassInfo queryBookTypeById(Integer id);
/**
* 修改图书类型信息
*/
void updateBookType(ClassInfo classInfo);
/**
* 删除 根据一个或多个id删除
*/
void deleteBookTypeByIds(List<Integer> 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<ClassInfo> queryClassInfoAll(String name,int page, int limit) {
// 传入参数,每页条数,当前页
PageHelper.startPage(page,limit);
List<ClassInfo> list = classDao.queryClassInfoAll(name);
// 通过包装获取分页需要的其他值
PageInfo<ClassInfo> 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<Integer> 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<ClassInfo> 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();
}
}