写一个Spring Boot的Hello World
尽管这个demo也就hello world水平,但我还是要记录一下(总算能动了QAQ),毕竟老是看文章不动手不行啊
上次写Servlet的CRUD项目还是2月份,虽然代码忘的差不多了,但我就记得JDBC写起来特别累,作为入门还是学学Spring吧,然而被Spring的配置劝退了(现在看好像也不是问题,只是没有那种经验,碰到就懵逼),现在改用SpringBoot了,并且学习牛客网上的小demo作为入门参考
从啥都不会到写个能动的Spring Boot应用需要知道以下概念↓
1.maven构建项目的套路,为了方便还要会改国内源(然后当你配置好后发现IDEA居然用自带的配置)
2.MVC架构,其中controller就是相当于一个服务器的入口
3.注解的用法,初次接触到高度封装的代码我确实头疼,一个@就完事的非侵入式写法很难明白它的原理(因为过于隐蔽了)
4.项目常规的通用结构,由于没有写过正经的项目,我也不懂该怎么分orz(只会DAO),参考一个project能解决这种问题(核心是在于了解MVC的细分)
5.懂设计模式,感谢自己几个月前了解了不少设计模式,核心的IOC/AOP不是问题(虽然这里没体现)
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项目了
期望能尽早肝出来
写一个Spring Boot的Hello World的更多相关文章
- 摊牌了!我要手写一个“Spring Boot”
目前的话,已经把 Spring MVC 相关常用的注解比如@GetMapping .@PostMapping .@PathVariable 写完了.我也已经将项目开源出来了,地址:https://gi ...
- 自己写一个spring boot starter
https://blog.csdn.net/liuchuanhong1/article/details/55057135
- 我的第一个spring boot程序(spring boot 学习笔记之二)
第一个spring boot程序 写在前面:鉴于spring注解以及springMVC的配置有大量细节和知识点,在学习理解之后,我们将直接进入spring boot的学习,在后续学习中用到注解及其他相 ...
- 创建一个 Spring Boot 项目,你会几种方法?
我最早是 2016 年底开始写 Spring Boot 相关的博客,当时使用的版本还是 1.4.x ,文章发表在 CSDN 上,阅读量最大的一篇有 42W+,如下图: 2017 年由于种种原因,就没有 ...
- 第一个Spring Boot程序
Windows 10家庭中文版,java version "1.8.0_152", Eclipse Oxygen.1a Release (4.7.1a),Spring Tools ...
- Spring Boot (1) 构建第一个Spring Boot工程
Spring boot简介 spring boot是spring官方推出的一个全新框架,其设计目的是用来简化新spring应用的初始搭建以及开发过程. Spring boot特点 1.化繁为简,简化配 ...
- 年轻人的第一个 Spring Boot 应用,太爽了!
Spring Boot 大家都知道是啥吧? 还有不知道的来看这篇扫下盲:告诉你,Spring Boot 真是个牛逼货!. 顺便再往下看,栈长给你带来年轻人的第一个 Spring Boot 应用,撸码史 ...
- 最近做的一个Spring Boot小项目,欢迎大家访问 http://39.97.115.152/
最近做的一个Spring Boot小项目,欢迎大家访问 http://39.97.115.152/,帮忙找找bug,网站里有源码地址 网站说明 甲壳虫社区(Beetle Community) 一个开源 ...
- 峰哥说技术:02-第一个Spring Boot应用程序
Spring Boot深度课程系列 峰哥说技术—2020庚子年重磅推出.战胜病毒.我们在行动 02第一个Spring Boot应用程序 1.版本要求 集成开发环境:IntelliJ IDEA 2017 ...
随机推荐
- 问题:Custom tool error: Failed to generate code for the service reference 'AppVot;结果:添加Service Reference, 无法为服务生成代码错误的解决办法
添加Service Reference, 无法为服务生成代码错误的解决办法 我的解决方案是Silverlight+WCF的应用,Done Cretiria定义了需要在做完Service端的代码后首先运 ...
- Diag:Diagonal matrices and diagonals of a matrix
Diag:Diagonal matrices and diagonals of a matrix Syntax X = diag(v,k) X = diag(v) v = diag(X,k) v = ...
- DAY18-Django之form表单
构建一个表单 假设你想在你的网站上创建一个简单的表单,以获得用户的名字.你需要类似这样的模板: <form action="/your-name/" method=" ...
- CSS中cursor的pointer 与 hand(转)
CSS中cursor的pointer 与 hand 转载 2015年12月25日 16:18:36 标签: cursorpointer / cursorhand 1781 cursor:hand 与 ...
- POJ 2176 Folding(区间DP)
题意:给你一个字符串,请把字符串压缩的尽量短,并且输出最短的方案. 例如:AAAAA可压缩为5(A), NEERCYESYESYESNEERCYESYESYES可压缩为2(NEERC3(YES)). ...
- 关于c#分支语句和分支嵌套还有变量的作用域。
分支语句: if....else if....else 必须以 if 开头 后面加括号写入需要判断的内容. 举个栗子说明一下 if (bool类型(比较表达式)) // 他会判断括号内的条件是否 ...
- Excel VBA连接MySql 数据库获取数据
编写Excel VBA工具,连接并操作Mysql 数据库. 系统环境: OS:Win7 64位 英文版 Office 2010 32位 英文版 1.VBA连接MySql前的准备 Tools---> ...
- 树莓派研究笔记(10)-- Retropie 模拟器
前面介绍过lakka模拟器,小巧,轻便,支持中文.其实最著名的游戏系统还是要属于Retropie啊.虽然笨重了一点,但是很多树莓派系统的原汁原味还是保留的很好.这样就不需要我们自己还要对lakka的源 ...
- Entity Framework Tutorial Basics(36):Eager Loading
Eager Loading: Eager loading is the process whereby a query for one type of entity also loads relate ...
- SDUT 3400 数据结构实验之排序三:bucket sort
数据结构实验之排序三:bucket sort Time Limit: 150MS Memory Limit: 65536KB Submit Statistic Problem Description ...