第一步,连接数据库
-
万事先导包儿
-
在pom.xml中导入这两个包,默认的数据库的版本是8.0以上滴,不信你自己看
-
<!-- mysql--> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <!-- jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
-
当然是编写驱动啥的
-
表自己建
-
在applcation.yaml文件中,导入以下配置
-
#连接数据库 spring: datasource: username: root password: 123456 url: jdbc:mysql://localhost:3306/depart?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false driver-class-name: com.mysql.cj.jdbc.Driver
-
编写测试
-
@SpringBootTest class Spingboot04ApplicationTests { @Autowired private DataSource dataSource; @Test void contextLoads() throws SQLException { System.out.println(dataSource.getClass()); System.out.println(dataSource.getConnection()); } }
如果运行成功,就是代表连接上了!!!!!!!
第二步整合 Mybatis
-
还是老话,万事,先导包
-
查看版本匹配问题【这很重要】
-
当前是如下情况
-
要与你自己版本匹配,比如SpringBoot 3.0以上需要java17
-
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>3.0.2</version> </dependency>
-
编写mapper
-
在resources目录下建mybatis.mapper包
-
这样,在这里面弄对应的mapper
-
配置
-
在application.yaml文件中到入Mybatis的对应的配置
-
#整合Mybatis mybatis: type-aliases-package: com.packet.pojo mapper-locations: classpath:mybatis/mapper/*.xml
aliases-package: com.packet.pojo
mapper-locations: classpath:mybatis/mapper/*.xml
就算完成了!!!!!!!!!!