SpringBoot学习笔记之——整合mybatis

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_38863292/article/details/80609704

一:首先是pom.xml文件如下:

   
<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.2</version>
		</dependency>

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.0</version>
		</dependency>



这里用的是alibaba的druid数据库连接池。

二:在src/main/resources下创建mapper文件夹,用来存放*Mapper.xml文件。

三:修改application.properties文件:

server.port=8080
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url = jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 
spring.datasource.driverClassName = com.mysql.jdbc.Driver
mybatis.mapper-locations=classpath*:mapper/*Mapper.xml  
mybatis.type-aliases-package=com.hongcj.bean 

mybatis.mapper-locations为扫描mapper.xml文件的

mybaits.type-aliases-package 一般对应我们的实体类所在的包

每个属性值后千万千万不要有空格,被坑惨了。

四:修改主程序入口

添加注解 @MapperScan("com.hongcj.dao")  值为dao层。

配置完毕。

五:生成mapper.xml文件。

这里我用的mybatis的generator生成实体类和对应的mapper

最后的项目结构如图(listenner、filter和util请忽略):








展开阅读全文

springboot整合mybatis 启动报错

08-07

[code=text]rnjava.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.mybatis.spring.annotation.MapperScan.factoryBean()rn at java.lang.reflect.Method.getDefaultValue(Method.java:612) ~[na:1.8.0_131]rn at sun.reflect.annotation.AnnotationType.(AnnotationType.java:132) ~[na:1.8.0_131]rn at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:85) ~[na:1.8.0_131]rn at sun.reflect.annotation.AnnotationParser.parseAnnotation2(AnnotationParser.java:266) ~[na:1.8.0_131]rn at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:120) ~[na:1.8.0_131]rn at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:72) ~[na:1.8.0_131]rn at java.lang.Class.createAnnotationData(Class.java:3521) ~[na:1.8.0_131]rn at java.lang.Class.annotationData(Class.java:3510) ~[na:1.8.0_131]rn at java.lang.Class.getAnnotations(Class.java:3446) ~[na:1.8.0_131]rn at org.springframework.core.type.StandardAnnotationMetadata.(StandardAnnotationMetadata.java:70) ~[spring-core-5.0.8.RELEASE.jar:5.0.8.RELEASE]rn at org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition.(AnnotatedGenericBeanDefinition.java:58) ~[spring-beans-5.0.8.RELEASE.jar:5.0.8.RELEASE]rn at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.doRegisterBean(AnnotatedBeanDefinitionReader.java:216) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]rn at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.registerBean(AnnotatedBeanDefinitionReader.java:145) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]rn at org.springframework.context.annotation.AnnotatedBeanDefinitionReader.register(AnnotatedBeanDefinitionReader.java:135) ~[spring-context-5.0.8.RELEASE.jar:5.0.8.RELEASE]rn at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:158) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:135) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.BeanDefinitionLoader.load(BeanDefinitionLoader.java:127) ~[spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.SpringApplication.load(SpringApplication.java:704) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.SpringApplication.prepareContext(SpringApplication.java:393) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.SpringApplication.run(SpringApplication.java:328) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.SpringApplication.run(SpringApplication.java:1258) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at org.springframework.boot.SpringApplication.run(SpringApplication.java:1246) [spring-boot-2.0.4.RELEASE.jar:2.0.4.RELEASE]rn at com.example.TestDemoApplication.main(TestDemoApplication.java:12) [classes/:na]rn[/code]rnspringboot版本 2.0.4.RELEASE mybatis版本 3.4.6 mybatis-spring版本 1.3.2 论坛

没有更多推荐了,返回首页