The method of USING mybatis under springboot

  • 2020-11-26 18:47:06
  • OfStack

Just use ES1en-ES2en-ES3en-ES4en. Simply put, mybatis saw how popular spring boot was and came up with the mybatis-ES9en-ES10en-ES11en solution to better integrate with springboot

As shown in the

http://www.mybatis.org/spring/zh/index.html

Introduce the pom file for ES21en-ES22en-ES23en-ES24en


<dependency>  
  <groupId>org.mybatis.spring.boot</groupId>  
  <artifactId>mybatis-spring-boot-starter</artifactId>  
  <version>1.1.1</version>  
</dependency>

application. properties adds the relevant configuration


spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = mysql

springboot will automatically load the configuration of ES35en.datasource.*, and the data source will be injected into sqlSessionFactory, and sqlSessionFactory will be injected into Mapper automatically. By the way, you don't have to worry about every cut, just pick it up and use it.


mybatis.type-aliases-package=com.test.demo.model

This configuration is used to specify which package bean is in so that bean cannot be found with the same name class

Add @MapperScan to the boot class to specify the location of dao or mapper packages. Multiple packages can be specified as {"",""}


@SpringBootApplication
@MapperScan("com.test.demo.dao")
public class Application {
  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }
}

You can also specify mapper by adding @Mapper annotation to the Mapper class. It is recommended to use the above annotation. It is quite troublesome to add several annotations to each mapper

Next, you can develop mapper using the annotation pattern or xml pattern

Annotation model


@Mapper
public interface CityMapper {
  @Select("select * from city where state = #{state}")
  City findByState(@Param("state") String state);
}

@ES69en is the annotation of the query class. All queries use this @ES70en annotation to decorate the returned result set. The associated entity class attribute corresponds to the database field 11. @ES71en is used to insert into the database. Passing in the entity class will automatically parse the attribute to the corresponding value @Update for modification, or directly passing in the object @delete for deletion

http://www.mybatis.org/mybatis-3/zh/java-api.html

xml mode

The xml schema maintains the old tradition of mapping files, and ES83en.properties needs to be added


mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

Specify the mapping xml file location for mybatis In addition, you can specify the configuration file for mybatis, and if you need to add 1 base configuration for mybatis, you can add the following configuration


mybatis.config-locations=classpath:mybatis/mybatis-config.xml

Specify the mybatis base profile

mybatis-config.xml can add 1 configuration of the mybatis base, for example


<configuration>
  <typeAliases>
    <typeAlias alias="Integer" type="java.lang.Integer" />
    <typeAlias alias="Long" type="java.lang.Long" />
    <typeAlias alias="HashMap" type="java.util.HashMap" />
    <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
    <typeAlias alias="ArrayList" type="java.util.ArrayList" />
    <typeAlias alias="LinkedList" type="java.util.LinkedList" />
  </typeAliases>
</configuration>

Write code for the Dao layer


public interface CityDao {
  public City selectCityByState(String State);
}

The corresponding xml mapping file


<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.test.demo.dao.CityDao">
  <select id="selectCityByState" parameterType="String" resultType="City">
    select * from city where state = #{state}
  </select></mapper>

conclusion

Above is the site to you to introduce springboot mybatis method, I hope to help you!


Related articles: