• Springboot整合jdbc和Mybatis


    目录

    整合jdbc

    1. 新建项目

    2. 编写yaml配置文件连接数据库

    3. 测试类

    使用原生的jdbcTemplate进行访问测试

    使用Druid连接池

    1. 添加类型

    2. 初始化连接池

    3. 编写config类

    配置Druid数据源监视

    整合Mybatis

    1. 导入依赖

    2. 编写mapper接口

    3. 编写实体类

    4. 编写mapper.xml

    5. controller层调用方法


    整合jdbc

    1. 新建项目

    2. 编写yaml配置文件连接数据库

    1. spring:
    2. datasource:
    3. username: root
    4. password: root
    5. url: jdbc:mysql://localhost:3306/springboot?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
    6. driver-class-name: com.mysql.cj.jdbc.Driver

    3. 测试类

    1. @SpringBootTest
    2. class Springboot04JdbcApplicationTests {
    3. @Autowired
    4. DataSource dataSource;//注入依赖
    5. @Test
    6. void contextLoads() throws SQLException {
    7. System.out.println(dataSource.getClass());//测试一下类型
    8. Connection connection = dataSource.getConnection();
    9. System.out.println(connection);
    10. }
    11. }

    可以得到数据源为:class com.zaxxer.hikari.HikariDataSource

    使用原生的jdbcTemplate进行访问测试

    • execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

    • update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

    • query方法及queryForXXX方法:用于执行查询相关语句;

    • call方法:用于执行存储过程、函数相关语句。

    1. @RestController
    2. public class JDBCController {
    3. @Autowired
    4. JdbcTemplate jdbcTemplate;
    5. @RequestMapping(value = "/list")
    6. public List> userList(){
    7. String sql = "select * from user";
    8. List> list = jdbcTemplate.queryForList(sql);
    9. return list;
    10. }
    11. }

    使用Druid连接池

    1. 添加类型

    在yml中设置type

    type: com.alibaba.druid.pool.DruidDataSource # 自定义数据源

    2. 初始化连接池

    1. initialSize: 5
    2. minIdle: 5
    3. maxActive: 20
    4. maxWait: 60000
    5. timeBetweenEvictionRunsMillis: 60000
    6. minEvictableIdleTimeMillis: 300000
    7. validationQuery: SELECT 1 FROM DUAL
    8. testWhileIdle: true
    9. testOnBorrow: false
    10. testOnReturn: false
    11. poolPreparedStatements: true

    3. 编写config类

    1. @Configuration
    2. public class DruidConfig {
    3. @ConfigurationProperties(prefix = "spring.datasource")
    4. @Bean
    5. public DataSource druidDataSource(){
    6. return new DruidDataSource();
    7. }
    8. }

    @ConfigurationProperties(prefix = "spring.datasource"):将全局配置文件中前缀为 spring.datasource的属性值注入到 com.alibaba.druid.pool.DruidDataSource 的同名参数中

    配置Druid数据源监视

    1. @Bean
    2. public ServletRegistrationBean servletRegistrationBean(){
    3. ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
    4. HashMap init = new HashMap<>();
    5. init.put("loginUsername","admin");//初始化
    6. init.put("loginPassword","123");
    7. //后台允许谁访问
    8. init.put("allow","");
    9. bean.setInitParameters(init);
    10. return bean;
    11. }

    当执行一次sql时,后台会有监视 

    整合Mybatis

    1. 导入依赖

    1. <dependency>
    2. <groupId>org.mybatis.spring.bootgroupId>
    3. <artifactId>mybatis-spring-boot-starterartifactId>
    4. <version>2.1.1version>
    5. dependency>

    2. 编写mapper接口

    1. @Mapper
    2. @Component
    3. public interface UserMapper {
    4. List query();
    5. }

     @Mapper : 表示本类是一个 MyBatis 的 Mapper

    3. 编写实体类

    使用Lombok注解

    1. @Data
    2. @AllArgsConstructor
    3. @NoArgsConstructor
    4. public class User {
    5. int id;
    6. String name;
    7. String password;
    8. }

    4. 编写mapper.xml

    1. "1.0" encoding="UTF-8" ?>
    2. mapper
    3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    5. <mapper namespace="com.demos.mapper.UserMapper">
    6. <select id="query" resultType="User">
    7. select * from user
    8. select>
    9. mapper>

    5. controller层调用方法

    1. @RestController
    2. public class UserController {
    3. @Autowired
    4. UserMapper userMapper;
    5. @RequestMapping(value = "/query")
    6. public List query(){
    7. List query = userMapper.query();
    8. return query;
    9. }
    10. }
  • 相关阅读:
    java包以及权限修饰符
    俄语第二格
    【原创】指针变量作为函数参数要点注意+main函数中值是否改变
    UGUI交互组件ScrollView
    java注解和通过反射获取注解值
    “移动机器人课程群实践创新的困境与突围”素材
    算法—4、寻找两个正序数组的中位数
    基于JAVA气候分析平台计算机毕业设计源码+数据库+lw文档+系统+部署
    php组件漏洞
    阿里妈妈牟娜:定向广告新一代点击率预估主模型——深度兴趣演化网络
  • 原文地址:https://blog.csdn.net/weixin_64704029/article/details/133106471