springboot整合mybatis中的問題及出現的一些問題小結

這篇文章主要介紹了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中的問題及出現的一些問題小結,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回覆大家的。在此也非常感謝大家對神馬文庫網站的支持!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章