一:mapper接口生成代理类
1.配置类上加
@MapperScan(basePackages = {"com.zzy.mapper"})
2.mapper接口上加
@Mapper
二:mapper Xml找不到?
1.直接在mapper方法上用注解
@Delete("delete from cmall.logistics_info where id = #{id}")
int deleteById(Integer id);
**2.配置文件
#mybtis的,功能比较少
mybatis:
#找到xml
mapper-locations: classpath:/mapper/*Mapper.xml,classpath:/mapper/**/*Mapper.xml
#给指定包下的类起别名
typeAliasesPackage: com.zzy.entity
#mybatis-plus下的,功能强大
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml,classpath:/mapper/**/*Mapper.xml
typeAliasesPackage: com.zzy.entity
#打印日志,服务器上不要打开
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
三 No MyBatis mapper was found in ‘[启动类路径]’ package. Please check your configuration.
//为什么我明明在启动类加了这个
@MapperScan(basePackages = {"com.zzy.mapper"})
为什么还是报
No MyBatis mapper was found in '[启动类路径]' package. Please check your configuration.
并且还不影响项目,就是看的不舒服
简单说就是执行顺序的问题
最简单解决:
就是在启动类同级建一个mapper接口,并加上@Mapper,但没啥意义
出现原因:
一般是引入了高版本的mybatis-spring-boot-starter
详细可以看
https://www.cnblogs.com/yangshaoxiang/p/12974555.html
四 打印日志
1.yml
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
2.多数据源版本
@Bean("sqlSessionFactory")
public SqlSessionFactory sqlSessionFactory() throws Exception {
// 导入mybatissqlsession配置
MybatisSqlSessionFactoryBean sessionFactory = new MybatisSqlSessionFactoryBean();
// 指明数据源
sessionFactory.setDataSource(multipleDataSource(dataSource0(), dataSource1(), dataSource2()));
// 指明mapper.xml位置(配置文件中指明的xml位置会失效用此方式代替,具体原因未知)
sessionFactory.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/mapper/**Mapper.xml"));
// 指明实体扫描(多个package用逗号或者分号分隔)
sessionFactory.setTypeAliasesPackage("gsa.geographic.system.entity");
// 导入mybatis配置
MybatisConfiguration configuration = new MybatisConfiguration();
configuration.setJdbcTypeForNull(JdbcType.NULL);
configuration.setMapUnderscoreToCamelCase(true);
configuration.setCacheEnabled(false);
// 配置打印sql语句
configuration.setLogImpl(StdOutImpl.class);
sessionFactory.setConfiguration(configuration);
// 添加分页功能
sessionFactory.setPlugins(new Interceptor[]{
paginationInterceptor()
});
// 导入全局配置
sessionFactory.setGlobalConfig(globalConfiguration());
return sessionFactory.getObject();
}
mybatis-plus的yml
mybatis-plus:
# Mapper.xml 文件位置 Maven 多模块项目的扫描路径需以 classpath*: 开头
mapperLocations: classpath*:com/vanhr/**/xml/*Mapper.xml
# #MyBaits 别名包扫描路径,通过该属性可以给包中的类注册别名 实体扫描,多个package用逗号或者分号分隔
# typeAliasesPackage: com.vanhr.user.dao.entity
# #通过父类(或实现接口)的方式来限定扫描实体
# typeAliasesSuperType: com.vanhr.user.dao.entity.baseEntity
# #枚举类 扫描路径 如果配置了该属性,会将路径下的枚举类进行注入,让实体类字段能够简单快捷的使用枚举属性
# typeEnumsPackage: com.vanhr.user.dao.enums
# #启动时是否检查 MyBatis XML 文件的存在,默认不检查 仅限spring boot 使用
# checkConfigLocation : true
# #通过该属性可指定 MyBatis 的执行器,MyBatis 的执行器总共有三种:
# # ExecutorType.SIMPLE:该执行器类型不做特殊的事情,为每个语句的执行创建一个新的预处理语句(PreparedStatement)
# # ExecutorType.REUSE:该执行器类型会复用预处理语句(PreparedStatement)
# # ExecutorType.BATCH:该执行器类型会批量执行所有的更新语句
# executorType: SIMPLE
# # 指定外部化 MyBatis Properties 配置,通过该配置可以抽离配置,实现不同环境的配置部署
# configurationProperties:
configuration: # MyBatis 原生支持的配置
# 是否开启自动驼峰命名规则(camel case)映射
mapUnderscoreToCamelCase: true
# 枚举处理类,如果配置了该属性,枚举将统一使用指定处理器进行处理
# org.apache.ibatis.type.EnumTypeHandler : 存储枚举的名称
# org.apache.ibatis.type.EnumOrdinalTypeHandler : 存储枚举的索引
# com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler : 枚举类需要实现IEnum接口或字段标记@EnumValue注解.(3.1.2以下版本为EnumTypeHandler)
# defaultEnumTypeHandler: com.baomidou.mybatisplus.extension.handlers.MybatisEnumTypeHandler
# 配置JdbcTypeForNull, oracle数据库必须配置
jdbc-type-for-null: null
global-config: # 全局策略配置
# 是否控制台 print mybatis-plus 的 LOGO
banner: false
db-config:
# id类型
id-type: auto
# 表名是否使用下划线命名,默认数据库表使用下划线命名
table-underline: true
#是否开启大写命名,默认不开启
# capital-mode: false
# #逻辑已删除值,(逻辑删除下有效) 需要注入逻辑策略LogicSqlInjector 以@Bean方式注入
# logic-not-delete-value: 0
# #逻辑未删除值,(逻辑删除下有效)
# logic-delete-value: 1