背景
dao层:对应每个表做增删改查
service层:对dao的增删改查整合(一般service只调用自己的dao接口,如果想调用其他层的dao接口,则通过引入该层的service)
web层(controller层):定义路由访问,对service层整合
Service层
//配置+接口+实现类
AddressbookService.java
package com.haward.addressbook.service;
import com.haward.addressbook.entity.Addressbook;
import java.util.List;
//配置@Service+接口+实现类
public interface AddressbookService {
List<Addressbook> getAddressbookList();
Addressbook getAddressbookById(int id);
boolean addAddressbook(Addressbook addressbook);
boolean modifyAddressbook(Addressbook addressbook);
boolean deleteAddressbook(int id);
}
AddressbookServiceImpl.java
package com.haward.addressbook.service.impl;
import com.haward.addressbook.dao.AddressbookDao;
import com.haward.addressbook.entity.Addressbook;
import com.haward.addressbook.service.AddressbookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;
@Service
public class AddressbookServiceImpl implements AddressbookService {
@Autowired
private AddressbookDao addressbookDao;
@Override
public List<Addressbook> getAddressbookList() {
return addressbookDao.queryAddressbook();
}
@Override
public Addressbook getAddressbookById(int id) {
return addressbookDao.queryAddressbookById(id);
}
@Transactional
@Override
public boolean addAddressbook(Addressbook addressbook) {
if(addressbook.getAddressbookName()!=null && !"".equals(addressbook.getAddressbookName())){
addressbook.setCreateTime(new Date());
try{
int effectedNum = addressbookDao.insertAddressbook(addressbook);
if(effectedNum>0)
return true;
else{
throw new RuntimeException("插入联系人失败!");
}
}catch(Exception e){
throw new RuntimeException("插入联系人失败:"+e.getMessage());
}
}else{
throw new RuntimeException("联系人不能为空");
}
}
@Transactional
@Override
public boolean modifyAddressbook(Addressbook addressbook) {
if(addressbook.getAddressbookId()!=null && !"".equals(addressbook.getAddressbookId())){
addressbook.setLastEditTime(new Date());
try{
int effectedNum = addressbookDao.updateAddressbook(addressbook);
if(effectedNum>0)
return true;
else{
throw new RuntimeException("更新联系人失败!");
}
}catch(Exception e){
throw new RuntimeException("更新联系人失败:"+e.getMessage());
}
}else{
throw new RuntimeException("联系人Id不能为空");
}
}
@Transactional
@Override
public boolean deleteAddressbook(int id) {
if(id > 0)
{
try{
int effectedNum = addressbookDao.deleteAddressbook(id);
if(effectedNum > 0)
return true;
else{
throw new RuntimeException("删除联系人失败!");
}
}catch(Exception e){
throw new RuntimeException("删除联系人失败"+e.getMessage());
}
}else{
throw new RuntimeException("删除联系人Id出错! ");
}
}
}
controller层
//web层即Controller层
@RestController //@Controller + @ResponseBody 返回的不是页面,而是实际页面body的值
@RequestMapping("/superadmin") //指定根路由
public class AddressbookController {
@Autowired
private AddressbookService addressbookService;
@RequestMapping(value = "/listaddressbook",method = RequestMethod.GET)
private Map<String,Object> listAddressbook(){
Map<String,Object> modelMap = new HashMap<String,Object>();
List<Addressbook> list = addressbookService.getAddressbookList();
modelMap.put("addressbookList",list);
return modelMap;
}
//http://127.0.0.1:8080/addressbook/superadmin/addressbookbyid?id=2
@RequestMapping(value = "/addressbookbyid",method = RequestMethod.GET)
private Map<String,Object> getAddressbookByid(Integer id)
{
Map<String,Object> modelMap = new HashMap<>();
Addressbook addressbook = addressbookService.getAddressbookById(id);
modelMap.put("addressbook",addressbook);
return modelMap;
}
// 1、@requestBody注解常用来处理content-type不是默认的application/x-www-form-urlcoded编码的内容,比如说:application/json或者是application/xml等。一般情况下来说常用其来处理application/json类型。
//
// 2、
//
// 通过@requestBody可以将请求体中的JSON字符串绑定到相应的bean上,当然,也可以将其分别绑定到对应的字符串上。
// 例如说以下情况:
// $.ajax({
// url:"/login",
// type:"POST",
// data:'{"userName":"admin","pwd","admin123"}',
// content-type:"application/json charset=utf-8",
// success:function(data){
// alert("request success ! ");
// }
// });
//
// @requestMapping("/login")
// public void login(@requestBody String userName,@requestBody String pwd){
// System.out.println(userName+" :"+pwd);
// }
// 这种情况是将JSON字符串中的两个变量的值分别赋予了两个字符串
@RequestMapping(value="/addaddressbook",method=RequestMethod.POST)
private Map<String,Object> addAddressbook(@RequestBody Addressbook addressbook){
Map<String,Object> modelMap = new HashMap<>();
modelMap.put("success",addressbookService.addAddressbook(addressbook));
//返回: success:true
return modelMap;
}
@RequestMapping(value = "/modifyaddressbook",method=RequestMethod.POST)
private Map<String,Object> modifyAddressbook(@RequestBody Addressbook addressbook){
Map<String,Object> modelMap = new HashMap<>();
modelMap.put("success",addressbookService.modifyAddressbook(addressbook));
return modelMap;
}
@RequestMapping(value = "/removeaddressbook",method = RequestMethod.GET)//这里为了方便用GET请求
private Map<String,Object> removeAddressbook(Integer id){
Map<String,Object> modelMap = new HashMap<>();
modelMap.put("sucess",addressbookService.deleteAddressbook(id));
return modelMap;
}
}
网友评论