SoFunction
Updated on 2025-03-08

How to use mybatis under springboot

Just use mybatis-spring-boot-starter. Simply put, mybatis saw that spring boot is so popular, so I came up with the mybatis-spring-boot-starter solution to better integrate with springboot.

See details

/spring/zh/

Introduce mybatis-spring-boot-starter pom file

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

Add related configuration

 = 
 = jdbc:mysql://localhost:3306/city?useUnicode=true&characterEncoding=utf-8
 = root
 = mysql

Springboot will automatically load.* related configurations, and the data source will be automatically injected into the sqlSessionFactory, and the sqlSessionFactory will be automatically injected into the mapper. By the way, you don’t have to worry about everything, just pick it up and use it.

-aliases-package=

This configuration is used to specify which package the bean is in, so as to avoid the bean not being found when the class with the same name exists

Add @MapperScan to the startup class to specify the location of dao or mapper package. You can specify multiple packages in the form of {"","}

@SpringBootApplication
@MapperScan("")
public class Application {
  public static void main(String[] args) {
    (, args);
  }
}

Or you can also specify mapper by adding annotations to the Mapper class. It is recommended to use the above. It is very troublesome to add annotations to each mapper. If it is a dao package, you should still use @MapperScan to specify the location.

Next, you can use the annotation mode to develop mapper, or use the xml mode to develop

Annotation mode

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

@Select is an annotation of the query class. All queries use this @Result to modify the result set returned, and the associated entity class attributes and database fields correspond one by one. If the entity class attributes and database attribute names are consistent, this attribute is not needed to modify. @Insert is inserted into the database and directly passed into the entity class will automatically parse the attribute to the corresponding value. @Update is responsible for modifying it, or you can directly pass into the object. @delete is responsible for deleting it. For more annotations, please refer to here.

/mybatis-3/zh/

xml mode

The xml mode maintains the old tradition of mapping files and needs to be added

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

Specify the mapping xml file location of mybatis In addition, you can also specify the configuration file of mybatis. If you need to add some basic configuration of mybatis, you can add the following configuration.

-locations=classpath:mybatis/

Specify the mybatis basic configuration file

You can add some basic configurations of mybatis, for example

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

Writing Dao layer code

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

The corresponding xml mapping file

<!DOCTYPE mapper
PUBLIC "-////DTD Mapper 3.0//EN"
"/dtd/">
<mapper namespace="">
  <select  parameterType="String" resultType="City">
    select * from city where state = #{state}
  </select></mapper>

Summarize

The above is the method of using mybatis under springboot introduced to you. I hope it will be helpful to everyone!