介紹
從Spring Boot項目名稱中的Boot可以看出來,SpringBoot的作用在於創建和啟動新的基於Spring框架的項目,它的目的是幫助開發人員很容易的創建出獨立運行的產品和產品級別的基於Spring框架的應用。SpringBoot會選擇最適合的Spring子項目和第三方開源庫進行整合。大部分Spring Boot應用只需要非常少的配置就可以快速運行起來。
SpringBoot包含的特性
- 創建可以獨立運行的Spring應用
- 直接嵌入Tomcat或Jetty服務器,不需要部署WAR文件
- 提供推薦的基礎POM文件來簡化Apache Maven配置
- 盡可能的根據項目依賴來自動配置Spring框架
- 提供可以直接在成產環境中使用的功能,如性能指標,應用信息和應用健康檢查
- 沒有代碼生成,也沒有XML配置文件
SpringBoot與Mybatis的完美融合
首先得先創建一個SpringBoot項目,然后基於這個項目在融合進Mybatis
下面給出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>20171111</groupId> <artifactId>springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>springboot</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.2.RELEASE</version> <relativePath></relativePath> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.2.0.Final</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>net.sourceforge.nekohtml</groupId> <artifactId>nekohtml</artifactId> <version>1.9.22</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
之后創建一個啟動類,其實在配置SpringBoot項目的時候就已經創建過一個啟動類了,這里只是貼下代碼:
package springboot; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; @SpringBootApplication public class HelloworldDemoApplication implements EmbeddedServletContainerCustomizer{ public static void main(String[] args){ SpringApplication.run(HelloworldDemoApplication.class, args); } public void customize(ConfigurableEmbeddedServletContainer container) { // TODO �Զ����ɵķ������ container.setPort(8088); } }
然后,創建配置文件在resource根目錄下創建application.properties文件:
spring.thymeleaf.mode=LEGACYHTML5
spring.datasource.url=jdbc:mysql://10.0.20.252:3306/mybatis
spring.datasource.username=root
spring.datasource.password=Free-Wi11
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
server.port=8088
這里的server.port=8088是定義了該項目的端口,如果不指定,則使用默認端口8080
然后定義一個java的實體類,User.java
package com.fpc.Entity; public class User { private int id; private String name; private int age; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
這里實體類的字段要和數據庫的字段對應起來,如果實體類的字段和數據庫的字段不相同,則需要使用別名:
這里我們看下數據庫中的字段和數據:
mysql> select * from users;
+----+-----------+------+
| id | name | age |
+----+-----------+------+
| 1 | Lily | 24 |
| 2 | b | 27 |
| 5 | userrrrrr | 25 |
+----+-----------+------+
3 rows in set (0.00 sec)
之后,定義一個dao的接口:
package springboot.Mapper; import java.util.List; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import com.fpc.Entity.User; @Mapper public interface UserDao { @Select("select * from users where age=#{age}") public List<User> get(int age); }
@Mapper就是我們要與mybatis融合關鍵的一步,只要一個注解就搞定了。
然后再寫一個測試類,也就是Controller:
package springboot.Controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import com.fpc.Entity.User; import springboot.Mapper.UserDao; @Controller @RequestMapping("/b") public class UserController { @Autowired private UserDao userDao; @RequestMapping("/showDao/{age}") @ResponseBody public List<User> show(@PathVariable("age") int age) { // return userServices.show(); // return "hellllllllllllllllllllllll"; System.out.println(age); return userDao.get(age); } // @RequestMapping("/showDao") // public Object showDao(int age) { // return userServicesImpl.showDao(age); // } }
@RestController是對應的restful風格的控制器,但是我這里直接用的@Controller
運行該項目,Run as - Maven Build
打開瀏覽器,輸入:localhost:8088/b/showDao/27,運行的結果如下:

按名字查找年齡為27的user,如果直接在數據庫中查找也會得到一樣的結果:
mysql> select * from users where age=27;
+----+------+------+
| id | name | age |
+----+------+------+
| 2 | b | 27 |
+----+------+------+
1 row in set (0.00 sec)
總結
- Controller,Entity,Mapper都要在跟啟動類HelloworldDemoApplication在同一個目錄下,而且啟動類要在外層,項目目錄結構會在下面給出。
- 聲明一個UserDao的接口,在Mapper文件夾下,其中用注解的形式寫了SQL語句,而且要在接口的外面協商@Mapper(這是關鍵)
- 在Controller中,直接通過@Autowired將UserDao直接注入到Controller中,然后就可以在Controller中進行操作了。
項目工程目錄

