Springboot整合MyBatis

本文详细介绍了Springboot整合MyBatis的配置步骤,包括数据库连接配置、MyBatis的type-aliases-package和mapper-locations设置。在配置过程中遇到的URL未识别、返回类型未识别及Mapper找不到的问题,通过调整pom.xml资源过滤和检查接口与XML文件对应关系得以解决。同时强调了Mapper接口和XML文件命名的一致性对于正确运行的重要性。
摘要由CSDN通过智能技术生成

Springboot整合MyBatis

感谢大佬:
spring boot框架mybatis.mapper-locations配置问题详解_MarkZP-CSDN博客

  • 非spring官方写的用框架名开头

@mapper是给mybatis看的,@repository是给spring扫描组件注册到IOC容器里面用的

1、配置数据库信息:

spring:
  datasource:
    username: root
    password: 123456
    url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&serverTimezone=GMT
    driver-class-name: com.mysql.cj.jdbc.Driver
    type: com.alibaba.druid.pool.DruidDataSource #自定义数据眼
    #Spring Boot 默认是不注入这些属性值的,需要自己绑定
    #druid 数据源专有配置
    initialSize: 5
    minIdle: 5
    maxActive: 20
    maxWait: 60000
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: SELECT 1 FROM DUAL
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true

    #配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
    #如果允许时报错  java.lang.ClassNotFoundException: org.apache.log4j.Priority
    #则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
    filters: stat,wall,log4j
    maxPoolPreparedStatementPerConnectionSize: 20
    useGlobalDataSourceStat: true
    connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500


mybatis:
  type-aliases-package: com.pojo
  #若 mapper文件要和接口放一起,则不配置下列:
#  mapper-locations: classpath:mybatis/mapper/*.xml
2.报错:
not set url:

明明已经在yaml配置 url,未识别,可能是 maven配置资源过滤问题

pom.xml 配置:

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.xml</include>
        </includes>
        <filtering>true</filtering>
    </resource>
</resources>
<!--by kuang shen -->

或:(都尝试一下)

<resources>
    <resource>
        <directory>src/main/java</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
    <resource>
        <directory>src/main/resources</directory>
        <includes>
            <include>**/*.yml</include>
            <include>**/*.properties</include>
            <include>**/*.xml</include>
        </includes>
        <filtering>false</filtering>
    </resource>
</resources>
  1. 未识别返回类型:

原因: 未添加:

mybatis:
  type-aliases-package: com.pojo
  mapper-locations: classpath:com/dao/*.xml

注意

  • 此处 我是在接口所在包中加入接口

    image-20211028220710955

    • mapper-locations: 尽量去掉!!

  1. mapper找不到:
 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

​ 我写了全名反而找不到,奇奇怪怪

  1. 还有一件事

接口名字要与xml文件名字保持一致!

2、编写 pojo 实体类和Mapper 接口
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Department {
  private  Integer Did;
  private  String DepartmentName;

}
@Mapper
  • *表示本类是一个 MyBatis 的 Mapper。

  • 对应的xxxMapper.xml就是来实现这个Mapper。*

@Mapper
@Repository
public interface DepartmentMapper {
    int  addDepartment(Department Department);
    int  deleteDepartment(Integer id);
    int  updateDepartment(Department Department);
    List<Department> findAllDepartment();
    Department findDepartmentbyid(Integer id);   
    
}
3、 controller
@Controller

public class Indexcontroller {
    @Autowired
    private DepartmentMapper departmentMapper;

    @RequestMapping("/findAllDepartment")
    @ResponseBody
    public List<Department> findAllDepartment(Model model){
        List<Department> list = departmentMapper.findAllDepartment();
        for (Department department : list) {
            System.out.println(department);
        }
        return list;
    }


}

结果:

终于出来了,好累
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值