尽管这个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项目了
指望能尽早肝出来