栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot整合MyBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Springboot整合MyBatis

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
        
        true
    


或:(都尝试一下)


    
        src/main/java
        
            ***.properties
            ***.yml
            ***.xml
        
        false
    

  1. 未识别返回类型:

原因: 未添加:

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

注意

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

    • 故 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 findAllDepartment();
    Department findDepartmentbyid(Integer id);   
    
}
3、 controller
@Controller

public class Indexcontroller {
    @Autowired
    private DepartmentMapper departmentMapper;

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


}

结果:

终于出来了,好累

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/358555.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号