欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringBoot整合Mybatis和MySql注意事项

程序员文章站 2024-03-24 16:32:34
...

1、所需依赖

  <!-- springBoot的启动器,没有该启动器跑不动,最为核心的-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--注意: springBoot 要求模板形式的视图层技术的文件必须要放到 src/main/resources 目录下必 须要一个名称为 templates-->
        <!-- logger -->
        <!--日志 start-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.7.25</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.7.25</version>
            <scope>test</scope>
        </dependency>
        <!--日志end-->
        <!--邮件依赖-->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.5.0-b01</version>
        </dependency>
        <!--junit依赖,主要在于TestController用到,修改server后,不需要安装,直接测试-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
        <!--ContextConfiguration的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>

        <!-- thymeleaf 的依赖及启动器
        视图目录位置:src/main/resources/templates templates:
        该目录是安全的。意味着该目录下的内容是不允许外界直接访问的。 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>


        <!-- SpringBoot 整合 Freemarker,freemarker 启动器的坐标 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>

        <!-- SpringBoot 整合 jstl表达式 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>
        <!-- SpringBoot整合jasper,Tomcat中对jsp处理的引擎 -->
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- springBoot整合Mybatis 启动器 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <!--<version>2.0.1</version>-->
            <version>1.1.1</version>
        </dependency>

        <!--  springBoot整合mysql 数据库驱动,版本不配置的话默认最高,可能出错 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>
        <!-- springBoot整合druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

2、添加 application.properties 全局配置文件

## 设置单个上传文件的大小
## spring.http.multipart.maxFileSize=200MB
## 设置一次请求上传文件的总容量
## spring.http.multipart.maxRequestSize=200MB
## 配置jsp访问的前缀
## spring.mvc.view.prefix=/WEB-INF/jsp/
## 配置jsp访问的后缀
## spring.mvc.view.suffix=.jsp
## 注意JSP好像不能和thymelea进行兼容!
## thymelea模板配置
## spring.thymeleaf.prefix=classpath:/templates/
## spring.thymeleaf.suffix=.html
## spring.thymeleaf.mode=HTML5
## spring.thymeleaf.encoding=UTF-8
#热部署文件,页面不产生缓存,及时更新
## spring.thymeleaf.cache=false
## spring.resources.chain.strategy.content.enabled=true
## spring.resources.chain.strategy.content.paths=/**
#添加 application.properties 全局配置文件
# 数据连接池
spring.datasource.driverClassName=com.mysql.jdbc.Driver
# 数据库
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
# 账号
spring.datasource.username=root
# 密码
spring.datasource.password=root
# 数据库类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 给包起别名
mybatis.type-aliases-package=com.springboot.common.pojo

3、service层识别不到mapper的解决方案!

在对应的mapper文件中,添加这个注释:@Repository
@Repository将生成的mapper交给spring管理,如果不交给spring管理,会报错,或者直接在mybatis里直接配置bean即可!
SpringBoot整合Mybatis和MySql注意事项

4、mybatis报错:org.apache.ibatis.binding.BindingException:Invalid bound statement (not found)的解决方案

因为识别不到对应的sql语句,扫描不到并且没有吧mapper.xml给与spring管理,所以会报错!

   <build>
		<resources>
           <resource>
               <directory>src/main/java</directory>
               <excludes>
                   <exclude>**/*.java</exclude>
               </excludes>
           </resource>
           <resource>
               <directory>src/main/resources</directory>
               <includes>
                   <include>**/*.*</include>
               </includes>
           </resource>
		  </resources>
    </build>

5、springboot启动器中加上注解:@MapperScan(“mapper所在包路径”)

package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author huaiyan
 * @create 2019-04-12 11:13
 * @description springboot的启动类 //@MapperScan 用户扫描MyBatis的Mapper接口
 * *@Copyright 该版权归属
@SpringBootApplication
@MapperScan("com.springboot.common.dao")
public class SpringBootDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }

}

SpringBoot整合Mybatis和MySql注意事项