儘管這個demo也就hello world水平,但我仍是要記錄一下(總算能動了QAQ),畢竟總是看文章不動手不行啊html
上次寫Servlet的CRUD項目仍是2月份,雖然代碼忘的差很少了,但我就記得JDBC寫起來特別累,做爲入門仍是學學Spring吧,然而被Spring的配置勸退了(如今看好像也不是問題,只是沒有那種經驗,碰到就懵逼),如今改用SpringBoot了,而且學習牛客網上的小demo做爲入門參考java
從啥都不會到寫個能動的Spring Boot應用須要知道如下概念↓mysql
1.maven構建項目的套路,爲了方便還要會改國內源(而後當你配置好後發現IDEA竟然用自帶的配置)web
2.MVC架構,其中controller就是至關於一個服務器的入口spring
3.註解的用法,初次接觸到高度封裝的代碼我確實頭疼,一個@
就完事的非侵入式寫法很難明白它的原理(由於過於隱蔽了)sql
4.項目常規的通用結構,因爲沒有寫過正經的項目,我也不懂該怎麼分orz(只會DAO),參考一個project能解決這種問題(核心是在於瞭解MVC的細分)數據庫
5.懂設計模式,感謝本身幾個月前瞭解了很多設計模式,核心的IOC/AOP不是問題(雖然這裏沒體現)apache
6.瞭解一下用於注入的XML,前幾天的物聯網(安卓)實驗課上用了XML做爲顯式的注入,瞭解這個過程雖然用不上但會更明白Spring(Boot)的隱式注入是咋回事設計模式
7.同理,瞭解AOP的過程就是要嘗試寫一個靜態代理,而後到動態代理服務器
8.不知道是mybatis封裝的太好仍是怎樣,起碼能動的水平只要你會@
個CRUD就好
9.關於view的部分,我我的認爲目前會對着改網頁便可
10.項目中剩餘部分暫留
因爲這只是hello world水平的記錄,所以代碼也十分的hello world。。
項目結構以下(test/biz/utils等暫無),具體class/interface以.java區分
|------------com.caturra.training | |--------BookLibraryApplication.java | |-------controller | |----BookController.java |--------model |----Book |--------dao |----BookDAO.java |--------service |----BookService.java |------------resources |--------templates |----book |----books.html |----hello.html |-------application.properties |-------log4j.properties |-------mybatis-config.xml
UPDATE.更新個截圖版
如下忽略全限定名
BookLibraryApplication
package com.caturra.training; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class BookLibraryApplication { public static void main(String[] args) { SpringApplication.run(BookLibraryApplication.class,args); } }
BookController
package com.caturra.training.controller; import com.caturra.training.service.BookService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class BookController { @Autowired private BookService bookService; @RequestMapping(path = {"/index"},method = {RequestMethod.GET}) public String getAllBooks(Model model) { model.addAttribute("books",bookService.getAll()); return "book/books"; } @RequestMapping(path = {"/"},method = {RequestMethod.GET}) public String tempTest() { return "hello"; } }
Book
package com.caturra.training.model; public class Book { private int id; private String title; private String author; private int price; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public int getPrice() { return price; } public void setPrice(int price) { this.price = price; } }
BookDAO
package com.caturra.training.dao; import com.caturra.training.model.Book; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; @Mapper public interface BookDAO { String tableName = " book "; String insertField = " title, author, price "; String selectField = insertField; @Select({"SELECT",selectField,"FROM",tableName}) List<Book> getAll(); @Insert({"INSERT INTO",tableName,"(",insertField,") VALUES (#{title},#{author},#{price})"}) int add(Book book); }
BookService
package com.caturra.training.service; import com.caturra.training.dao.BookDAO; import com.caturra.training.model.Book; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class BookService { @Autowired private BookDAO bookDAO; public void add(Book book) { bookDAO.add(book); } public List<Book> getAll() { return bookDAO.getAll(); } }
application.properties配置
spring.freemarker.suffix=.html spring.datasource.url=jdbc:mysql://localhost:3306/mybooklib?useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password=123456 mybatis.config-location=classpath:mybatis-config.xml
mybatis和log4j的配置我沒有查過官方文檔,直接套用別人的,篇幅問題只放出mybatis方便之後抄(刪去)
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <!-- Globally enables or disables any caches configured in any mapper under this configuration --> <setting name="cacheEnabled" value="true"/> <!-- Sets the number of seconds the driver will wait for a response from the database --> <setting name="defaultStatementTimeout" value="3000"/> <!-- Enables automatic mapping from classic database column names A_COLUMN to camel case classic Java property names aColumn --> <setting name="mapUnderscoreToCamelCase" value="true"/> <!-- Allows JDBC support for generated keys. A compatible driver is required. This setting forces generated keys to be used if set to true, as some drivers deny compatibility but still work --> <setting name="useGeneratedKeys" value="true"/> </settings> <!-- Continue going here --> </configuration>
books.html
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>圖書列表</title> </head> <body> <div align="center"> <h3>圖書列表</h3> <table border="1" cellpadding="10"> <tr> <td>書名</td> <td>做者</td> <td>價格</td> </tr> <#list books as book> <tr> <td>《${book.title}》</td> <td>${book.author}</td> <td>${book.price}</td> </tr> </#list> </table> </div> </body> </html>
hello.html做爲檢查是否正常運行
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Hello</title> </head> <body> <p>hello the no offer world!</p> </body> </html>
遇到的坑:
0.數據庫記得寫好表
1.即便是最簡單的項目也有必定的依賴關係,所以從哪裏寫起也是一個問題,我的作法是先寫好controller,畫草稿寫設計一個模塊須要什麼東西,好比一個serive的完成順序model->DAO->service
2.比較晦澀的註解能夠用IDE的源碼查看下載大概看一下
3.一路寫過去大機率完蛋,能夠用簡單的頁面debug一下,或者寫好test
4.本身太菜是個問題
完成這些那就意味着我總算能動個人OJ項目了
指望能儘早肝出來