springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié)
1.springboot整合mybatis mapper注入時顯示could not autowire,如果強(qiáng)行寫(value = false ),可能會報(bào)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]
解決:實(shí)體對象類沒有序列化,需要implements Serializable
PS:下面看下springboot整合mybatis出現(xiàn)的一些問題
springboot整合mybatis非常非常的簡單,簡直簡單到發(fā)指。但是也有一些坑,這里我會詳細(xì)的指出會遇到什么問題,并且這些配置的作用
整合mybatis,無疑需要mapper文件,實(shí)體類,dao層,數(shù)據(jù)庫連接池。。。。。也就沒了。
先放配置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你的實(shí)體類的位置 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這個注解也可以。
總結(jié)
以上所述是小編給大家介紹的springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring使用注解方式實(shí)現(xiàn)創(chuàng)建對象
這篇文章主要介紹了Spring使用注解方式實(shí)現(xiàn)創(chuàng)建對象,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2023-02-02java實(shí)現(xiàn)識別二維碼圖片功能方法詳解與實(shí)例源碼
這篇文章主要介紹了java實(shí)現(xiàn)識別二維碼圖片,java無法識別二維碼情況下對二維碼圖片調(diào)優(yōu)功能方法與實(shí)例源碼,需要的朋友可以參考下2022-12-12springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證
本文主要介紹了springboot項(xiàng)目事務(wù)標(biāo)簽驗(yàn)證,文中通過示例代碼介紹的非常詳細(xì),詳細(xì)的介紹了不加事務(wù)標(biāo)簽和加事物標(biāo)簽的使用,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07springboot中如何配置LocalDateTime JSON返回時間戳
這篇文章主要介紹了springboot中如何配置LocalDateTime JSON返回時間戳問題。具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06Spring Security結(jié)合JWT的方法教程
這篇文章主要給大家介紹了關(guān)于Spring Security結(jié)合JWT的方法教程,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12springboot實(shí)現(xiàn)攔截器的3種方式及異步執(zhí)行的思考
實(shí)際項(xiàng)目中,我們經(jīng)常需要輸出請求參數(shù),響應(yīng)結(jié)果,方法耗時,統(tǒng)一的權(quán)限校驗(yàn)等。本文首先為大家介紹 HTTP 請求中三種常見的攔截實(shí)現(xiàn),并且比較一下其中的差異。感興趣的可以了解一下2021-07-07java fastJson轉(zhuǎn)JSON兩種常見的轉(zhuǎn)義操作
在實(shí)際開發(fā)中,我們有時需要將特殊字符進(jìn)行轉(zhuǎn)義,本文主要介紹了java fastJson轉(zhuǎn)JSON兩種常見的轉(zhuǎn)義操作,具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03