導入
Spring Bootプロジェクト名のブートからわかるように、Spring Bootの役割は、Spring Frameworkに基づいて新しいプロジェクトを作成して開始することです。その目的は、開発者がスタンドアロンと製品レベルのスプリングフレームワークに基づいてアプリケーションを簡単に作成できるようにすることです。 Spring Bootは、統合する最も適切なSpring Subprojectおよびサードパーティのオープンソースライブラリを選択します。ほとんどのスプリングブートアプリケーションは、迅速に実行するためにほとんど構成を必要としません。
Spring Bootには、次の機能が含まれています。
独立して実行できるスプリングアプリを作成します。
戦争ファイルを展開する必要なく、TomcatまたはJettyサーバーに直接埋め込みます。
Apache Maven構成を簡素化するために、推奨される基本的なPOMファイルを提供します。
プロジェクトの依存関係に基づいて、可能な限りスプリングフレームワークを自動的に構成します。
パフォーマンスメトリック、アプリケーション情報、アプリケーションの健康チェックなど、生産環境で直接使用できる機能を提供します。
コード生成もXML構成ファイルもありません。
さて、上記のことは、次のテキストの基礎を築くことです。興味のある友達は読み続けることができます。
誰もが、スプリングブートがスプリングフレームワークを構築するのに数秒しかかからないことを知っています。
スプリングブートとマイバティスの完璧な融合を紹介させてください。
最初:Springboot-Mybatisと呼ばれるMavenプロジェクトを作成します。覚えておいてください:あなたはメイベンしなければなりません。 Mavenを理解していない人は、自分でMavenの知識を補うことができます。ここでMavenを紹介しません。
pom.xmlの完全な構成を以下に示します。
<?xml version = "1.0" encoding = "utf-8"?> <プロジェクトxmlns = "http://maven.apache.org/pom/4.0.0" xmlns:xsi = "http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation = "http://maven.apache.org/4.0. http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0<//modelversion> <groupid> springboot-mybatis</groupid> <artifactid>springbootimybatis</ar tifactid> <version> 1.0.0 </version> <packaging> war </packaging> <name> springboot-mybatis </name> <説明> Springboot-mybatis </name> <説明> Springbootプロジェクト</description> <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <バージョン> 1.3.2.release </version> <re LativePath/> </parent> <Properties> <Project.build.sourceencoding> utf-8 </project.build.sourceencoding> <java.version> 1.8 </java.version> </properties> <depencies> <Dependency> groupId> org.springframework.boot </groupid> <artifactid> spring-boot-starter </artifactid> </dependency> <依存関係> <groupid> org.springframework.boot < /groupid> <artifactid> spring-boot-starter-test</artifactid> <scope </scope> </dependency>< dependency> <groupid> org.mybatis.spring.boot</groupid> <artifactid>my Batis-spring-boot-starter </artifactid> <バージョン> 1.1.1 </version> </dependency> <dependency> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-star ter-web </artifactid> </dependency> <dependency> <groupid> mysql </groupid> <artifactid> mysql-connector-java </artifactid> <バージョン> 5.1.21 </version> </dependency> </depe ndencies> <burtion> <blugins> <plugin> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </plugin> <plugin> <groupid> org.ap ache.maven.plugins </groupid> <artifactid> maven-surefire-plugin </artifactid> <configuration> <skip> true </skip> </configuration> </plugin> </plugin> </build> </project>
次に、スタートアップクラスを作成します。
Package org.shenlan; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplication;/*** 2016/9/2.*//@springbootlicispublic Classアプリケーション{Public Static void [] args){springApplication.run(application.class、args);}}このような完全なスプリングブートプロジェクトが完了しました、それは非常に簡単ではありませんか?
次に、MyBatisで物を整理できます。
まず、構成ファイル:Application.Propertiesを作成します
spring.datasource.url = jdbc:mysql:// localhost:3306/testspring.datasource.username = rootspring.datasource.passpring.datasource.datasource.datasource.datasource.driver-cass-name = com.mysql.jdbc.driversserver.port = 1111
ここでは、server.port = 1111はプロジェクトを変更するためのポートを定義し、デフォルトは8080です。
次に、Javaエンティティクラスを定義します。
Package org.shenlan.web;/*** Wangweiが2016/9/2。*/public classユーザー{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() = 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;/*** #{name} ")user finduserbyname(@param(" name ")string name);} @Mapperは、MyBatisと統合するための重要なステップであり、1つの注釈だけで行われます。
ハハハ、最後にテストクラスを書きましょう:
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;/*** Wangweiが2016/9/2。*/@restController@requestMapping({"/home"})パブリッククラスのusercontroller {@autowiredermapper usermapper; @Requestmapper()@requestmapper() "/autemapper = usermapper.finduserbyname( "wang wei"); return user.getName()+"-----"+user.getage();}}@restControllerは、対応するRESTFULスタイルコントローラーです。 @RequestMappingは配列に対応できますブラウザを開き、http:// localhost:1111/home/userを入力します
効果は次のとおりです。
上記は、SpringbootとMybatisの統合例の詳細な説明です(完全な統合)。それがあなたに役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!