Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to integrate mybatis3 in springboot2.0

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report