One: import jar package
Maven introduces jar package (mybatis+connector)
<!--mybatis--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <!--Connect driver--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency>
Two: Configure the main configuration file of mybatis
The main configuration file of mabatis is named mybatis-config.xml as follows
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- Configure global properties--> <settings> <!-- Use jdbc's getGeneratedKeys to get the database self-incrementing primary key value --> <setting name="useGeneratedKeys" value="true" /> <!-- Replace column names with column aliases Default: true --> <setting name="useColumnLabel" value="true" /> <!-- Turn on camel case name conversion: Table{create_time} -> Entity{createTime} --> <setting name="mapUnderscoreToCamelCase" value="true" /> <!-- print query statement--> <setting name="logImpl" value="STDOUT_LOGGING" /> </settings> </configuration>
The content is as above, and it will not be explained if a comment is added.
Two: Configure the mapper file of mybatis (xml uses mybatis)
1: The mapper file is mainly a configuration file for storing sql statements, the following AreaMapper
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.project.myo2o.mapper.AreaMapper"> <select id="queryArea" resultType="com.project.myo2o.entity.Area"> SELECT * FROM tb_area ORDER BY priority DESC </select> </mapper>
<mapper namespace="com.project.myo2o.mapper.AreaMapper">This tag fills in the location of the mapper interface class
<select id="queryArea" resultType="com.project.myo2o.entity.Area">
id is a method name in the mapper interface class, resultType is the mapping type of the result set, select the entity class (the mapping of the result set can be defined by yourself, and the label is resultMap)
2: The mapper file of the interface class is as follows AreaMapper
import com.project.myo2o.entity.Area; import java.util.List; public interface AreaMapper { List<Area> queryArea(); }
Three: Read the mapper file in the startup class
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletComponentScan; @SpringBootApplication @ServletComponentScan @MapperScan("com/project/myo2o/mapper") public class Myo2oApplication { public static void main(String[] args) { SpringApplication.run(Myo2oApplication.class, args); } }
@MapperScan("com/project/myo2o/mapper") This read must be written, otherwise springboot cannot read it. Of course, @Mapper annotation can also be added to the mapper file, but it is not recommended, this is more convenient
Four: configure the yml file (properties file)
spring: datasource: url: jdbc:mysql://localhost:3307/myo2o?useUnique=true&characterEncoding=utf8&useSSL=false username: root password: 123456 driverClassName: com.mysql.jdbc.Driver
Add the above database connection file, then mybatis can automatically read the configuration and load it
Five: use
add a serviceImpl
@Service public class AreaServiceImpl implements AreaService { @Autowired private AreaMapper areaMapper; @Override public List<Area> find() { List<Area> areaList = areaMapper.queryArea(); return areaList; } }
Directly inject the mapper interface, you can call the method inside
Six: Summary
This is the first time to write a blog, the logic is not very clear, summarize the above content, if there is something wrong, please lightly spray ~~~~~~~
First import the jar package, and then configure the main configuration file and mapper configuration file of mybatis (in fact, the database information configured in yml can be directly configured in the main configuration file of mybatis, but this is not done for the subsequent connection pool use), configure After the database information in yml and the path to scan mapper in the startup class, you can directly manipulate the database through the mapper interface.
For the specific working principle and process of mybatis, you can look at the source code and other tutorials, and follow-up updates to add the use of druid connection pool.
https://blog.csdn.net/qq_36784299/article/details/80250691 springboot+mybatis+druid connection pool
https://download.csdn.net/download/qq_36784299/10402440 source code