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(七):springboot+mybatis多数据源最简解决方案
springboot(五):spring data jpa的使用
springboot(十五):springboot+jpa+thymeleaf增删改查示例
一、mybatis常见库:
MyBatis Generator (MBG)
自动生成dao类mapper,mapper.xml和对应数据库字段的entity实体类
PageHelper
用于分页
通用mapper,
极其方便的使用MyBatis单表的增删改查
MyBatis-Plus(MP)
是一个 MyBatis 的增强工具,
在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。
二、mybatis集成demo:
原生mybatis可以使用xml,也可注解形式来操作数据库。
原生:
库的使用:
mybatis-generator + 通用Mapper的使用
其他:关于jpa的demo:
三、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;