Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to integrate mybatis3 in springboot2.0

Shulou Source: shulou.com Published: 2022-06-02 05:59:30 10月18日 Update

This article introduces how to integrate mybatis3 in springboot2.0. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

SpringBoot's support for MyBatis requires the following two dependencies to be added, which can be copied to the project's pom path

Org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.2 mysql mysql-connector-java

Then we need to configure MyBatis, including adding MyBatis classes, configuring related packet scanning paths, including scanning DAO, MODEL, and XML mapping files

Create a new MyBatis configuration class under the config package

Import org.apache.ibatis.session.SqlSessionFactory;import org.mybatis.spring.SqlSessionFactoryBean;import org.mybatis.spring.annotation.MapperScan;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.support.PathMatchingResourcePatternResolver;import javax.sql.DataSource;@Configurationpublic class MybatisConfig {@ Autowired private DataSource dataSource; @ Bean public SqlSessionFactory sqlSessionFactory () throws Exception {SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean () SessionFactory.setDataSource (dataSource); sessionFactory.setTypeAliasesPackage ("cn.mulc.swagger.entity"); / / scan Model PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver (); sessionFactory.setMapperLocations (resolver.getResources ("classpath*:/mybatis/*.xml")); / / scan mapping file return sessionFactory.getObject ();}}

Create a new database named demo, and then configure it in the application.yml file

Server: port: 8080spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/demo?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8 username: root password: root

Modify the startup class to add the package sweep configuration

@ SpringBootApplication@MapperScan ("cn.mulc.swagger.mapper") public class SwaggerApplication {public static void main (String [] args) {SpringApplication.run (SwaggerApplication.class, args);}}

Start the program, you can find that there is no error and the log has been typed out without mapper

On how to integrate mybatis3 in springboot2.0 to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Tags: Configuration file integration two content more path help good interest including name guy partner data database article log knowledge program Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker vpn Shulou Tech Info NVidia Microsoft