mybatis常见库及问题汇总

orm框架的本质是简化编程中操作数据库的编码,发展到现在基本上就剩两家了,
一个是宣称可以不用写一句SQL的hibernate,
一个是可以灵活调试动态sql的mybatis,
两者各有特点,在企业级系统开发中可以根据需求灵活使用。
发现一个有趣的现象:
传统企业大都喜欢使用hibernate,互联网行业通常使用mybatis。

hibernate特点就是所有的sql都用Java代码来生成,不用跳出程序去写(看)sql,有着编程的完整性,
发展到最顶端就是spring data jpa这种模式了,基本上根据方法名就可以生成对应的sql了。

mybatis初期使用比较麻烦,需要各种配置文件、实体类、dao层映射关联、还有一大推其它配置。
当然mybatis也发现了这种弊端,
初期开发了generator可以根据表结果自动生产实体类、配置文件和dao层代码,可以减轻一部分开发量;
后期也进行了大量的优化可以使用注解了,自动管理dao层和配置文件等,发展到最顶端就是今天要讲的这种模式了,
mybatis-spring-boot-starter就是springboot+mybatis可以完全注解不用配置文件,也可以简单配置轻松上手。

参考:

springboot-mybatis多数据源的两种整合方法

springboot(六):如何优雅的使用mybatis

springboot(七):springboot+mybatis多数据源最简解决方案

springboot(五):spring data jpa的使用

springboot(十五):springboot+jpa+thymeleaf增删改查示例

一、mybatis常见库:

MyBatis Generator (MBG)

自动生成dao类mapper,mapper.xml和对应数据库字段的entity实体类

mybatis/generator

PageHelper

用于分页

pagehelper/Mybatis-PageHelper

通用mapper,

极其方便的使用MyBatis单表的增删改查

abel533/Mapper

MyBatis-Plus(MP)

是一个 MyBatis 的增强工具,

在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。

baomidou/mybatis-plus

二、mybatis集成demo:

原生mybatis可以使用xml,也可注解形式来操作数据库。

原生:

xml方式原生mybatis

注解方式原生mybatis

原生mybatis(注解)多数据源

原生mybatis(xml)多数据源

库的使用:

MyBatis Generator的使用

mybatis-generator + 通用Mapper的使用

MyBatis-Plus的使用

其他:关于jpa的demo:

spring data jpa

三、mybatis常见问题:

问题一:

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'com.example.demo.DemoApplicationTests': 
Unsatisfied dependency expressed through field 'userMapper'; 

nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 
No qualifying bean of type 'com.example.demo.dao.UserMapper' available: 
expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: 
{@org.springframework.beans.factory.annotation.Autowired(required=true)}

原因:找不到mapper的Java文件,缺少@Mapper或者@MapperScan(“Mapper包名”),添加即可,如下:

@Mapper
public interface UserMapper {
    int deleteByPrimaryKey(Integer id);
    int insert(User record);
    User selectByPrimaryKey(Integer id);
    List<User> selectAll();
    int updateByPrimaryKey(User record);
}

@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class DemoApplication {

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

}

注意:只要有mapper的Java文件就需要配置。

问题二:

org.apache.ibatis.binding.BindingException: 
Invalid bound statement (not found): com.example.demo.dao.UserMapper.selectByPrimaryKey

//或

org.apache.ibatis.binding.BindingException: 
Invalid bound statement (not found): com.example.demo.dao.BookDao.getAll

原因:找不到mapper的xml文件,【缺少】以下配置或者【路径错误】:

# mapper
mybatis.mapper-locations=classpath:mapper/*.xml

注意:只要有mapper的xml文件就需要配置。

问题三

java.sql.SQLException: 

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. 

You must configure either the server or JDBC driver (via the serverTimezone configuration property) 

to use a more specifc time zone value if you want to utilize time zone support.

数据库配置需要添加时区:&serverTimezone=GMT,如下:

# database
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=admin
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

问题四

### Cause: com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: 
Data truncation: Out of range value for column 'id' at row 1
; Data truncation: Out of range value for column 'id' at row 1; 
nested exception is com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: 
Data truncation: Out of range value for column 'id' at row 1] with root cause

将字段的类型由int转为BIGINT,如下:

ALTER TABLE user MODIFY id BIGINT(20) AUTO_INCREMENT;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值