看到张图,画的挺好:
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