码迷,mamicode.com
首页 > 编程语言 > 详细

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

时间:2018-01-13 18:42:21      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:javadoc   water   autowired   xsd   logging   www   roo   sql   描述   

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

这是一个简单的SpringBoot整合实例

这里是项目的结构目录

技术分享图片

首先是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>Ming</groupId>  <artifactId>SpringBoot</artifactId>  <version>0.0.1-SNAPSHOT</version>   <!-- Spring Boot 启动父依赖 -->  <parent>      <groupId>org.springframework.boot</groupId>      <artifactId>spring-boot-starter-parent</artifactId>      <version>1.5.4.RELEASE</version>  </parent>  <properties>      <mybatis-spring-boot>1.2.0</mybatis-spring-boot>      <mysql-connector>5.1.39</mysql-connector>      <druid>1.0.18</druid>      <java.version>1.8</java.version>  </properties>  <dependencies>       <!-- Spring Boot Web 依赖 -->       <dependency>           <groupId>org.springframework.boot</groupId>           <artifactId>spring-boot-starter-web</artifactId>       </dependency>       <!-- Spring Boot Mybatis 依赖 -->       <dependency>           <groupId>org.mybatis.spring.boot</groupId>           <artifactId>mybatis-spring-boot-starter</artifactId>           <version>1.1.1</version>       </dependency>       <!-- MySQL 连接驱动依赖 -->       <dependency>           <groupId>mysql</groupId>           <artifactId>mysql-connector-java</artifactId>           <version>${mysql-connector}</version>       </dependency>       <!-- Druid 数据连接池依赖 -->       <dependency>           <groupId>com.alibaba</groupId>           <artifactId>druid</artifactId>           <version>${druid}</version>       </dependency>       <!-- 分页插件 -->         <dependency>             <groupId>com.github.pagehelper</groupId>             <artifactId>pagehelper</artifactId>             <version>4.1.6</version>         </dependency>         <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-freemarker</artifactId>       </dependency>           </dependencies></project>

接着在src/main/resources目录下新建一个application.yml文件

server:  port: 8080logging:  level:     learning: tracespring:  #数据源配置  datasource:    url: jdbc:mysql://localhost:3306/test    username: root    password: 123456    driver-class-name: com.mysql.jdbc.Driver    type: com.alibaba.druid.pool.DruidDataSource    connectionProperties: druid.stat.mergeSql=true  #模板引擎  freemarker:    #关闭缓存    cache: false    request-context-attribute: request    #模板加载的位置    template-loader-path: classpath:/templates    #前缀    suffix: .htm    #后缀    prefix: /htm/mybatis:  #实体类所做包  type-aliases-package: learning.model  #mapper.xml所在位置  mapper-locations: classpath:mappers/*.xml

在conf包下新建一个MybatisConfig类以配置使用PageHelper分页
代码如下

@Configurationpublic class MybatisConfig {    @Bean    public PageHelper pageHelper(){        PageHelper pageHelper = new PageHelper();          //添加配置,也可以指定文件路径        Properties p = new Properties();        p.setProperty("offsetAsPageNum", "true");        p.setProperty("rowBoundsWithCount", "true");        p.setProperty("reasonable", "true");        pageHelper.setProperties(p);        return pageHelper;    }}

接着就可以编写实例类

public class Player {    private Integer id;    private String name;    private Double points;    public Integer getId() {        return id;    }    public void setId(Integer id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Double getPoints() {        return points;    }    public void setPoints(Double points) {        this.points = points;    }}

dao接口

public interface PlayerDao {    void save (Player player);    Player findByKey(Integer id);    List<Player> findAll();}

service接口以及实现

public interface PlayerService {    void add(Player player);    List<Player> getAll();}@Servicepublic class PlayerServiceImpl implements PlayerService{    @Autowired    private PlayerDao playerDao;    @Override    public void add(Player player) {        playerDao.save(player);    }    @Override    public List<Player> getAll() {        PageHelper.startPage(1, 2);        return playerDao.findAll();    }}

在src/main/resources 下新建一个包mapppers用来存放mybatis的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="learning.mapper.PlayerDao">    <insert id="save">        INSERT INTO player (name,points) VALUES (#{name},#{points})    </insert>       <select id="findByKey" resultType="learning.model.Player">        SELECT * FROM player WHERE id = #{id}    </select>    <select id="findAll" resultType="learning.model.Player">        SELECT * FROM player    </select>   </mapper>

在src/main/resources在新建一个目录templates用来存放freemarker加载的模板
技术分享图片

随意编写一个freemarker 的模板html界面

<!DOCTYPE HTML><html><head></head><body>    <h3>success</h3>    <#list list as player>        ${player.name} : ${player.points} <br/>    </#list></body></html>

然后编写控制层代码

@Controller@RequestMapping("/Test")public class TestController {    @Autowired    private PlayerService playerService;    @RequestMapping("/getAll")    @ResponseBody    public Object getAll(){        Player player = new Player();        player.setName("杜兰特");        player.setPoints(28.4);        player.setId(3);        return player;                  }    @RequestMapping("/all")    @ResponseBody    public Object get(){        return playerService.getAll();    }    @RequestMapping("/add")    public String add(Player player){        playerService.add(player);        return "test/hello";    }    @RequestMapping("/hello")    public String hello(Model model) {        List<Player> list = playerService.getAll();        model.addAttribute("list",list);        return "test/hello";    }
最后编写项目的启动代码主要启动类的路径要在其他要扫描包的同级目录如图AppStart

技术分享图片

启动类代码

@SpringBootApplication//这里是扫描dao接口的包用于识别mybatis@MapperScan(basePackages="learning.mapper")public class AppStart {    /**     * @param args     */    public static void main(String[] args) {        SpringApplication.run(AppStart.class, args);    }}

最后启动AppStart main 方法即可

技术分享图片

日志信息
技术分享图片

这里是我的项目代码
http://download.csdn.net/download/cmmchenmm/9897156

我将代码上传到了GitHub上了
https://github.com/newShiJ/SpringBoot-Learing

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

标签:javadoc   water   autowired   xsd   logging   www   roo   sql   描述   

原文地址:https://www.cnblogs.com/jpfss/p/8279663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!