【MCA】Java Internet Senior Architect (1)
Course 1-Java Advanced Internet Architect – Introduction to SpringBoot
【MCA】Java Internet Senior Architect 【 Ma Soldier 】 Marry? : XB2K
Main Content of course
- MVC Architecture Idea
- Build the SpringBoot project using STS
- Build an Mvcweb project using SpringBoot
- Injection in the MVCWeb project
- Hot deployment V (cmL46679910)
introduce
SpringBoot addresses simple configuration (quick configuration), front-end separation, and rapid development in a microservices architecture
Advantages:
- Provides a quick start to get started
- Out of the box, provide default configurations
- Inline container Web projects
- No redundant code generation and XML configuration requirements
2. Run D emo
Create a project
There are several ways to create a SpringBoot project:
- InitializrV (cmL46679910)
- Create Maven projects and introduce dependencies using Eclipse, STS, Idea, and other ides
- Create the project using the STS plug-in’s SpringInitializr
Access start.spring. IO/to enter the Spring project Initializr
Generate download demo.zip
Import the project
1.I Mport a Maven project
2 . Select the file to import
3 . Project skeleton
Start the project
- Direct run launches the Main () method in the program
- You can install the STS plug-in or use STS to right-click RunAS->Spring Boot APP on the project
The following message is displayed:
If an error occurs, refer to FAQ.
personalized
Modify the launch banner
Create a new banner. TXT in the Resources directory
[英文](www.network-science.de/ascii/)
[images] (www.degraeve.com/img2txt.php)
The skin of the Eclipse
In the menu bar
Help -> EclipseMarketplace
Search the Theme
Install this for a fool. When the installation is complete, you will be prompted to restart. Follow the instructions to choose your favorite style.
Simple to use
application.properties
All the configurations are in this file for unified management, as can Be done with Maven
Modifying the Tomcat port
server.port=90
Modifying the project path
Server.servlet. context-path=/demo V (cmL46679910)
Multiple entry main methods, no incoming class found after packaging
<plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> **V** (**cmL46679910**) </build>Copy the code
HelloWorld
RestController
RestController = @Controller+@ResponseBody
One effect V (cmL46679910)
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;
}
Copy the code
Use the Thymeleaf template engine
P Om. XML references
org.springframework.boot
spring-boot-starter-thymeleaf
The Controller code
Public class IndexController {@requestMapping ("/") **public** String Index (ModelMap map) { AddAttribute (" MSG ", "nihao~"); Return the name of the template file, corresponding to the SRC/main/resources/templates/index return HTML * * * * "index". }Copy the code
Template file code
hi!
V (cmL46679910)
Slightly more complex restful API applications
UserRestfulController
@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";
}
}
**public** **class** User {
**private** Long id;
**private** String loginName;
**private** String password;
**private** String nickName;
Copy the code
User
private Long id;
private String loginName;
private String password;
private String nickName;
Copy the code
Into 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;
}
Copy the code
UserService
**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"; }}Copy the code
Front end template display
V (cmL46679910)
<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>Copy the code