1 Introduction
SpringBoot main solution is to simplify the configuration in the micro-architecture services (fast configuration), separating the front and rear end, rapid development
advantage:
- l provides a quick start entry
- l-box, providing a default configuration
- l embedded container of web projects
- l no redundancy code generation and xml configuration requirements
2. Run D EMO
Creating SpringBoot several ways the project:
- l official website Initializr
- l use the Eclipse , STS , Idea and other IDE to create Maven project and the introduction of dependence
- l use STS plug the Spring Initializr create project
Access http://start.spring.io/ into the Spring project Initializr
Generate download demo.zip
Import project
1.Import a Maven project
2. Select the file you want to import
3. Project skeleton
Startup project
l direct run to start the program in the Main () method
l installed STS plug-in or using STS can right click on the project RunAS -> the Spring the Boot APP
Run successfully message:
If you run error, please refer to the FAQ.
personalise
Modify the startup banner
In the new resources under the directory banner.txt
http://www.network-science.de/ascii/ English
https://www.degraeve.com/img2txt.php Pictures
${AnsiColor.BRIGHT_CYAN} _ _ _ _ _ ______ | | (_)| | | | | | | ___ \ | | _ | |_ | |_ | | ___ | |_/ /__ _ __ _ ___ | | | || __|| __|| | / _ \| __// _` | / _` | / _ \ | |____| || |_ | |_ | || __/| | | (_| || (_| || __/ \_____/|_| \__| \__||_| \___|\_| \__,_| \__, | \___| __/ | |___/ ${AnsiColor.BRIGHT_RED} Logo Designer: LittlePage Spring Boot Version: ${spring-boot.version}${spring-boot.formatted-version}
Eclipse的皮肤
菜单栏中
Help -> EclipseMarketplace
搜索Theme
傻瓜式安装这个,安装完成会提示重启,跟随指引选择喜欢的风格。
简单使用
application.properties
把所有的配置全放在这个文件里,方便统一管理,maven也可以做到
修改tomcat端口
server.port=90
修改项目路径
server.servlet.context-path=/demo
多个入口main方法,打包之后找不到入库类
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>com.yxxy.MyApp</mainClass> </configuration> </plugin> </plugins> </build>
HelloWorld
RestController
RestController = @Controller+@ResponseBody
一个效果
@RestController public class MyAppController { @RequestMapping("/") public Map<String, String> index() { Map<String, String> map = new HashMap<>(); map.put("aaa", "bbb"); map.put("aaa", "bbb"); map.put("aaa", "bbb"); map.put("aaa", "bbb"); return map; }
使用thymeleaf模板引擎
Pom.xml引用
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
Controller代码
@Controller public class IndexController { @RequestMapping("/") public String index(ModelMap map) { // 加入一个属性,用来在模板中读取 map.addAttribute("msg", "nihao~"); return模板文件的名称,对应src/main/resources/templates/index.html return "index"; } 模板文件代码 <h1 th:text="${msg}">hi!</h1>
稍微复杂的restful api应用
UserRestfulController @RequestMapping("/") @RestController public class UserRestfulController { static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>()); @RequestMapping(value="/User",method=RequestMethod.GET) public List<User> getUserList(){ ArrayList<User> list = new ArrayList<>(users.values()); return list; } @RequestMapping(value="User",method=RequestMethod.POST) public String addUser(@ModelAttribute User user) { users.put(user.getId(), user); return "addUser Success"; } } User public class User { private Long id; private String loginName; private String password; private String nickName; 注入Service UserRestfulController @Autowired private UserService userSrv; @RequestMapping(value="/User",method=RequestMethod.GET) public List<User> getUserList(){ return userSrv.getUserList(); } @RequestMapping(value="User",method=RequestMethod.POST) public String addUser(@ModelAttribute User user) { String msg = userSrv.addUser(user); return msg; } UserService @Service public class UserService { static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long,User>()); public List<User> getUserList() { ArrayList<User> list = new ArrayList<>(users.values()); return list; } public String addUser(User user) { users.put(user.getId(), user); return "addUser Success"; } }
前端模板显示
<h1>User list</h1> <table> <tr> <th>NAME</th> <th>loginName</th> <th>nickName</th> </tr> <tr th:each="user : ${list}"> <td th:text="${user.id}">id</td> <td th:text="${user.loginName}">loginName</td> <td th:text="${user.nickName}">nickName</td> </tr> </table> <p> <a href="../home.html" th:href="@{/}">Return to home</a> </p>
常见问题
Pom.xml
Springboot项目必须要继承的parnet
Pom.xml第一行报错
进入本地库 for /r %i in (*.lastUpdated) do del %i 然后update
缺少或包错误
删掉 重新update
找不到主类
所有错误都解决后
Jar方式运行 首先得有这个jar包
先clean package 生成jar文件,然后再run main方法
找不到jdk
把jre的路径换成jdk的
启动后自动停止