美团前端引擎用freemarker多些
毕设打算用spring boot + freemarker + mybatis + mysql 做
因此提前做一下技术调研和整合,自己实操下
之前简单学习过一点spring boot,不够系统
也做过一些spring boot简单的增删改查
spring boot开发特别方便,很多东西不用配置(约定大于配置)
目前计划系统的学习下
先来个spring boot整合freemarker的小例子🌰
如果对spring boot已经挺熟的同学,请直接跳过本文,
这里参考了网上的一个小例子,自己做了一些改动和补充,mybatis基于注解开发 :
准备好数据库和表已经模拟几条数据即可
SQL语句:
CREATE TABLE `city` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT COMMENT '城市编号',
`province_id` int(10) unsigned NOT NULL COMMENT '省份编号',
`city_name` varchar(25) DEFAULT NULL COMMENT '城市名称',
`description` varchar(25) DEFAULT NULL COMMENT '描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
INSERT city(province_id,city_name,description) VALUES (3,'北京','互联网公司多机会多');
INSERT city(province_id,city_name,description) VALUES (4,'信阳','环境好,宜居');
INSERT city(province_id,city_name,description) VALUES (5,'厦门','海边,风景美');
现在要实现2个功能:
1,根据id查询城市
2,查询城市列表
创建spring boot项目:
下一步
选择依赖:
下一步
项目创建成功
添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
整个pom文件一览:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>pers.wmx</groupId>
<artifactId>springboot-freemarker-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-freemarker-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件application.properties:
## 数据源配置
spring.datasource.url=jdbc:mysql://ip:port/数据库名?useUnicode=true&characterEncoding=utf8
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
## Mybatis 配置
mybatis.typeAliasesPackage=pers.wmx.springbootfreemarkerdemo.domain
mybatis.configuration.mapUnderscoreToCamelCase=true
## Freemarker 配置
## 文件配置路径
spring.freemarker.template-loader-path=classpath:/templates/
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
驼峰命名转换是什么意思?
数据库的city_name
在java结果集对象中自动转换成驼峰命名参数 cityName
city表对应的实体类:
package pers.wmx.springbootfreemarkerdemo.domain;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
/**
* @author: wangmingxin02
* @create: 2019-03-26
**/
@Getter
@Setter
public class City {
Long id;
Long provinceId;
String cityName;
String description;
}
@Getter、@Setter可以省略get和set方法
项目启动类(程序入口):
@SpringBootApplication
public class SpringbootFreemarkerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootFreemarkerDemoApplication.class, args);
}
}
controller层:
package pers.wmx.springbootfreemarkerdemo.controller;
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 pers.wmx.springbootfreemarkerdemo.domain.City;
import pers.wmx.springbootfreemarkerdemo.service.ICityService;
import java.util.List;
/**
* @author: wangmingxin02
* @create: 2019-03-26
**/
@Controller
public class CityController {
@Autowired
ICityService cityService;
@RequestMapping("/api/city/{id}")
public String findCityById(Model model, @PathVariable("id") Long id){
City city = cityService.findById(id);
model.addAttribute("city",city);
return "index";
}
@RequestMapping("/api/city")
public String findCityList(Model model){
List<City> cityList = cityService.findCityList();
model.addAttribute("cityList",cityList);
return "citylist";
}
}
service层:
package pers.wmx.springbootfreemarkerdemo.service;
import pers.wmx.springbootfreemarkerdemo.domain.City;
import java.util.List;
public interface ICityService {
public City findById(Long id);
public List<City> findCityList();
}
package pers.wmx.springbootfreemarkerdemo.service.impl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pers.wmx.springbootfreemarkerdemo.dao.CityMapper;
import pers.wmx.springbootfreemarkerdemo.domain.City;
import pers.wmx.springbootfreemarkerdemo.service.ICityService;
import java.util.List;
/**
* @author: wangmingxin02
* @create: 2019-03-26
**/
@Service
public class CityServiceImpl implements ICityService {
@Autowired
CityMapper cityMapper;
@Override
public City findById(Long id) {
return cityMapper.findById(id);
}
@Override
public List<City> findCityList() {
return cityMapper.findCityList();
}
}
mapper层(mybatis基于注解开发):
package pers.wmx.springbootfreemarkerdemo.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import pers.wmx.springbootfreemarkerdemo.domain.City;
import java.util.List;
@Mapper
public interface CityMapper {
@Select("select * from city where id = #{id}")
public City findById(Long id);
@Select("select * from city")
public List<City> findCityList();
}
这样写特别方便
准备前端页面:
index.ftl
<!DOCTYPE html>
<html lang="en">
<body>
城市: ${city.cityName}! <br>
城市描述: ${city.description}!
</body>
</html>
citylist.ftl
<!DOCTYPE html>
<html lang="en">
<body>
<#list cityList as e>
城市: ${e.cityName}! <br>
城市描述:${e.description}!<br>
<hr/>
</#list>
</body>
</html>
启动项目
访问 http://localhost:8080/api/city/3
访问 http://localhost:8080/api/city
最后附上这个例子的代码:
https://github.com/xinyeshuaiqi/springboot-freemarker-demo
转载请注明:汪明鑫的个人博客 » spring boot 入门篇
说点什么
2 评论 在 "spring boot 入门篇"
[…] 以我之前那个spring boot整合freemarker的项目为例 […]
[…] spring boot 入门篇 […]