spring boot+mybatis+freemarker

您所在的位置:网站首页 freemarker配置文件 spring boot+mybatis+freemarker

spring boot+mybatis+freemarker

#spring boot+mybatis+freemarker| 来源: 网络整理| 查看: 265

之前写过一篇spring boot简单入门介绍,没想到访问量这么多,今天我就继续了! 不说废话,上代码! application.properties配置文件

logging.level.com.peng.demo=debug logging.level.org.springframework=debug server.port= spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://:/springbootdb?useUnicode=true&characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.beetl.cofig = classpath:beetl.properties spring.freemarker.suffix=.ftl spring.freemarker.templateEncoding=UTF- spring.freemarker.templateLoaderPath=classpath:/templates/ spring.freemarker.content-type=text/html

controller

@Controller public class BeetlController { @GetMapping(value = "/beetl") public String test(Model model){ Map user=new HashMap(); user.put("id", ); user.put("name", "曹操"); user.put("description", "一代枭雄"); model.addAttribute("user", user); return "index"; } } package com.peng.demo.controller; import com.peng.demo.domain.City; import com.peng.demo.service.CityService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.List; @Controller public class CityRestController { @Autowired private CityService cityService; @GetMapping(value = "/api/city")//代替requestMapping method get public String findOneCity(@RequestParam(value = "cityName", required = true) String cityName, Model model) { model.addAttribute("city",cityService.findCityByName(cityName)); return "city"; } @RequestMapping(value = "/api/cityLei",method = RequestMethod.GET) public List findCityByName(String name){ return cityService.findCityLikeName(name); } @RequestMapping(value = "/api/addCity",method = RequestMethod.GET) public void add(){ City city1=new City(L,"sdsd","sadsads"); City city2=new City(L,"sdsd","sadsads"); City city3=new City(L,"sdsd","sadsads"); City city4=new City(L,"sdsd","sadsads"); cityService.addCity(city1); cityService.addCity(city2); cityService.addCity(city3); cityService.addCity(city4); } @RequestMapping(value = "/api/hello",method = RequestMethod.GET) public String hello(){ return "hello"; } } package com.peng.demo; import org.beetl.core.resource.WebAppResourceLoader; import org.beetl.ext.spring.BeetlGroupUtilConfiguration; import org.beetl.ext.spring.BeetlSpringViewResolver; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.core.io.DefaultResourceLoader; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternUtils; import java.io.IOException; @SpringBootApplication @ComponentScan(basePackages = {"com.peng.demo.controller"}) public class SpringbootLearningApplication { public static void main(String[] args) { SpringApplication.run(SpringbootLearningApplication.class, args); } @Bean(initMethod = "init", name = "beetlConfig") public BeetlGroupUtilConfiguration getBeetlGroupUtilConfiguration() { BeetlGroupUtilConfiguration beetlGroupUtilConfiguration = new BeetlGroupUtilConfiguration(); ResourcePatternResolver patternResolver = ResourcePatternUtils.getResourcePatternResolver(new DefaultResourceLoader()); try { // WebAppResourceLoader 配置root路径是关键 WebAppResourceLoader webAppResourceLoader = new WebAppResourceLoader(patternResolver.getResource("classpath:/templates").getFile().getPath()); beetlGroupUtilConfiguration.setResourceLoader(webAppResourceLoader); } catch (IOException e) { e.printStackTrace(); } //读取配置文件信息 return beetlGroupUtilConfiguration; } @Bean(name = "beetlViewResolver") public BeetlSpringViewResolver getBeetlSpringViewResolver(@Qualifier("beetlConfig") BeetlGroupUtilConfiguration beetlGroupUtilConfiguration) { BeetlSpringViewResolver beetlSpringViewResolver = new BeetlSpringViewResolver(); beetlSpringViewResolver.setPrefix("/"); beetlSpringViewResolver.setSuffix(".html"); beetlSpringViewResolver.setContentType("text/html;"); beetlSpringViewResolver.setOrder(); beetlSpringViewResolver.setConfig(beetlGroupUtilConfiguration); return beetlSpringViewResolver; } }

mybatis使用注解

package com.peng.demo.dao; import com.peng.demo.domain.City; import org.apache.ibatis.annotations.*; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.List; /** * 城市 DAO 接口类 * * Created by xchunzhao on 02/05/2017. */ @Component @Mapper // 标志为 Mybatis 的 Mapper public interface CityDao { /** * 根据城市名称,查询城市信息 * * @param cityName 城市名 */ @Select("SELECT * FROM city where city_name=#{cityName}") // 返回 Map 结果集 @Results({//property为javabean,comum为数据库字段 @Result(property = "id", column = "id"), @Result(property = "provinceId", column = "province_id"), @Result(property = "cityName", column = "city_name"), @Result(property = "description", column = "description"), }) City findByName(@Param("cityName") String cityName); /** * * @param name 模糊查询 concat对字符串进行拼接 * @return */ @Select("select * from city where city_name like concat((#{cityName}),'%')") @Results({ @Result(property = "id", column = "id"), @Result(property = "provinceId", column = "province_id"), @Result(property = "cityName", column = "city_name"), @Result(property = "description", column = "description"), }) List findLikeName(@Param("cityName") String name); /** * * @param cityName 根据名字进行删除 */ @Delete("delete from city where city_name=#{cityName}") void deleteByName(String cityName); @Insert("insert into city(province_id,city_name,description) values(#{provinceId},#{cityName},#{description})") void add(City city); @Update("update city province_id=#{provinceId},city_name=#{cityName},description=#{description} where id=#{id}") void update(City city); } package com.peng.demo.domain; import java.io.Serializable; /** * 城市实体类 * * Created by xchunzhao on 02/05/2017. */ public class City implements Serializable { private static final long serialVersionUID = -L; /** * 城市编号 */ private Long id; /** * 省份编号 */ private Long provinceId; /** * 城市名称 */ private String cityName; /** * 描述 */ private String description; public City(Long provinceId, String cityName, String description) { this.provinceId = provinceId; this.cityName = cityName; this.description = description; } public City(){ } 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; } }

页面

beetl servlet 这是一个beetl servlet例子 ${user.id} ${user.name} ${user.description} City: ${city.cityName}! Q:Why I like? A:${city.description}!

mybatis的注解和freeamarker是前端模板 spring boot默认的页面路径在resource/templete下 就是这些,大家一起努力吧! !!



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3