学习@Mapper

看到张图,画的挺好:

https://blog.csdn.net/qq_38129062/article/details/88967217

实际上手,各种错误,记录如下:

1. No MyBatis mapper was found in '[xx.mapper]' package. Please check your configuration.
https://blog.csdn.net/qinxian20120/article/details/80255976


2. Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.

Reason: Failed to determine a suitable driver class

这是因为没创数据库,要先创数据库!

  • sql-front每一步操作都闪退,但貌似没什么影响:

https://blog.csdn.net/qq_30532777/article/details/106081526

  • config->database.properties的datasource.url要设置些啥:
spring.datasource.name=mydb
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=****
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.type-aliases-package=com.jiayuezh.hello.model
mybatis.mapper-locations=classpath:mapper/*.xml

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
  • 依赖: 
		<!-- 添加MySQL数据库驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!-- 添加数据库连接池 -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid-spring-boot-starter</artifactId>
			<version>${druid.version}</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!--通用mapper-->
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>${tk.mybatis.version}</version>
		</dependency>
		<dependency>
  • 最后在main里导入config->database.properties:

@Autowired

Config config; 


3. Field config in com.jiayuezh.hello.HelloApplication required a bean of type 'tk.mybatis.mapper.entity.Config' that could not be found.

是上一步的autowired config 不对劲,引用了'tk.mybatis.mapper.entity.Config',而不是我自己写在config下的配置

 

 4.把autowired刪掉, 出現:

If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active)

在main函数加:

@PropertySource({"database.properties"}))

 

5.class path resource [database.properties] cannot be opened because it does not exist

将database.properties放到resources目录下

 

6. Invocation of init method failed; nested exception is tk.mybatis.mapper.MapperException: tk.mybatis.mapper.MapperException: java.lang.NoClassDefFoundError: javax/persistence/Table

把主函数的MapperScan改成如图,再在引用userMapper的地方,手动import com.jiayuezh.hellp.mapper

@SpringBootApplication
@RestController
@tk.mybatis.spring.annotation.MapperScan("com.jiayuezh.hellp.mapper") //原先还写了model
@PropertySource(value="database.properties")

运行,竟然可以了...

明天来试试增删改查。

让你远离sql语句的Mybatis工具:Tkmybatis,增删改查。

https://baijiahao.baidu.com/s?id=1634366904707283647&wfr=spider&for=pc

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值