1。Windows7 Enterprise Edition
2。アイデア14
3。JDK1.8
4。Maven3.5.2
5。マリアドブ
6。SQLYOG
MavenディレクトリのConf Directoryのsettings.xmlを次のコンテンツに追加します。
1. Alibaba Cloudのリポジトリの使用は、公式Webサイトよりもはるかに高速です。
<Mirros> <id> nexus-aliyun </id> <mirricof> central </mirrorof> <name> nexus aliyun </name> <url> http://maven.aliyun.com/nexus/content/groups/public </url> </mirrer>
2。グローバルJDK構成
<! - グローバルJDK構成、settings.xml-> <profile> <id> jdk18 </id> <activation> <cativebydefault> true </activebydefault> <jdk> 1.8 </jdk> </activation> <properties> <maven.compiler.source> 1.8 </maven.compiler.source> <maven.compiler.target> 1.8 </maven.compiler.target> <maven.compiler.compilerversion> 1.8 </maven.compiler.compilerversion> </properties> </properties> </properties> </exper>
1. Maven設定:Mavenディレクトリを選択し、同時にファイルとローカルリポジトリを構成します。
2。文字エンコード設定
以下に示すように、Auto-Importを有効にし、プロジェクトディレクトリを作成します。
1。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 <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modeleversion> 4.0.0 </modelversion> <groupid> cn.temptation </groupid> <artifactid> sudyspringboot </artifactid> <バージョン> 1.0-snapshot </version> <! - spring boot-> <parent> springframework.boot </groupid> <artifactid> spring-boot-starter-parent </artifactid> <berversed> <! - web-> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <! - thymeleaf-> <dependency> org.springframework.boot < </dependency> <! - mysql ---> <依存関係> groupid> mysql </groupid> <artifactid> mysql-connector-java </artifactid> <バージョン> 5.1.21 </version> </dependency> <! - jpa-> <依存関係> <グループ< <artifactid> spring-boot-starter-data-jpa </artifactid> </dependency> </dependencies> </project>
2。リソースディレクトリに新しいapplication.propertiesを作成します(もちろん、yamlを使用する場合は、yamlを使用できます)
# Database connection spring.datasource.url=jdbc:mysql://127.0.0.1:3306/testspring.datasource.username=rootspring.datasource.password=saspring.datasource.driver-class-name=com.mysql.jdbc.Driver# JPA configuration spring.jpa.properties.hibernate.hbm2ddl.auto = update
3. Springboot Program Startup Class SpringBootApplication.javaを作成します
パッケージcn.temptation; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springbootapplicationpublic class springbootapplication {public static boid main [] {// springboot Projects Projects Startssssssssssings) springApplication.run(springbootapplication.class、args); }}4.エンティティクラスCategory.javaを作成します
パッケージcn.temptation.model; Import javax.persistence (null、 'Shoes'); //// select * from category; @entity @table(name = "category")public class category {@id @generatedValue(Strategy = GenerationType.identity)@column(name = "categoryid")private Integer categoryid; @column(name = "categoryName")private string categoryName; public Integer getCategoryId(){return categoryId; } public void setCategoryId(integer categoryId){this.categoryId = categoryId; } public string getCategoryName(){return categoryName; } public void setCategoryName(string categoryName){this.categoryName = categoryName; }}5. DAOインターフェイスCategoryDao.javaを作成します
パッケージcn.temptation.dao; import cn.temptation.model.category; Import org.springframework.data.jpa.repository.jparepository; public interface categorydaoはjparepository <category、integer> {}を拡張します6. Controller Class CategoryController.javaを作成します
パッケージcn.temptation.web; import cn.temptation.dao.categorydao; Import cn.temptation.model.category; Import org.springframework.beans.factory.annotation.autowired; Import org.springframework.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data.data org.springframework.data.dodain.pageable; Import org.springframework.data.domain.sort; Import org.springframework.stereotywork.stereotywork.stringframework.web.bind.annotation.requestmapping; import.springframework.not.annot.annot.annot.annot.annot.annot.annot.annot.annot.annot.not.not.not.not.not.not.not.not.not.not.net. org.springframework.web.servlet.modelandview; Import java.util.list; @controllerpublic class categorycontroller {@autowired private categorydao categorydao; /** * ofging query * * @return * /// @requestmapping( "/categorylist")// public modelandview categorylist(){// list <category> list = categorydao.findall(); //// modelandview mav = new modelandview( "); }/** * Pagination Query * * @return */@RequestMapping( "/categorylist")public ModelandView Categorylist(@RequestParam(value = "start"、defaultValue = "0")integer start、 @requestparam(value = "lime"、defaultvalue = " 0:start;ソートsort = new sot(sort.default_direction、 "categoryid"); Pageable Pageable = new Pagerequest(start、lime、sort); Page <Category> Page = categorydao.findall(pageable); // System.out.println(page.getnumber()); // System.out.println(page.getNumberFelements()); // System.out.println(page.getSize()); System.out.println(page.getTotalPages()); // System.out.println(page.isfirst()); // System.out.println(page.islast()); ModelAndView MAV = new ModelAndView( "CategoryList"); mav.addobject( "page"、page); mavを返します。 }/*** categoryの新しいビュー* @return*/@requestMapping( "/categoryInit")public string categoryInit(){return "CategoryInit"; }/** *カテゴリの新しい操作 * @param Model * @return */@RequestMapping( "/categoryInsert")public String categoryInsert(category model){categorydao.save(model); "redirect:categorylist"を返します。 }/** *カテゴリ削除操作 * @param categoryid * @return */@requestMapping( "/categorydelete")public string categorydelete(integer categoryid){categorydao.deletebyid(categoryid); "redirect:categorylist"を返します。 }/** *カテゴリ編集ビュー * @param categoryid * @return */@requestMapping( "/categoryedit")public modelandview categoryedit(integer categoryid){category model = categorydao.getone(categoryid); ModelAndView MAV = new ModelAndView( "CategoryEdit"); mav.addobject( "category"、model); mavを返します。 }/** *カテゴリ編集操作 * @param Model * @return */@RequestMapping( "/categoryUpdate")public string categoryupdate(category model){categorydao.save(model); "redirect:categorylist"を返します。 }}7.リソースディレクトリの下に新しいテンプレートディレクトリを作成し、プレゼンテーションレイヤーを作成します:カテゴリリストページ(categorylist.html)、カテゴリ新しいページ(categoryinit.html)、カテゴリ編集ページ(categoryedit.html)
カテゴリリストページ(categorylist.html)
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>カテゴリリスト</title> <style>テーブル、th、td {border:1px solid緑。国境崩壊:崩壊; } </style> </head> <body> <a th:href = "@{/categoryInit}"> add </a> <table> <tr> <th>カテゴリ番号</th> <th>操作</th> <th>操作</th> </tr> <! - no page traversal - > <! traversal - > <tr th:hride = "item:$ {page.content}"> <td th:text = "$ {item.categoryid}">カテゴリ番号</td> <td th:text = "$ {item.categoryName}">カテゴリ名</td> <td> <td> < th:href = "@{/categoryedit(categryid = $ {item.categoryid})}"> edit </a> <a th:href = "@{/categorydelete(categoryid = $ {item.categoryid})}}"> delete </a> th:href = "@{/categorylist(start = 0)}"> [home] </a> <a th:if = "$ {not page.isfirst()}" th:href = "@{/categorylist(start = $ {page.totalpage-1})}" Th:href = "@{/categoryList(start = $ {page.number+1})}"> [次のページ] </a> <a th:href = "@{/categorylist(start = $ {page.totalpages-1})}"> [last page] </a> </div> </body> </html>カテゴリの新しいページ(categoryinit.html)
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> new category </title> </head> <body> <form action = "categoryinsert" method = "post"> <label for = "txtcategoryname">カテゴリ名:</ラベル> <入力タイプ= name = "categoryName"/> <br/> <入力タイプ= "submit" value = "submit"> </form> </body> </html>
カテゴリ編集ページ(categoryedit.html)
<!doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> category edit </title> </head> <body> <form action = "categoryupdate" method = "post> <input type =" hidden "=" txtcategoryid "name ="カテゴリド " Th:field = "$ {category.categoryid}"/> <br/> <label for = "txtcategoryName">カテゴリ名:</label> <入力タイプ= "テキスト" id = "txtcategoryName" name = "categoryName" th:field = "$ {category.categoryName}" value = "submit" "> </form> </body> </html>要約します
上記は、アイデア+Maven+Springboot+JPA+Thymeleafの編集者が紹介したCrudとPaginationの実装です。私はそれが誰にでも役立つことを願っています。ご質問がある場合は、メッセージを残してください。編集者は、すべての人に時間内に返信します。 wulin.comのウェブサイトへのご支援ありがとうございます!