1.springboot整合mybatis mapper注入時(shí)顯示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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
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層接口 //就簡單的寫一個(gè)方法
1
2
3
|
public interface usermapper { int insert(user record); } |
mapper
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
<?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方法
1
2
3
4
5
6
7
8
|
@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")
這個(gè)注解來指定mapper接口的位置。用@componentscan(basepackages={"com.coder520.mamabike"})
這個(gè)注解來讓spring掃描我們指定包下的注解。
如果我們不用@mapperscan這個(gè)注解的話,也可以在接口類的上方加上@mapper這個(gè)注解也可以。
總結(jié)
以上所述是小編給大家介紹的springboot整合mybatis中的問題及出現(xiàn)的一些問題小結(jié),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!
原文鏈接:https://blog.csdn.net/qq_41602595/article/details/84316153