SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker實例
阿新 • • 發佈:2018-01-13
fontsize () mysq 分頁插件 findall req 實體 select ppp
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實例