数据提交到后台数据库
写在前面:本篇仅用于帮助博主个人梳理思路
源码地址在本人GitHub:链接: Bond-hsh.
.js文件(common.js)
//统一ajax请求
function ajaxRequest(url,requestType,data,callback){
$.ajax({
url:url,
type:requestType,
data:data,
dataType:'json',
success:function(rst){
if(rst.code == 0){
callback(rst);
}else{
alert(rst.msg);
}
},
error:function(data){
alert('网络错误!');
}
});
}
.ftl文件(publish.ftl)
ajaxRequest('publish','post',$("#publish-form").serialize(),function(){
window.location.href="index";
});
控制器文件(stuController.java)
@RequestMapping(value="/publish",method=RequestMethod.POST)
@ResponseBody
public Result<Boolean> publish(Goods goods){
CodeMsg validate = ValidateEntityUtil.validate(goods);
if(validate.getCode()!=CodeMsg.SUCCESS.getCode()){
return Result.error(validate);
}
if(goods.getGoodsCategory() ==null ||goods.getGoodsCategory().getId() == null ||goods.getGoodsCategory().getId().longValue() == -1){
return Result.error(CodeMsg.HOME_STU_PUBLISH_CATEGORY_EMPTY);
}
Stu loginedStu = (Stu)SessionUtil.get(SessionConstant.SESSION_STUDENT_LOGIN_KEY);
goods.setStu(loginedStu);
if(goodsService.save(goods) == null){
return Result.error(CodeMsg.HOME_STU_PUBLISH_ERROR);
}
return Result.success(true);
}
新建Dao层(GoodsDao.java)
package shuhuan.bond.dao.common;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import shuhuan.bond.entity.common.Goods;
import shuhuan.bond.entity.common.Stu;
/**
* 债券数据库操作DAO
* @author Shinelon
*
*/
@Repository
public interface GoodsDao extends JpaRepository<Goods, Long> {
@Query("select g from Goods g where id = :id")
Goods find(@Param("id")Long id);
/**
* 根据用户查询
* @param stu
* @return
*/
List<Goods> findByStu(Stu stu);
}
新建service层(GoodsService.java)
package shuhuan.bond.service.common;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import shuhuan.bond.bean.PageBean;
import shuhuan.bond.dao.common.GoodsDao;
import shuhuan.bond.entity.common.Goods;
@Service
public class GoodsService {
@Autowired
private GoodsDao goodsDao;
/**
* 物品添加/编辑;当id不为空时,编辑
* @param goods
* @return
*/
public Goods save(Goods goods){
return goodsDao.save(goods);
}
/**
* 搜索分类列表
* @param pageBean
* @param goods
* @return
*/
public PageBean<Goods> findlist(PageBean<Goods> pageBean,Goods goods){
ExampleMatcher exampleMatcher = ExampleMatcher.matching();
exampleMatcher = exampleMatcher.withMatcher("name", GenericPropertyMatchers.contains());
//不加的话,也会把int直接识别成0,数据库会有问题
exampleMatcher=exampleMatcher.withIgnorePaths("flag");
Example<Goods> example=Example.of(goods, exampleMatcher);
Sort sort = Sort.by(Direction.DESC, "createTime");
PageRequest pageable=PageRequest.of(pageBean.getCurrentPage()-1, pageBean.getPageSize(), sort);
Page<Goods> findAll = goodsDao.findAll(example, pageable);
pageBean.setContent(findAll.getContent());
pageBean.setTotal(findAll.getTotalElements());
pageBean.setTotalPage(findAll.getTotalPages());
return pageBean;
}
/**
* 根据id查询
* @param id
* @return
*/
public Goods findById(Long id){
return goodsDao.find(id);
}
/**
* 物品分类删除
* @param id
*/
public void delete(Long id){
goodsDao.deleteById(id);
}
/**
* 在前端获取所有物品
* @return
*/
public List<Goods>findAll(){
return goodsDao.findAll();
}
}