controller層的作用
阿新 • • 發佈:2018-11-01
以下從controller層開始:
package com.bootdo.common.controller; import com.bootdo.common.domain.City; import com.bootdo.common.domain.Province; import com.bootdo.common.service.SiteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.io.Serializable; import java.util.List; @RequestMapping("/site") @Controller public class SiteController implements Serializable{ private static final long serialVersionUID = 1L; @Autowired SiteService siteService; @GetMapping("/get_Province") @ResponseBody() public List<Province> initProvince() { return siteService.getAllProvince(); } @GetMapping("/get_city") @ResponseBody() public List<City> getCity(HttpServletRequest request) { String codeP = request.getParameter("codeP"); return siteService.getCity(codeP); } @GetMapping("/get_area") @ResponseBody() public List<Province> getArea(HttpServletRequest request) { String codeC = request.getParameter("codeC"); List<Province> list = siteService.getArea(codeC); return list; } }
在Java的開發中,controller層是連線前端和後端的。
package com.bootdo.common.service; import com.bootdo.common.domain.City; import com.bootdo.common.domain.Province; import org.springframework.stereotype.Service; import java.util.List; @Service public interface SiteService { List<Province> getAllProvince(); List<City> getCity(String codeP); List<Province> getArea(String codeC); }
接受前端發來的請求,同時向後端傳送請求,——>service 層——>serviceImpl實現service層,同時連線dao層,(在dao層中同樣是介面)——>通過dao層去實現對資料庫的操作——在XML檔案中通過namespace完成連線dao層。
package com.bootdo.common.service.impl; import com.bootdo.common.dao.AreaDao; import com.bootdo.common.dao.CityDao; import com.bootdo.common.dao.ProvinceDao; import com.bootdo.common.domain.City; import com.bootdo.common.domain.Province; import com.bootdo.common.service.SiteService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class SiteServiceImpl implements SiteService { @Autowired ProvinceDao provinceMapper; @Autowired CityDao cityMapper; @Autowired AreaDao areaMapper; @Override public List<Province> getAllProvince() { List<Province> list = provinceMapper.list(); return list; } @Override public List<City> getCity(String codeP) { return cityMapper.list(codeP); } @Override public List<Province> getArea(String codeC) { return areaMapper.list(codeC); } }
下邊是dao層程式碼:
package com.bootdo.common.dao;
import com.bootdo.common.domain.Province;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* 系統日誌
* @author chglee
* @email [email protected]
* @date 2017-10-03 15:45:42
*/
@Mapper
public interface AreaDao {
List<Province> list(String codeC);
}
最後就是操作資料庫了。
<?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.bootdo.common.dao.AreaDao">
<select id="list" resultType="com.bootdo.common.domain.Area" parameterType="java.lang.String">
select `id`,`code_a`,`name`,`code_c` from area WHERE code_c= #{codeC}
</select>
</mapper>
從這個裡邊的程式碼中可以看出來,對於不同的資料庫,不同的表操作,可以通過在serviceImple層來變換。然後寫不同的dao層和不同的XML檔案來操作。