This is the 9th day of my participation in the November Gwen Challenge. See details: The Last Gwen Challenge 2021.
# introduction
# Environment building
POM
- Setting up a Springboot project is very simple. We just need to inherit SpringBoot from poM
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> . < version > 2.0.3 RELEASE < / version > < / parent >Copy the code
- Because we’re building SpringBoot for the Web. So the dependency of the Web module is introduced
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
Copy the code
- Then springBoot packages the required plug-ins
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
Copy the code
- At this point, our POM link setup is complete. We still need a startup class to build
# start class
@SpringBootApplication public class CrontabApplication { public static void main(String[] args) { SpringApplication.run(CrontabApplication.class, args); }}Copy the code
- That’s the main function above. Add @SpringBootApplication to the class. It’s that simple. This is the end of our Springboot project. Run is not to say.
Consolidated database
- The above two steps completed the setup of the SpringBoot project. At this point the project is a shell project. Since it’s a Web project, we can’t do without the database. Next we will integrate mybatis with mysql database.
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> The < version > 8.0.15 < / version > < / dependency >Copy the code
- Because SpringBoot provides us with a lot of default configurations. So let’s introduce the database here, and then we just need to configure the database information in the configuration file.
Spring: a datasource: url: JDBC: mysql: / / 127.0.0.1:3306 / crontab? serverTimezone=GMT%2B8 username: root password: 123456 driver-class-name: com.mysql.cj.jdbc.DriverCopy the code
- Next we write an interface to manipulate the database try. We won’t configure XML here for testing purposes. Write directly as annotations
@mapper public interface TestMapper {@return */ @select (" Select * from Test") public List<Test> getTests(); }Copy the code
- Then we write the test class
@RunWith(SpringRunner.class) @SpringBootTest(classes = CrontabApplication.class) public class Test { @Autowired private TestMapper testMapper; @org.junit.Test public void getTests() { List<com.github.zxhtom.crontab.model.Test> tests = testMapper.getTests(); System.out.println(tests); }}Copy the code
- We have checked out the database information in the figure. The strength of Springboot is the detail. It’s very comfortable for us. But as a developer we can’t just use it. Getting started should be easy. We’re going to take you layer by layer.