From 039a866bdf1311a8f18f6de45494c7002c7bb1ca Mon Sep 17 00:00:00 2001 From: JeffLi1993 Date: Wed, 22 Feb 2017 20:20:41 +0800 Subject: [PATCH] =?UTF-8?q?Spring=20Boot=20=E9=9B=86=E6=88=90=20FreeMarker?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + pom.xml | 1 + .../org/spring/springboot/Application.java | 26 ++++++++ .../springboot/controller/CityController.java | 30 +++++++++ .../org/spring/springboot/dao/CityDao.java | 35 +++++++++++ .../org/spring/springboot/domain/City.java | 61 +++++++++++++++++++ .../springboot/service/CityService.java | 52 ++++++++++++++++ .../service/impl/CityServiceImpl.java | 45 ++++++++++++++ .../src/main/resources/application.properties | 21 +++++++ .../src/main/resources/city/city.ftl | 11 ++++ .../src/main/resources/mapper/CityMapper.xml | 60 ++++++++++++++++++ 11 files changed, 343 insertions(+) create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/Application.java create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/controller/CityController.java create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/dao/CityDao.java create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/domain/City.java create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/service/CityService.java create mode 100644 springboot-freemarker/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java create mode 100644 springboot-freemarker/src/main/resources/application.properties create mode 100644 springboot-freemarker/src/main/resources/city/city.ftl create mode 100644 springboot-freemarker/src/main/resources/mapper/CityMapper.xml diff --git a/README.md b/README.md index fe66c8d..56aa68d 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ springboot 版本 1.5.1.RELEASE [《Springboot 实现 Restful 服务,基于 HTTP / JSON 传输》](http://www.bysocket.com/?p=1627 "Springboot 实现 Restful 服务,基于 HTTP / JSON 传输")
- Spring Boot 集成 FreeMarker
Spring Boot 集成 FreeMarker 及使用 taglib +- Spring Boot 集成 Dubbo
## 二、项目 Quick Start 快速开发指南 #### a. 基本环境配置 diff --git a/pom.xml b/pom.xml index 1295e05..7a46ea1 100644 --- a/pom.xml +++ b/pom.xml @@ -12,6 +12,7 @@ springboot-helloworld springboot-mybatis springboot-restful + springboot-freemarker \ No newline at end of file diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/Application.java b/springboot-freemarker/src/main/java/org/spring/springboot/Application.java new file mode 100644 index 0000000..79807ed --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/Application.java @@ -0,0 +1,26 @@ +package org.spring.springboot; + +import org.mybatis.spring.annotation.MapperScan; +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * Spring Boot 应用启动类 + * + * Created by bysocket on 16/4/26. + */ +// Spring Boot 应用的标识 +@SpringBootApplication +// mapper 接口类扫描包配置 +@MapperScan("org.spring.springboot.dao") +public class Application { + + public static void main(String[] args) { + // 程序启动入口 + // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件 + SpringApplication.run(Application.class,args); + } +} diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/controller/CityController.java b/springboot-freemarker/src/main/java/org/spring/springboot/controller/CityController.java new file mode 100644 index 0000000..577aad6 --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/controller/CityController.java @@ -0,0 +1,30 @@ +package org.spring.springboot.controller; + +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; +import org.springframework.ui.ModelMap; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * 城市 Controller 实现 Restful HTTP 服务 + *

+ * Created by bysocket on 07/02/2017. + */ +@Controller +public class CityController { + + @Autowired + private CityService cityService; + + @RequestMapping(value = "/api/city") + public String findOneCity(Model model, @RequestParam(value="id", required=false, defaultValue="1") Long id) { + model.addAttribute("city", cityService.findCityById(id)); + return "city"; + } +} diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/dao/CityDao.java b/springboot-freemarker/src/main/java/org/spring/springboot/dao/CityDao.java new file mode 100644 index 0000000..22f2654 --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/dao/CityDao.java @@ -0,0 +1,35 @@ +package org.spring.springboot.dao; + +import org.apache.ibatis.annotations.Param; +import org.spring.springboot.domain.City; + +import java.util.List; + +/** + * 城市 DAO 接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityDao { + + /** + * 获取城市信息列表 + * + * @return + */ + List findAllCity(); + + /** + * 根据城市 ID,获取城市信息 + * + * @param id + * @return + */ + City findById(@Param("id") Long id); + + Long saveCity(City city); + + Long updateCity(City city); + + Long deleteCity(Long id); +} diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/domain/City.java b/springboot-freemarker/src/main/java/org/spring/springboot/domain/City.java new file mode 100644 index 0000000..556709c --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/domain/City.java @@ -0,0 +1,61 @@ +package org.spring.springboot.domain; + +/** + * 城市实体类 + * + * Created by bysocket on 07/02/2017. + */ +public class City { + + /** + * 城市编号 + */ + private Long id; + + /** + * 省份编号 + */ + private Long provinceId; + + /** + * 城市名称 + */ + private String cityName; + + /** + * 描述 + */ + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Long getProvinceId() { + return provinceId; + } + + public void setProvinceId(Long provinceId) { + this.provinceId = provinceId; + } + + public String getCityName() { + return cityName; + } + + public void setCityName(String cityName) { + this.cityName = cityName; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } +} diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/service/CityService.java b/springboot-freemarker/src/main/java/org/spring/springboot/service/CityService.java new file mode 100644 index 0000000..b97ba1e --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/service/CityService.java @@ -0,0 +1,52 @@ +package org.spring.springboot.service; + +import org.spring.springboot.domain.City; + +import java.util.List; + +/** + * 城市业务逻辑接口类 + * + * Created by bysocket on 07/02/2017. + */ +public interface CityService { + + /** + * 获取城市信息列表 + * + * @return + */ + List findAllCity(); + + /** + * 根据城市 ID,查询城市信息 + * + * @param id + * @return + */ + City findCityById(Long id); + + /** + * 新增城市信息 + * + * @param city + * @return + */ + Long saveCity(City city); + + /** + * 更新城市信息 + * + * @param city + * @return + */ + Long updateCity(City city); + + /** + * 根据城市 ID,删除城市信息 + * + * @param id + * @return + */ + Long deleteCity(Long id); +} diff --git a/springboot-freemarker/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java b/springboot-freemarker/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java new file mode 100644 index 0000000..8140ecf --- /dev/null +++ b/springboot-freemarker/src/main/java/org/spring/springboot/service/impl/CityServiceImpl.java @@ -0,0 +1,45 @@ +package org.spring.springboot.service.impl; + +import org.spring.springboot.dao.CityDao; +import org.spring.springboot.domain.City; +import org.spring.springboot.service.CityService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * 城市业务逻辑实现类 + * + * Created by bysocket on 07/02/2017. + */ +@Service +public class CityServiceImpl implements CityService { + + @Autowired + private CityDao cityDao; + + public List findAllCity(){ + return cityDao.findAllCity(); + } + + public City findCityById(Long id) { + return cityDao.findById(id); + } + + @Override + public Long saveCity(City city) { + return cityDao.saveCity(city); + } + + @Override + public Long updateCity(City city) { + return cityDao.updateCity(city); + } + + @Override + public Long deleteCity(Long id) { + return cityDao.deleteCity(id); + } + +} diff --git a/springboot-freemarker/src/main/resources/application.properties b/springboot-freemarker/src/main/resources/application.properties new file mode 100644 index 0000000..9208814 --- /dev/null +++ b/springboot-freemarker/src/main/resources/application.properties @@ -0,0 +1,21 @@ +## 数据源配置 +spring.datasource.url=jdbc:mysql://localhost:3306/springbootdb?useUnicode=true&characterEncoding=utf8 +spring.datasource.username=root +spring.datasource.password=123456 +spring.datasource.driver-class-name=com.mysql.jdbc.Driver + +## Mybatis 配置 +mybatis.typeAliasesPackage=org.spring.springboot.domain +mybatis.mapperLocations=classpath:mapper/*.xml + +## Freemarker 配置 +## 文件配置路径 +spring.freemarker.template-loader-path=classpath:/city/ +spring.freemarker.cache=false +spring.freemarker.charset=UTF-8 +spring.freemarker.check-template-location=true +spring.freemarker.content-type=text/html +spring.freemarker.expose-request-attributes=true +spring.freemarker.expose-session-attributes=true +spring.freemarker.request-context-attribute=request +spring.freemarker.suffix=.ftl \ No newline at end of file diff --git a/springboot-freemarker/src/main/resources/city/city.ftl b/springboot-freemarker/src/main/resources/city/city.ftl new file mode 100644 index 0000000..22bf699 --- /dev/null +++ b/springboot-freemarker/src/main/resources/city/city.ftl @@ -0,0 +1,11 @@ + + + + + +City: ${city.cityName}!
+Q:Why I like?
+A:${city.description}! + + + \ No newline at end of file diff --git a/springboot-freemarker/src/main/resources/mapper/CityMapper.xml b/springboot-freemarker/src/main/resources/mapper/CityMapper.xml new file mode 100644 index 0000000..1855f82 --- /dev/null +++ b/springboot-freemarker/src/main/resources/mapper/CityMapper.xml @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + id, province_id, city_name, description + + + + + + + + insert into + city(id,province_id,city_name,description) + values + (#{id},#{provinceId},#{cityName},#{description}) + + + + update + city + set + + province_id = #{provinceId}, + + + city_name = #{cityName}, + + + description = #{description} + + where + id = #{id} + + + + delete from + city + where + id = #{id} + +