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

Stella981
• 阅读 881

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

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

这里是项目的结构目录

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

首先是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: 8080 logging: level: learning: trace spring: #数据源配置 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分页
代码如下

@Configuration public 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(); } @Service public 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加载的模板
SpringBoot 使用yml配置 mybatis+pagehelper+druid+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 

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

启动类代码

@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 方法即可

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

日志信息
SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例

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

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

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
待兔 待兔
4个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Wesley13 Wesley13
3年前
java反射, 不看你可别后悔
<divid"content\_views"class"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,
Easter79 Easter79
3年前
springboot下静态资源的处理(转)
<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divstyle"whitespace
Wesley13 Wesley13
3年前
java图形验证码生成工具类及web页面校验验证码
<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divclass"htmledit\_vi
Stella981 Stella981
3年前
AndroidStudio封装SDK的那些事
<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael
Stella981 Stella981
3年前
Mycat入门教程
<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divclass"markdown\_vi
Wesley13 Wesley13
3年前
Java8中的LocalDateTime工具类
<divid"content\_views"class"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,
Stella981 Stella981
3年前
Spring Boot MyBatis 连接数据库
<divid"article\_content"class"article\_contenttrackingad"datamod"popu\_307"datadsm"post"style"overflow:hidden;"<divclass"markdown\_views"<p最近比较忙,没来得及抽时间把MyBati
Wesley13 Wesley13
3年前
JAVA中this用法小结
<divid"article\_content"class"article\_contentcsdntrackingstatisticstrackingclick"datamod"popu\_519"datadsm"post"style"overflow:hidden;"<divclass"htmledit\_vi