springboot整合mybatis中的问题及出现的一些问题小结
1.springboot整合mybatis mapper注入时显示could not autowire,如果强行写(value = false ),可能会报NullPointException异常
解决方案:
dao层加注解@Component(value = "首字母小写的接口名如UserMapper->userMapper")
dao层还可以加注解@Mapper
2.The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone问题
3.java.lang.IllegalArgumentException: DefaultSerializer requires a Serializable payload but received an object of type[xxx]
解决:实体对象类没有序列化,需要implements Serializable
PS:下面看下springboot整合mybatis出现的一些问题
springboot整合mybatis非常非常的简单,简直简单到发指。但是也有一些坑,这里我会详细的指出会遇到什么问题,并且这些配置的作用
整合mybatis,无疑需要mapper文件,实体类,dao层,数据库连接池。。。。。也就没了。
先放配置application.yml
spring: datasource: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 name: test url: jdbc:mysql://localhost:3306/mama-bike?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull username: root password: root mybatis: #告诉spring你的mapper的位置。 mapper-locations: classpath:com/coder520/mamabike/**/**.xml #告诉spring你的实体类的位置 type-aliases-package: classpath:com.coder520.mamabike.**.entity logging: config: classpath:logback.xml
dao层接口 //就简单的写一个方法
public interface UserMapper { int insert(User record); }
mapper
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.coder520.mamabike.user.dao.UserMapper" > <resultMap id="BaseResultMap" type="com.coder520.mamabike.user.entity.User" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="nickname" property="nickname" jdbcType="VARCHAR" /> <result column="enable_flag" property="enableFlag" jdbcType="TINYINT" /> <result column="verify_flag" property="verifyFlag" jdbcType="TINYINT" /> <result column="head_img" property="headImg" jdbcType="VARCHAR" /> <result column="mobile" property="mobile" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, nickname, enable_flag, verify_flag, head_img, mobile </sql> <insert id="insert" parameterType="com.coder520.mamabike.user.entity.User" > insert into user (id, nickname, enable_flag, verify_flag, head_img, mobile ) values (#{id,jdbcType=BIGINT}, #{nickname,jdbcType=VARCHAR}, #{enableFlag,jdbcType=TINYINT}, #{verifyFlag,jdbcType=TINYINT}, #{headImg,jdbcType=VARCHAR}, #{mobile,jdbcType=VARCHAR} ) </insert> </mapper>
main方法
@SpringBootApplication @ComponentScan(basePackages={"com.coder520.mamabike"}) @MapperScan(basePackages="com.demo.user.mapper") public class MamaBikeApplication { public static void main(String[] args) { SpringApplication.run(MamaBikeApplication.class, args); } }
需要注意的是,dao层接口spring怎么会知道呢?这里就需要@MapperScan(basePackages="com.demo.user.mapper")
这个注解来指定mapper接口的位置。用@ComponentScan(basePackages={"com.coder520.mamabike"})
这个注解来让spring扫描我们指定包下的注解。
如果我们不用@MapperScan这个注解的话,也可以在接口类的上方加上@Mapper这个注解也可以。
总结
以上所述是小编给大家介绍的springboot整合mybatis中的问题及出现的一些问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!
您可能感兴趣的文章:
- SpringBoot Controller Post接口单元测试示例
- 详解springboot整合Listener的两种方式
- Springboot整合Shiro之加盐MD5加密的方法
- springboot2.0集成rabbitmq的示例代码
- 如何解决SpringBoot2.x版本对Velocity模板不支持的方案
- Centos下SpringBoot项目启动与停止脚本的方法
- 详解springboot之jackson的两种配置方式
- SpringBoot2.0整合jackson配置日期格式化和反序列化的实现
- 详解springboot设置cors跨域请求的两种方式
- 详解在springboot中使用Mybatis Generator的两种方式
- Spring Boot单元测试中使用mockito框架mock掉整个RedisTemplate的示例
- 关于IDEA工具在springboot整合mybatis中出现的Invalid bound statement (not found)问题
- springboot多模块化整合mybatis,出现mapper自动注入失败问题
- 关于spring boot整合mybatis使用oracle数据库出现could not load:oracle.jdbc.driver.OracleDriver问题的终极解决方案
- spring boot整合shiro引用配置文件配置是出现的问题
- spring mybatis整合框架时由于mybatis版本与mysql客户端不匹配出现的问题
- spring 整合mybatis 出现解析不了配置文件的问题
- Spring与Mybatis的整合过程中遇到的一些问题
- SpringMvc+Mybatis整合hbase时出现的问题
- Maven项目中,关于Spring Boot 整合MyBatis时,Service层无法找到mapper接口的问题解决
- Spring Boot 整合Spring Security 和Swagger2 遇到的问题小结
- 在idea里使用SpringBoot整合MyBatis时遇到的Mapper扫描不到的问题
- [小技巧]关于spring boot 整合mybatis-plus: value为null时不返回的问题。
- springboot学习笔记-2 一些常用的配置以及整合mybatis
- SpringBoot整合Mybatis扫描不到Mapper的问题
- spring,mybatis整合时出现的一个小问题
- Spring Boot整合Activiti,查看流程图出现中文乱码问题
- spring boot版本更新出现的一些问题
- springboot整合Mybatis扫描不到mapper问题
- springboot与mybatis整合dao层不能注入的问题
- 关于Springboot整合mybatis启动的问题