Preface
I used to study Spring MVC, and later I heard from my classmates that Spring Boot is very useful and I highly recommend that I learn this ghost. At the beginning, when I looked for Spring Boot's learning materials online, their blog post was not written well, but was not very detailed.
I was thinking that I would write an article as detailed as possible by myself. I won’t say much below, let’s take a look at the detailed introduction.
Technology stack
Design Pattern
MVC
Function
Editor
IntelJ IDEA 2017
Directory structure
text
The first step is to create a project, open idea File -> New -> Project. We are creating Spring Boot project, so when we come to Project, we choose Spring Initializr, select jdk and click next.
Then I came to this interface, here is the directory where you can fill in the project. If you like it, please be ok.
The next step is to let you choose which dependencies you need, which web, MyBaits, MongoDB, database (I use mysql, so I checked mysql) to check it.
Finally, fill in the project name, then click Finish and you will complete the creation.
How to integrate Spring+SpringBoot+MyBatis+MongoDB
In the first step, in the project directory you filled in, I like to create a folder called Controller, which of course is used to place the Controller. The Entity folder places the entity class, and the Service file is stored in the business logic layer. There is also a ServiceImpl folder under this file that corresponds to the implementation class that stores the Service.
The second step is to configure the detailed code. I have put it on github and click to jump to github (local download). Our configuration is written in a file called application.yml. The project you created is the application.properties file by default, but the .yml file is simpler than the .properties file, so I personally prefer the .yml file.
You will know how to compare the concise method. .properties are configured like this (using the configuration to send emails as an example)
spring.mail.host=smtp.qq.comspring.mail.username=username spring.mail.password=password spring.mail.properties.mail.smtp.auth=truespring.mail.properties.mail.smtp.starttls.enable=truespring.mail.properties.mail.smtp.starttls.required=truespring.mail.properties.mail.smtp.starttls.required=trues
And .yml is configured like this:
mail: host: smtp.qq.com username: //Account used to send mail password: //This is the authorization password of the IMAP/SMTP service properties: mail: stmp: auth: true starttls: enable: true required: true port: 587
Configure data source (data source, MongoDB and mail are all under spring),
spring: datasource: url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver platform: mysql jpa: show-sql: true data: mongodb: uri: mongodb://localhost:27017/blog //blog Remember to change to the name you chose
Configure MyBaits, it has the same status as spring in .yml, so mybatis, spring indentation is the same.
mybatis: type-aliases-package: com.example.junior.Entity //This is the package mapper-locations: classpath:/mapper/*.xml //This is the mapping file for sql statements
Another noteworthy point is JuniorApplication.java, which is not only a boot class, but also a configuration class. So there are some configurations that need to be written here.
@SpringBootApplication@EnableTransactionManagement@EnableCaching@EnableScheduling //I have a timer, this annotation is to let it discover the timer @MapperScan(basePackages = "com.example.junior.Dao") //Let it discover your Dao layer public class JuniorApplication { public static void main(String[] args) { SpringApplication.run(JuniorApplication.class, args); }}If you forget to check some dependencies when creating a project, it doesn't matter, you can add dependencies in the pom.xml file. After adding it, right-click Maven in pom.xml -> Reimport and it will be OK
Summarize
The above is the entire content of this article. I hope that the content of this article has certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support to Wulin.com.