当前位置: 代码迷 >> 综合 >> SpringBoot2.3.4+mybatis3.4配置与使用
  详细解决方案

SpringBoot2.3.4+mybatis3.4配置与使用

热度:8   发布时间:2024-03-08 12:21:48.0

详细安装步骤看博客:https://www.cnblogs.com/l-y-h/p/12859477.html

在安装过程中主要遇到的问题:

 1)当前的项目能启动,但是启动打印3行日志后就停止了。解决方法:

 添加依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

添加注解:

 

2)报错:Property 'sqlSessionFactory' or 'sqlSessionTemplate' 解决方法:

 1、降级包(没用过,太low)

 2、创建一个适用于所有Dao的基类CommonDao,通过CommonDao来继承SqlSessionDaoSupport,通过set方法注入SqlSessionFactory(没用过,我的Dao是接口呀,这个方法根本也用不了)

3、配置类方法(简单):新建配置内如下:

package com.stars.api.config;import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
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.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;import javax.sql.DataSource;/*** @author cangyue* @Deseription* @create 2019/2/25 16:30**/
@Configuration
@EnableConfigurationProperties(DataSourceProperties.class) //不加DataSourceProperties会注入失败
public class MybatisConfig {@Autowired(required = true)private DataSourceProperties dataSourceProperties;@Bean(name = "dataSource")public DataSource dataSource() {DruidDataSource dataSource = new DruidDataSource(); //这里用的是druiddataSource.setUrl(dataSourceProperties.getUrl());dataSource.setDriverClassName(dataSourceProperties.getDriverClassName());dataSource.setUsername(dataSourceProperties.getUsername());dataSource.setPassword(dataSourceProperties.getPassword());return dataSource;}@Beanpublic SqlSessionFactory sqlSessionFactory() throws Exception {MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean(); //一定要用MybatisSqlSessionFactoryBean 不然会报org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):sqlSessionFactoryBean.setDataSource(dataSource());return sqlSessionFactoryBean.getObject();}@Beanpublic PlatformTransactionManager transactionManager() {return new DataSourceTransactionManager(dataSource());}}

3)相关配置

 

4)完整的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.4.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.stars</groupId><artifactId>demo</artifactId><version>0.0.1</version><name>stars</name><description>Stars project for Spring Boot</description><properties><java.version>11</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId><version>2.3.4.RELEASE</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-tx</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus</artifactId><version>3.4.0</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.18</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.10</version></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.3.1.tmp</version></dependency><!-- 添加 模板引擎 依赖 --><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope><exclusions><exclusion><groupId>org.junit.vintage</groupId><artifactId>junit-vintage-engine</artifactId></exclusion></exclusions></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

 

5)项目结构:

  相关解决方案