springboot與mybatis整合執行個體詳解(完美融合)_java

來源:互聯網
上載者:User

簡介

從 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的完整配置:

<?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>

之後建立一個啟動類:

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);}}

這樣一個完整的springboot項目就完成了,是不是很簡單。

接下來就可以整理與mybatis的東東了。

首先,建立設定檔: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是定義了改項目的連接埠,預設的是8080.

然後,定義一個java的實體類:

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的介面:

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融合關鍵的一步,只要一個註解就搞定了。

哈哈哈,最後我們就來寫一個測試類別吧:

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是對應的restful風格的控制器,@RequestMapping裡面可以對應一個數組哦

開啟瀏覽器,輸入:http://localhost:1111/home/user

效果如下:

以上所述是小編給大家介紹的springboot與mybatis整合執行個體詳解(完美融合),希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.