spring boot+mybatis+freemarker

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

logging.level.com.peng.demo=debug
logging.level.org.springframework=debug
server.port=8990

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/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-8
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<String,Object> user=new HashMap<>();
        user.put("id", 1);
        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<City> findCityByName(String name){
        return cityService.findCityLikeName(name);
    }
    @RequestMapping(value = "/api/addCity",method = RequestMethod.GET)
    public void add(){
        City city1=new City(2L,"sdsd","sadsads");
        City city2=new City(2L,"sdsd","sadsads");
        City city3=new City(2L,"sdsd","sadsads");
        City city4=new City(2L,"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;charset=UTF-8");
        beetlSpringViewResolver.setOrder(0);
        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<City> 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 = -1L;

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

页面

<html>
<head>
    <title>beetl servlet</title>
</head>
<body>
这是一个beetl servlet例子

${user.id}
${user.name}
${user.description}
</body>

</html>
<!DOCTYPE html>

<html lang="en">

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

</html>

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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值