This section describes the complete process of SpringBoot creating the first sample SSM project, using the tool STS, which is essentially similar to IDEA operations.
Example code at: github.com/laolunsi/sp…
preface
According to the feedback of several netizens, the article was re-edited. This article starts with environment configuration, then goes to project creation, and finally describes SSM framework integration, showing a complete SpringBoot project creation and use process.
Those of you who have been tormented by configuration files, especially XML, will know the trouble of building a direct SSM or SSH framework based on Maven. SpringBoot solves this problem by eliminating all the tedious configuration and using only an application.properties file.
Let’s get down to business.
I. Environment construction
Start by downloading a version of Eclipse designed specifically for Spring, Spring Tool Suite, or STS. It is a special version of Eclipse with a very similar interface and operation to Eclipse, and you can download the ZIP package and run it directly. Note: The steps and interface for creating a Springboot project are exactly the same for IDEA and STS. The structure of the project created is similar. The project created by STS can be directly imported into IDEA for use.
Take a look at the interface:
Create a SpringBoot project
Unzip the package and run the following exe file (the one with the green icon), then you will see the screen above. Then click on the top left corner, File — new — Spring Starter Project. Here are the steps: First, new — >Spring Starter Project.
Next, fill in the name of the project, group is optional, do not care about other, here the service URL refers to Spring Boot official website address.
Select Web, DevTools, MySQL, Mybatis, Thymeleaf. Note: This environment is optional and can be added to maven’s dependency configuration file pom.xml if necessary. I will add them first, and explain them later.
Click Next/Finish and wait for a while. The project is created and the directory is as follows:
Note: If the static or templates folder under Resources doesn’t exist, don’t worry. I created it because I selected those dependencies.
Iii. Project launch
So far, the SpringBoot project has been created. We can see the start class SpringBootDemoApplication. Java class – this class.
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDemoApplication {
public static void main(String[] args) { SpringApplication.run(SpringBootDemoApplication.class, args); }}Copy the code
What does this class do? We see that there’s a main method in there. Yes, the SpringBoot project is launched using this class. Right click on this class, run as — SpringBoot App, and the project will be launched. There is a misconception here as to why following the steps I have done here will result in an error. This was due to the fact that I had chosen to add dependencies such as Web, where the project could not be executed directly — a look at the console log shows that the database was not configured. And if I did not add these dependencies, run directly SpringBootWebApplication. Java file, you can start the project. Next, let’s take a look at the configuration of the environment – once configured, you can run the empty SSM project.
4. Environment configuration
4.1 the maven pom. XML
Why maven first? Because I said SSM — Spring+SpringMVC+Mybatis project. This should be more interesting to everyone — at present, most of these projects in the enterprise are SSM framework. SSH, which used to be popular, is not used much now. Out of the way, SSH really sucks. Take a look at my pom.xml and check if the dependency isn’t added correctly:
<?xml version="1.0" encoding="UTF-8"? >
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>SpringBootDemo</artifactId>
<version>0.0.1 - the SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6. RELEASE</version>
<relativePath/> <! -- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<! -- SpringBoot recommended template engine, to map HTML/JSP, must introduce thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<! -- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<! -- Hot deployment, change code without restarting the project -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
</dependency>
<! Mysql > select * from 'mysql';
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Copy the code
4.2 Configuration file application.properties
The SpringBoot project configuration is based on the application.properties file. Configure the database, Mybatis mapping file and more advanced Redis, RabbitMQ etc. (the configuration file here has been modified and is up to date on Github). ** Note: the following configuration of the database address, account and password, must be exactly the same as your machine! ** If your database account is a different name, such as admin, please modify the following configuration.
# server config
server.port: 8081
# mysql
spring.datasource.url: jdbc:mysql://localhost:3306/umanager? useSSL=false&autoReconnect=true
spring.datasource.username: root
spring.datasource.password: root
spring.datasource.driver-class-name: com.mysql.jdbc.Driver
spring.datasource.dbcp2.validation-query: 'select 1'
spring.datasource.dbcp2.test-on-borrow: true
spring.datasource.dbcp2.test-while-idle: true
spring.datasource.dbcp2.time-between-eviction-runs-millis: 27800
spring.datasource.dbcp2.initial-size: 5
spring.datasource.dbcp2.min-idle: 5
spring.datasource.dbcp2.max-idle: 100
spring.datasource.dbcp2.max-wait-millis: 10000
# thymleaf
spring.thymeleaf.cache : false
# mybatis
mybatis.mapper-locations: classpath:mapper/*.xml
mybatis.configuration.map-underscore-to-camel-case: true
Copy the code
4.3 Starting the Project
Find SpringBootDemoApplication class, Run the As – Spring Boot App, launch is successful, the console is not an error.
5. Examples of SpringBoot+SSM framework integration
The first step is to create a database – this is very important. Create database and table according to our configuration in application.properties. I use uManager database and user table.
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL.`password` varchar(255) NOT NULL.`address` varchar(255) NOT NULL,
PRIMARY KEY (`id`))ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES ('1'.'ja'.'123'.'jiangsu');
INSERT INTO `user` VALUES ('2'.'BL'.'123'.'Singapore');
Copy the code
Step 2: Create BasicController.java (see bottom for full project directory)
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import com.example.demo.model.bean.User;
import com.example.demo.model.dao.UserDAO;
// @RestController = @Controller + @ResponseBody
@RestController
public class BasicController {
@Autowired
private UserDAO userDAO;
@GetMapping(value = "")
public String index(a) {
return "login"; // The return value is a String with the value "login". The reason it doesn't point to the interface is because the class annotation is @RestController
}
@GetMapping(value = "index.do")
public ModelAndView index2(a) {
return new ModelAndView("login"); // Point to the interface here
}
@GetMapping(value = "login.do")
public Object login(String name, String password) {
System.out.println("Pass parameter: name=" + name + ", password=" + password);
if (StringUtils.isEmpty(name)) {
return "Name cannot be empty";
} else if (StringUtils.isEmpty(password)) {
return "Password can't be empty.";
}
User user = userDAO.find(name, password);
if(user ! =null) {
return user;
} else {
return "Wrong username or password"; }}}Copy the code
This class uses the User class and injects the UserDAO interface. We also create these two classes:
public class User implements Serializable {
private static final long serialVersionUID = -5611386225028407298L;
private Integer id;
private String name;
private String password;
private String address;
// Omit the get and set methods
}
Copy the code
package com.example.demo.model.dao;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import com.example.demo.model.bean.User;
@Mapper
public interface UserDAO {
public User find(@Param("name")String name, @Param("password")String password);
// Note: CRTL+Shift+O can quickly import all the import files
}
Copy the code
Mybatis. Mapper-locations = mybatis. Classpath :mapper/*.xml, create mapper folder under Resources folder, and add Mybatis XML file below. Write userdao.xml here:
<?xml version="1.0" encoding="UTF-8" ? >
<mapper namespace="com.example.demo.model.dao.UserDAO">
<select id="find" resultType="com.example.demo.model.bean.User">
SELECT id, name, password, address from user where name = #{name} and password = #{password}
</select>
</mapper>
Copy the code
You’ll also need a login. HTML page under the Resources /templates folder:
<html>
<! -- meta specifies the encoding format to prevent Chinese garble -->
<meta charset="UTF-8" />
<head>
<title>The login</title>
</head>
<body>
<form action="/login.do" method="GET">User name:<input type="text" id="name" name="name" />Password:<input type="password" id="password" name="password" />
<input type="button" value="Login" onclick="submit()" />
</form>
</body>
</html>
Copy the code
Next, let’s look at the project directory structure:! [Insert image description here](02-Springboot SSM.assets/70-20191211222243726.png)
Start and test
So far, we have integrated the SSM framework in SpringBoot. Let’s run this to see what happens. After the Application class is started, the console is error free. Enter http://localhost:8081/ in the browser. The following screen is displayed:
login
http://localhost:8081/
The following test the login function, enter http://localhost:8081/index.do, see the following interface:
Enter a correct user in your database user table and click Login to get the following sample data:
If incorrect data is entered:
This shows that the SSM framework has been integrated successfully! Our first example of SpringBoot+SSM is complete!!
learn
Personal website: www.eknown.cn
GitHub:github.com/laolunsi
Public account: Ape biological language, “share technology, also feel life”, welcome to pay attention to!