1. Sql
2. 数据源的自动配置
-
启动器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jdbc</artifactId> </dependency> // mysql <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.20</version> </dependency>
- 版本控制
-
数据源的自动配置类为:DataSourceAutoConfiguration 其中自动注入了 DataSourceProperties类作为配置 此类前缀为 (prefix = “spring.datasource”) 所以对于数据源的配置应该使用spring.datasource.xxx
# 数据源配置 spring: datasource: url: jdbc:mysql://localhost:3306 username: root password: Wjy12300. driver-class-name: com.mysql.jdbc.Driver
-
jdbc的测试
@SpringBootTest class Springboot04SystemApplicationTests { @Autowired JdbcTemplate jdbcTemplate; @Test void contextLoads() { Integer integer = jdbcTemplate.queryForObject("select count(*) from user", Integer.class); System.out.println(integer); } }
3.使用德鲁伊数据库
4.整合Mybatis操作
- 启动器
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
-
分析启动器
maven:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-vFqTOmMn-1627462652270)(…/…/截图/iShot2021-07-27 19.49.09.png)]
自动配置类:[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Du9q4fQl-1627462652273)(…/…/截图/iShot2021-07-27 19.51.37.png)]
-
根据springboot官方文档定义
-
全局配置文件 (都被sprinboot自动配置完了)
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="org/mybatis/example/BlogMapper.xml"/> </mappers> </configuration>
-
mapper.xml (UserMapper.xml)
<?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.rain.springboot04system.mapper.UserMapper"> <select id="findAll" resultType="com.rain.springboot04system.pojo.User"> select * from user </select> </mapper>
-
配置springBoot的yaml配置文件指定springboot中mybatis配置
在MybatisAutoConfiguration自动配置类中导入的配置类MybatisProperties中定义了priex为“mybatis”
的若干配置 数据源配置
# mybatis 配置 # 1. 全局配置文件位置 # 2. mapper文件位置 mybatis: config-location: classpath:mybatis/mybatis-config.xml mapper-locations: classpath:mybatis/mapper/*.xml
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Ntm8blrq-1627462652275)(…/…/截图/image-20210728141845460.png)]
-
建立Service @Service 自动注入 Mapper
-
调用service完成查询
-
-
注解配置
-
导入mybatis-start启动器
-
在yaml中设置数据源
(注册mapper service)
-
定义mapper
- mapper中定义方法
- @注解执行操作
-
定义service
- 自动导入mapper
- 定义方法 public
-
执行
代码如下:
pojo -> Mybatis
public class Mybatis implements Serializable { private int id; private String url; private String username; private String password; private String driver; }
mapper -> MybatisMapper
@Mapper @Component public interface MybatisMapper { @Select("select * from mybatis where id = #{id}") Mybatis findNyid(@Param("id") int id); }
service -> MybatisService
@Service public class MybatisService { @Autowired MybatisMapper mybatisMapper; public Mybatis findNyid(@Param("id") int id){ return mybatisMapper.findNyid(id); } }
controller
@Controller public class MybatisController { @Autowired MybatisService mybatisService; @ResponseBody @GetMapping("/mybatisTest") public String mybatisTest(@Param("id") int id){ return mybatisService.findNyid(id)+""; } }
执行结果:
-