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

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

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

时间: 2024-10-25 08:03:03

SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例的相关文章

SpringBoot五步配置Mybatis

第一步:Maven里面添加mybatis的引用jar包: <!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <depen

springboot用yml配置druid

spring: datasource: driver-class-name: com.mysql.jdbc.Driver type: com.alibaba.druid.pool.DruidDataSource filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: timeBetweenEvictionRunsMillis: 60000 minEvictionRunsMillis: 300000 validation

springboot配置数据库连接池druid、整合mybatis、整合pagehelper

springboot配置数据库连接池druid druid所需pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>o

SpringBoot学习--04SpringBoot整合Mybatis(上)(配置mybatis generator,PageHelper)

陆陆续续又忙了几天,继续写. 本篇仿照着优秀的文章的书写,加上自己的理解和踩过的坑,原文地址:https://www.jianshu.com/p/5cd772c07041?utm_campaign=haruki&utm_content=note&utm_medium=reader_share&utm_source=weixin 环境/版本一览: 开发工具:eclipse springboot: 2.0.1.RELEASE jdk:1.8.0_40 maven:3.3.9 额外功能:

SpringBoot入门之基于Druid配置Mybatis多数据源

上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,SpringBoot就会基于约定根据配置信息实例化对象,但是一般大型的项目都是有多个子系统或者多个数据源组成,那怎么使用SpringBoot进行Mybatis多数据源配置呢? 一.数据库准备 我们这里准备使用主从两个数据库来进行演示多数据源配置.一个主库用来写write,一个从库用来读read.至于两个

Spring Boot + MyBatis + Pagehelper 配置多数据源

前言: 本文为springboot结合mybatis配置多数据源,在项目当中很多情况是使用主从数据源来读写分离,还有就是操作多库,本文介绍如何一个项目同时使用2个数据源. 也希望大家带着思考去学习!博主是最近才学的配置写成博文分享心得和技巧,文中有不足的欢迎留言指正,谢谢! 思考: 1.如果从传统的单数据源转换为多数据源,以前使用boot只用导包写配置文件boot会帮我们自动配置,如果不用自动配置我们改怎么配呢? 2.怎么结合mybatis分页插件一起使用呢? .................

Springboot集成Mybatis+PageHelper

1.Springboot项目引入mysql和mybatis的依赖: <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <gr

springboot整合drui、mybatis、pagehelper

springboot配置数据连接池druid Druid是阿里巴巴开发的号称为监控而生的数据库连接池,Druid是目前最好的数据库连接池. 在功能.性能.扩展性方面,都超过其他数据库连接池,同时加入了日志监控, 可以很好的监控DB池连接和SQL的执行情况 配置pom依赖 <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactI

SpringBoot学习笔记(1):配置Mybatis

SpringBoot学习笔记(1):配置Mybatis 参考资料: 1.AndyLizh的博客 2.xiaolyuh123的博客 快速开始 添加Mybatis依赖(其他依赖已省去) <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId