您的位置:首页 > 编程语言 > Java开发

eclipse 使用maven 构建springbooot+mysql +freemarker 简单web 项目

2017-04-01 21:43 1041 查看
本文参考:http://www.bysocket.com/?p=1666/


一、Apache Freemarker 简介

Apache FreeMarker模板引擎:Java库来生成文本输出(HTML网页,电子邮件,配置文件,源代码,等等)基于模板和变化的数据。模板都写在FreeMarker模板语言(FTL),这是一个简单的、专门的语言(不是一个成熟的编程语言(比如PHP)。你要准备的数据显示在一个真正的编程语言,就像数据库查询和做生意的计算问题,然后模板已经准备好的数据的显示。模板中你关注如何呈现数据,和外部的模板你关注哪些数据。



这种方法通常被称为MVC(模型-视图-控制器)模式,并为动态网页特别受欢迎。它有助于分离网页设计师(HTML作者)从开发者(Java程序员通常)。在模板设计者不会面临复杂的逻辑,可以改变页面的外观无需程序员修改或重新编译代码。


二、运行 springboot-freemarker 工程

1.数据库准备

a.创建数据库 master:

CREATE DATABASE master;
b.创建表 city :

/*
Navicat MySQL Data Transfer

Source Server         : 本地数据库
Source Server Version : 50027
Source Host           : localhost:3306
Source Database       : master

Target Server Type    : MYSQL
Target Server Version : 50027
File Encoding         : 65001

Date: 2017-04-01 21:35:35
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for city
-- ----------------------------
DROP TABLE IF EXISTS `city`;
CREATE TABLE `city` (
`id` bigint(20) NOT NULL auto_increment,
`province_id` bigint(20) default NULL,
`city_name` varchar(128) default NULL,
`description` varchar(128) default NULL,
PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


2. 项目结构介绍

项目结构如下图所示:



1、springboot-freemarker 的pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.zzg</groupId>
<artifactId>springboot-freemarker</artifactId>
<version>0.0.1-SNAPSHOT</version>

<!-- Spring Boot 启动父依赖 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.1.RELEASE</version>
</parent>

<properties>
<mybatis-spring-boot>1.2.0</mybatis-spring-boot>
<mysql-connector>5.1.39</mysql-connector>
</properties>

<dependencies>
<!-- Spring Boot Freemarker 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Boot Test 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Spring Boot Mybatis 依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>${mybatis-spring-boot}</version>
</dependency>

<!-- MySQL 连接驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql-connector}</version>
</dependency>

<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
</project>


2、springboot-freemarker 相关资源文件

application.properties
## \u6570\u636E\u6E90\u914D\u7F6E
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/master?useUnicode=true&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

## Mybatis \u914D\u7F6E
mybatis.typeAliasesPackage=org.spring.springboot.domain
mybatis.mapperLocations=classpath:mapper/*.xml

## Freemarker \u914D\u7F6E
## \u6587\u4EF6\u914D\u7F6E\u8DEF\u5F84
spring.freemarker.template-loader-path=classpath:/web/
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

/web/city.ftl

<!DOCTYPE html>

<html lang="en">

<body>
City: ${city.cityName}! <br>
Q:Why I like? <br>
A:${city.description}!
</body>

</html>


/web/cityList.ftl

<!DOCTYPE html>

<html lang="en">

<body>
<#list cityList as city>

City: ${city.cityName}! <br>
Q:Why I like? <br>
A:${city.description}!

</#list>
</body>

</html>


/mapper/CityMapper.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.spring.springboot.dao.CityDao">
<resultMap id="BaseResultMap" type="com.spring.springboot.domain.City">
<result column="id" property="id" />
<result column="province_id" property="provinceId" />
<result column="city_name" property="cityName" />
<result column="description" property="description" />
</resultMap>

<parameterMap id="City" type="com.spring.springboot.domain.City"/>

<sql id="Base_Column_List">
id, province_id, city_name, description
</sql>

<select id="findById" resultMap="BaseResultMap" parameterType="java.lang.Long">
select
<include refid="Base_Column_List" />
from city
where id = #{id}
</select>

<select id="findAllCity" resultMap="BaseResultMap" >
select
<include refid="Base_Column_List" />
from city
</select>

<insert id="saveCity" parameterMap="City" useGeneratedKeys="true" keyProperty="id">
insert into
city(id,province_id,city_name,description)
values
(#{id},#{provinceId},#{cityName},#{description})
</insert>

<update id="updateCity" parameterMap="City">
update
city
set
<if test="provinceId!=null">
province_id = #{provinceId},
</if>
<if test="cityName!=null">
city_name = #{cityName},
</if>
<if test="description!=null">
description = #{description}
</if>
where
id = #{id}
</update>

<delete id="deleteCity" parameterType="java.lang.Long">
delete from
city
where
id = #{id}
</delete>
</mapper>


3、springboot-freemarker 源代码:

package com.spring.springboot.dao;

import java.util.List;

import org.apache.ibatis.annotations.Param;

import com.spring.springboot.domain.City;

/**
* 城市 DAO 接口类
*
* Created by zzg on 01/04/2017.
*/
public interface CityDao {

/**
* 获取城市信息列表
*
* @return
*/
List<City> findAllCity();

/**
* 根据城市 ID,获取城市信息
*
* @param id
* @return
*/
City findById(@Param("id") Long id);

Long saveCity(City city);

Long updateCity(City city);

Long deleteCity(Long id);
}

package com.spring.springboot.domain;

public class City implements java.io.Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

/**
* 城市编号
*/
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;
}

}

package com.spring.springboot.server.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.springboot.dao.CityDao;
import com.spring.springboot.domain.City;
import com.spring.springboot.server.CityService;

/**
* 城市业务逻辑实现类
*
* Created by zzg on 01/04/2017.
*/
@Service
public class CityServiceImpl implements CityService {

@Autowired
private CityDao cityDao;

public List<City> 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);
}

}

package com.spring.springboot.server;

import java.util.List;

import com.spring.springboot.domain.City;

/**
* 城市业务逻辑接口类
*
* Created by zzg on 01/04/2017.
*/
public interface CityService {

/**
* 获取城市信息列表
*
* @return
*/
List<City> 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);
}

package com.spring.springboot.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.spring.springboot.domain.City;
import com.spring.springboot.server.CityService;

/**
* 城市 Controller 实现 Restful HTTP 服务
* <p>
* Created by zzg on 01/04/2017.
*/
@Controller
public class CityController {

@Autowired
private CityService cityService;

@RequestMapping(value = "/api/city/{id}", method = RequestMethod.GET)
public String findOneCity(Model model, @PathVariable("id") Long id) {
model.addAttribute("city", cityService.findCityById(id));
return "city";
}

@RequestMapping(value = "/api/city", method = RequestMethod.GET)
public String findAllCity(Model model) {
List<City> cityList = cityService.findAllCity();
model.addAttribute("cityList",cityList);
return "cityList";
}
}

package com.spring.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* Spring Boot 应用启动类
*
* Created by zzg on 01/04/2017.
*/
// Spring Boot 应用的标识
@SpringBootApplication
// mapper 接口类扫描包配置
@MapperScan("com.spring.springboot.dao")
public class Application {

public static void main(String[] args) {
// 程序启动入口
// 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
SpringApplication.run(Application.class,args);
}
}


4、项目运行截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: