Today I tried to build a Spring Boot +Mybatis, and found that it is much simpler than the original Spring+SpringMVC+Mybatis!! Although said only Spring Boot can also be developed, but for multi-table multi-condition paging query, Spring Boot is a bit inadequate, so LZ Mybatis integration, found that this work twice the result with half the effort! Wish I had built it earlier!!

This article focuses on how Springboot can integrate MyBatis, using XML configuration SQL instead of annotations. Most importantly, SQL and business code should be isolated to facilitate SQL collation with dbAs.

  1. Build Java Web development environment online tutorial many, their own Baidu ha
  2. Spring the boot up

1. Intellij IDEA menu bar File-> New -> Project, select Spring Initializr in the left bar, select JDK version and default Service URL on the right side, click Next.



2. Fill in the Group and Artifact information for the project, and click Next



3. Click Web on the left side, select Web on the middle side, select SQL on the left side, select MyBatis and MySQL on the middle side, and click Next



4. Fill in the Project name and click Finish.



XML, Spring Boot, mysql data connection and other related JAR packages.

  1. Database preparation

    Create a new database using MySql:

  2. Mybatis – Generator Mybatis – Gennerator can automatically generate DAO, bean, mapper XML files required by MyBatis. 1. Create a folder and name it Generator. 2. Prepare the required JAR package, mybatis- Generator-core-1.3.6. jar. Download: github.com/mybatis/gen… 3. Create a generator-xml file in mybatis-generator-core-1.3.6 and set it as follows:

    123456789101112131415161718192021222324252627282930313233

    Note: references to entity “useSSL” must end with “; ‘end of delimiter otherwise an error will be reported)

    4. Run the Java -jar mybatis-generator-core-1.3.6.jar -configfile generator-xml -overwrite command

    5. After success, you can see that the DAO, Model, and Mapper XML files have been generated

    1. Pom.xml adds the necessary dependencies

      4.0.0

      < the groupId > com. Example < / groupId > < artifactId > demo < / artifactId > < version > 0.0.1 - the SNAPSHOT < / version > <packaging>jar</packaging> <name>demo</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> . < version > 2.0.0 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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> < artifactId > mybatis - spring - the boot - starter < / artifactId > < version > 1.3.2 < / version > < / dependency > < the 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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>Copy the code

      1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465

    2. application.properties

    Open the application.properties file and modify the corresponding data source configuration, such as address, account, password, etc.

    spring.datasource.url=jdbc:mysql://localhost:3306/mydemo? useUnicode=true&characterEncoding=utf8&useSSL=false spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver mybatis.typeAliasesPackage=com.example.demo.dal.DO mybatis.mapperLocations=classpath:mapper/*.xml 123456Copy the code

    (Make corresponding changes according to the project catalog. Mybatis. TypeAliasesPackage make entity object package, with a database table one to one correspondence; Mybatis. MapperLocations is the location of the mapper file.

    1. dao

      The DAO file that needs to be configured for the operation data table interface. Dao files are scanned with the @mapperscan annotation.

    2. controller

    3. run

    Right-click the DemoApplication startup class’s main function and access the data in your browser

    See here friends, if you like this article, don’t forget to forward, favorites, message interaction!

    If you have any questions about this article, please feel free to contact me in the comments section

    Recently I have sorted out some Java materials, including interview sharing, simulation test questions, and video dry goods, if you need, welcome to private message me!