zoukankan      html  css  js  c++  java
  • springboot JDBC整合

    1、建立一个springboot项目,并导包:JDBC API 与 MySQL Driver

     2、项目建好之后,发现自动帮我们导入了如下的启动器:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>

    3、编写yaml配置文件连接数据库;也可以使用properties配置文件

    spring:
      datasource:
        username: mysql账号
        password: 密码
        #配置时区  serverTimezone=UTC&
        url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
        driver-class-name: com.mysql.jdbc.Driver

    4、测试,查看是否成功

    控制台打印出:class com.alibaba.druid.pool.DruidDataSource     成功

    @SpringBootTest
    class SpringbootDataJdbcApplicationTests {
    
        //注入数据源
        @Autowired
        DataSource dataSource;
    
        @Test
        public void contextLoads() throws SQLException {
            //默认数据源
            System.out.println(dataSource.getClass());
            //获得连接
            Connection connection =   dataSource.getConnection();
            System.out.println(connection);
            //关闭连接
            connection.close();
        }
    }

    扩展

    JDBCTemplate

    JdbcTemplate主要提供以下几类方法:

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

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

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

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

    package com.company.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.ui.Model;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    import java.util.Map;
    
    @RestController
    public class TestController {
    
        @Autowired
        JdbcTemplate jdbcTemplate;
    
    //    查询全部信息
    //    没有实体类,使用万能的  Map
        @GetMapping("/user")
        public List<Map<String,Object>> userAll(){
    
            String sql="select * from user";
            List<Map<String, Object>> maps = jdbcTemplate.queryForList(sql);
            return maps;
        }
    
        @RequestMapping("/update/{id}")
        public String update(@PathVariable("id") int id){
            //两种写法
            //String sql="update user set name=?,pwd=? where id="+id;  则不需要    o[2]=id;
    
            String sql="update user set name=?,pwd=? where id=?";
            Object[] o=new Object[3];
            o[0]="kkkkl";
            o[1]="12341131";
            o[2]=id;
            jdbcTemplate.update(sql,o);
            return "update-Ok";
    
        }
    
        @RequestMapping("/insert")
        public String insert(){
            String sql="insert into user(id,name,pwd) value(6,'ppppp','111111')";
            jdbcTemplate.update(sql);
            return "insert-OK";
        }
    
        @RequestMapping("/delete/{id}")
        public String delete(@PathVariable("id") int id){
            String sql="delete from user where id="+id;
            int update = jdbcTemplate.update(sql);
            return "delete-Ok";
        }
    
        //测试项目是否搭建成功
        @RequestMapping("/u")
        public String all(Model model){
            return "jjjjj";
        }
    
    }
  • 相关阅读:
    Java中的逆变与协变
    JAVA中使用DOM解析XML文件
    ReentrantLock的使用
    tomcat源码 Container
    tomcat源码 Connector
    tomcat源码 StandardService
    BlockingQueue队列
    tomcat源码 StandardServer
    tomcat源码 分析 Catalina
    tomcat整体架构
  • 原文地址:https://www.cnblogs.com/wdsjg/p/13599360.html
Copyright © 2011-2022 走看看