Springboot Integrated MyBatis and Thymeleaf

Source: Internet
Author: User
Tags mysql connect smarty template

Http://www.cnblogs.com/ludashi/archive/2017/05/08/6669133.html

Last blog we talked about the "Java EE development of the Springboot project creation, operation and Configuration", from the content of the previous blog we are not difficult to see springboot convenient. This blog we continue on the basis of the previous blog to see how springboot is introduced and used MyBatis and thymeleaf. In our previous blog, we mentioned hibernate, and today's blog introduces the role of MyBatis as Hibernate, a set of ORM frameworks that are primarily responsible for persistence and the framework for mapping data from databases directly into model.

And Thymeleaf is a template engine, similar to the Smarty template engine we talked about earlier in PHP. If your Web project is separate from front to back, then you don't need to thymeleaf the template engine. What this blog is going to do is introduce mybatis in the Springboot project, and then read the information in the database through the mapping methods and annotations provided by MyBatis. Then use the Thymeleaf template to display the data at the front end.

We used to talk about the content of the swift development Service, which is the perfect framework, and it uses MySQL-related stuff. This blog we will use the perfect framework before the operation of the database on the line. For information on Swift's Perfect framework, please visit the service-side framework in Swift---Perfect series blog. Because I have talked about MySQL related things before, this blog will not do too much to repeat. For the installation of MySQL, see the previous blog "MacOS Sierra installation Apache2.4+php7.0+mysql5.7.16". In this blog, we'll focus on the MyBatis and thymeleaf in spring boot.

First, the introduction and use of MyBatis

1. Configuring the Pom file

First, let's look at how to introduce mybatis in spring boot. First we find MyBatis Spring Boot starter Related MVN warehouse connection in http://mvnrepository.com/. As shown below, from below, it is not difficult to see that the current version of MyBatis Spring Boot Starter is 1.2.0. However, in this blog we are using 1.1.1 version, because I introduced 1.2.0, my Springboot project will not start, so change to 1.1.1 version is OK.

  

Add the configuration items on the pom.xml below to introduce the Mybatis-spring-boot-starter and Mysql-connector-java related configurations. Mysql-connector-java Gu Mingsi is the dependency package that is used to connect to the MySQL database. The configuration in Pom.xml is as follows.

        <!--Https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter--        < dependency>            <groupId>org.mybatis.spring.boot</groupId>            <artifactId> mybatis-spring-boot-starter</artifactid>            <version>1.1.1</version>        </dependency >                <!--https://mvnrepository.com/artifact/mysql/mysql-connector-java--        <dependency>            <groupId>mysql</groupId>            <artifactId>mysql-connector-java</artifactId>            <version>5.1.38</version>        </dependency>

2. Configure MySQL database connection

Because we are using Spring boot introduced Mybatis, Mybatis Spring boot starter will do a lot of automated configuration for us, such as the creation of sqlsessionfactory and so on. All we need to do is to configure the database connection in the Application.properties file. Below is the information about the connection database that we added in the configuration file. The main configuration is the database connection path, the database user name and password, the database driver.

#Config MySQL Connect infospring.datasource.url=jdbc:mysql://127.0.0.1:3306/perfect_ Notespring.datasource.username=root[email protected] #spring. datasource.driver-class-name=com.mysql.jdbc.driver

3. Create Model class

Next we'll create the data model class to map the corresponding data in the database. Next we will manipulate the content table in the Perfect_note database as shown below. The data in the database below and in the database are the data we used to talk about the perfect framework, and we still work on the data in this blog post.

  

Based on the fields in the Cotent table above, we will create the model class for the table. This content class below is the model for the content table that we created. The details are as follows.

  

4. Create a mapped interface

Next we create a map interface to the database table and the model directly. The interface provides a direct mapping of the data to the model and provides related SQL operations. The Contentmapper interface below is the mapping between the content table we created and the Contentmodel. and the corresponding SQL operation is provided in the annotations corresponding to the methods in the interface.

In the query () method of the Contentmapper interface, the SQL statement corresponding to the method is "select * from Content", which is the data in all content. @Results annotations provide a mapping between the fields of a database table and the properties of the model. The table fields "id" and "contentId" are specified below, and the field name "Create_time" corresponds to the "Createtime" property, and the field name is not specified as the property name.

Querybyid () is a conditional query method. The parameter is the condition of the query. Binding a condition to a parameter by @param annotations. The specific code is shown below.

  

5. Create a controller for testing

For the sake of simplicity, we do not create a DAO layer, just call the method in the controller to invoke the above interface. The Mybatistestcontroller below is the test controller we created to test the above operation. Of course we declare the controller as @restcontroller so that we can test it. Then we use the @autowired annotation to inject an object of the Contentmapper type, which we can manipulate to the corresponding method in the above interface. The Querycontentbyid () method corresponds to the Querybyid () method in Contentmapper, and the Queryall () method corresponds to the query () method in Mapper, as shown in the following code.

Below we directly return the obtained model or model array, in spring boot, the directly returned model will be mapped to the corresponding JSON format of the data, which we will see visually later.

  

6. Access the above route

We first access the/querycontentbyid this route, and then we access this route, we are going to provide a parameter contentid. That is, the criteria used to query the data. The result below is the result of our conditional query on contentid=6.

  

Next, let's look at all the data, that is, access to the Queryall route. The specific results are shown below.

  

Introduction and use of thymeleaf template

The introduction of the above MyBatis is completed, the future blog will continue to mybatis related things to introduce. Next we introduce the Thymeleaf template and then show the relevant data that we read with MyBatis. Below we will look at how the Thymeleaf template is integrated in spring boot

1. Configuring the Pom.xml File

In Pom.xml, add the libraries associated with thymeleaf in spring boot, as follows:

        <!--https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-thymeleaf        -- <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> spring-boot-starter-thymeleaf</artifactid>            <version>1.5.2.RELEASE</version>        </ Dependency>

2. Create the controller used by the template

Next, we'll create the controller corresponding to the template, as shown below. Below we still use the injected Contentmapper object to get the data. The obtained data is then added to the model object, and when added, we specify a parameter name for the data object, such as "contents" below. Then return to the template page, "display" below is the name of the file where our template page.

  

3. Create a template page

Then we should create the template page, which is the display.html here. Below are all the contents of the Display.html page. We have added some CSS styles to the display of the data, and used the CDN to introduce the latest version of the bootstrap. The attribute with the "th:" prefix below is the label for the Thymeleaf template.

First Use "th:if=" ${not #lists. IsEmpty (contents)} "" To determine whether the value of the contents property is empty, and if not, the contents of the label are executed. Then use the th:if= "${not #lists. IsEmpty (contents)}" to traverse the contents of the contents, similar to the while loop. Extract the data for each item by th:each= "Content:${contents}". Remove the value of a property by th:text= "${content.contentid}". The details are as follows.

<! DOCTYPE html>

4. Access the above route

After creating the template for the presentation, the next thing we want to do is to access it. Below is the result of our final visit.

  

This blog will come here first, and later will use the other content of MyBatis, then elaborate.

GitHub address of the code involved in this blog: https://github.com/lizelu/SpringBootProject

Springboot Integrated MyBatis and Thymeleaf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.