服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - springboot整合mybatis中的问题及出现的一些问题小结

springboot整合mybatis中的问题及出现的一些问题小结

2021-06-15 10:20King丶Star Java教程

这篇文章主要介绍了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问题

springboot整合mybatis中的问题及出现的一些问题小结

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

?
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你的实体类的位置
 type-aliases-package: classpath:com.coder520.mamabike.**.entity
 
logging:
 config: classpath:logback.xml

dao层接口      //就简单的写一个方法

?
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") 这个注解来指定mapper接口的位置。用@componentscan(basepackages={"com.coder520.mamabike"})这个注解来让spring扫描我们指定包下的注解。

如果我们不用@mapperscan这个注解的话,也可以在接口类的上方加上@mapper这个注解也可以。

总结

以上所述是小编给大家介绍的springboot整合mybatis中的问题及出现的一些问题小结,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://blog.csdn.net/qq_41602595/article/details/84316153

延伸 · 阅读

精彩推荐