簡(jiǎn)介
從 Spring Boot 項(xiàng)目名稱中的 Boot 可以看出來(lái),Spring Boot 的作用在于創(chuàng)建和啟動(dòng)新的基于 Spring 框架的項(xiàng)目。它的目的是幫助開(kāi)發(fā)人員很容易的創(chuàng)建出獨(dú)立運(yùn)行和產(chǎn)品級(jí)別的基于 Spring 框架的應(yīng)用。Spring Boot 會(huì)選擇最適合的 Spring 子項(xiàng)目和第三方開(kāi)源庫(kù)進(jìn)行整合。大部分 Spring Boot 應(yīng)用只需要非常少的配置就可以快速運(yùn)行起來(lái)。
Spring Boot 包含的特性如下:
創(chuàng)建可以獨(dú)立運(yùn)行的 Spring 應(yīng)用。
直接嵌入 Tomcat 或 Jetty 服務(wù)器,不需要部署 WAR 文件。
提供推薦的基礎(chǔ) POM 文件來(lái)簡(jiǎn)化 Apache Maven 配置。
盡可能的根據(jù)項(xiàng)目依賴來(lái)自動(dòng)配置 Spring 框架。
提供可以直接在生產(chǎn)環(huán)境中使用的功能,如性能指標(biāo)、應(yīng)用信息和應(yīng)用健康檢查。
沒(méi)有代碼生成,也沒(méi)有 XML 配置文件。
好了,上面說(shuō)這么多都是給下文做鋪墊,感興趣的朋友繼續(xù)往下閱讀吧。
大家都知道springboot搭建一個(gè)spring框架只需要秒秒鐘。
下面給大家介紹一下springboot與mybatis的完美融合:
首先:創(chuàng)建一個(gè)名為springboot-mybatis的maven項(xiàng)目,記住:一定要maven哦,不懂maven的可以自己惡補(bǔ)一下maven知識(shí),這里就不介紹maven了。
   下面給出pom.xml的完整配置:
<?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>
之后創(chuàng)建一個(gè)啟動(dòng)類:
package org.shenlan;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/*** Created by wangwei on 2016/9/2.*/@SpringBootApplicationpublic class Application {public static void main(String[] args){SpringApplication.run(Application.class,args);}}這樣一個(gè)完整的springboot項(xiàng)目就完成了,是不是很簡(jiǎn)單。
接下來(lái)就可以整理與mybatis的東東了。
首先,創(chuàng)建配置文件:application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=rootspring.datasource.driver-class-name=com.mysql.jdbc.Driverserver.port=1111
這里server.port=1111是定義了改項(xiàng)目的端口,默認(rèn)的是8080.
然后,定義一個(gè)java的實(shí)體類:
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;}}這里實(shí)體類的字段要和數(shù)據(jù)庫(kù)的字段對(duì)應(yīng)起來(lái),不然就要取別名了。
之后,定義一個(gè)dao的接口:
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.*/@Mapperpublic interface UserMapper {@Select("select * from user where name = #{name}")User findUserByName(@Param("name")String name);}@Mapper就是我們要與mybatis融合關(guān)鍵的一步,只要一個(gè)注解就搞定了。
哈哈哈,最后我們就來(lái)寫一個(gè)測(cè)試類吧:
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 {@AutowiredUserMapper userMapper;@RequestMapping(value = "/user")@ResponseBodypublic String user(){User user = userMapper.findUserByName("王偉");return user.getName()+"-----"+user.getAge();}}@RestController是對(duì)應(yīng)的restful風(fēng)格的控制器,@RequestMapping里面可以對(duì)應(yīng)一個(gè)數(shù)組哦打開(kāi)瀏覽器,輸入:http://localhost:1111/home/user
效果如下:

以上所述是小編給大家介紹的springboot與mybatis整合實(shí)例詳解(完美融合),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)武林網(wǎng)網(wǎng)站的支持!
新聞熱點(diǎn)
疑難解答
圖片精選