簡介
從 Spring Boot 項目名稱中的 Boot 可以看出來,Spring Boot 的作用在于創建和啟動新的基于 Spring 框架的項目。它的目的是幫助開發人員很容易的創建出獨立運行和產品級別的基于 Spring 框架的應用。Spring Boot 會選擇最適合的 Spring 子項目和第三方開源庫進行整合。大部分 Spring Boot 應用只需要非常少的配置就可以快速運行起來。
Spring Boot 包含的特性如下:
創建可以獨立運行的 Spring 應用。
直接嵌入 Tomcat 或 Jetty 服務器,不需要部署 WAR 文件。
提供推薦的基礎 POM 文件來簡化 Apache Maven 配置。
盡可能的根據項目依賴來自動配置 Spring 框架。
提供可以直接在生產環境中使用的功能,如性能指標、應用信息和應用健康檢查。
沒有代碼生成,也沒有 XML 配置文件。
好了,上面說這么多都是給下文做鋪墊,感興趣的朋友繼續往下閱讀吧。
大家都知道springboot搭建一個spring框架只需要秒秒鐘。
下面給大家介紹一下springboot與mybatis的完美融合:
首先:創建一個名為springboot-mybatis的maven項目,記住:一定要maven哦,不懂maven的可以自己惡補一下maven知識,這里就不介紹maven了。
下面給出pom.xml的完整配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
|
<?xml version= "1.0" encoding= "UTF-8" ?> <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>springboot-mybatis</groupId> <artifactId>springboot-mybatis</artifactId> <version> 1.0 . 0 </version> <packaging>war</packaging> <name>springBoot-mybatis</name> <description>Spring Boot project</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version> 1.3 . 2 .RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF- 8 </project.build.sourceEncoding> <java.version> 1.8 </java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version> 1.1 . 1 </version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version> 5.1 . 21 </version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <skip> true </skip> </configuration> </plugin> </plugins> </build> </project> |
之后創建一個啟動類:
1
2
3
4
5
6
7
8
9
10
11
12
|
package org.shenlan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; /** * Created by wangwei on 2016/9/2. */ @SpringBootApplication public class Application { public static void main(String[] args){ SpringApplication.run(Application. class ,args); } } |
這樣一個完整的springboot項目就完成了,是不是很簡單。
接下來就可以整理與mybatis的東東了。
首先,創建配置文件:application.properties
1
2
3
4
5
|
spring.datasource.url=jdbc:mysql: //localhost:3306/test spring.datasource.username=root spring.datasource.password=root spring.datasource.driver- class -name=com.mysql.jdbc.Driver server.port= 1111 |
這里server.port=1111是定義了改項目的端口,默認的是8080.
然后,定義一個java的實體類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
package org.shenlan.web; /** * Created by wangwei on 2016/9/2. */ public class User { private Integer id; private String name; private Integer age; 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 Integer getAge() { return age; } public void setAge(Integer age) { this .age = age; } } |
這里實體類的字段要和數據庫的字段對應起來,不然就要取別名了。
之后,定義一個dao的接口:
1
2
3
4
5
6
7
8
9
10
11
12
|
package org.shenlan.web; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Select; /** * Created by Administrator on 2016/9/2. */ @Mapper public interface UserMapper { @Select ( "select * from user where name = #{name}" ) User findUserByName( @Param ( "name" )String name); } |
@Mapper就是我們要與mybatis融合關鍵的一步,只要一個注解就搞定了。
哈哈哈,最后我們就來寫一個測試類吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
package org.shenlan.web; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; /** * Created by wangwei on 2016/9/2. */ @RestController @RequestMapping ({ "/home" }) public class UserController { @Autowired UserMapper userMapper; @RequestMapping (value = "/user" ) @ResponseBody public String user(){ User user = userMapper.findUserByName( "王偉" ); return user.getName()+ "-----" +user.getAge(); } } @RestController 是對應的restful風格的控制器, @RequestMapping 里面可以對應一個數組哦 |
打開瀏覽器,輸入:http://localhost:1111/home/user
效果如下:
以上所述是小編給大家介紹的springboot與mybatis整合實例詳解(完美融合),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:http://www.cnblogs.com/shenlanzhizun/p/5832976.html