zoukankan      html  css  js  c++  java
  • SpringBoot连接数据库

    1连接mysql数据库

    1. appliction配置文件

    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/student?useUnicode=true&characterEncoding=utf-8
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource
    

    2.pom.xml添加依赖

    添加数据库连接
    <dependency>
       <groupId>mysql</groupId>
       <artifactId>mysql-connector-java</artifactId>
       <scope>runtime</scope>
    </dependency>
    JdbcTemplate支持
    dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.10.RELEASE</version>
    </dependency>

    3.利用JdbcTemplate执行语句

    @Autowired
        private JdbcTemplate jdbcTemplate;
    
    @RequestMapping("/list")
    public List<Map<String,Object>> getUser(){
        System.out.print("111111111111");
        String sql="select * from students";
        List<Map<String,Object>> list=jdbcTemplate.queryForList(sql);
        return list;
    }
    

      

      如图所示,已经成功连接查询

    PS:  1.添加JdbcTemplate 支持

         2.使用JdbcTemplate需要在配置文件声明

        spring.datasource.type=com.mysql.jdbc.jdbc2.optional.MysqlDataSource(个人使用过程中遇到问题添加后解决,type根据实际情况而定)

    
    
    
    往事如烟,余生有我.
  • 相关阅读:
    c++ exports def文件
    对比WDCP面板与AMH面板的区别与选择
    阿里云服务器配置 SVN 服务器与生产站点同步
    linux-Centos7安装python3并与python2共存
    oracle数据库定时任务dbms_job的用法详解
    AnyRobot
    spring mvc activemq
    kafka 查看队列信息
    json多态序列化
    CentOS7.x使用overlay文件系统
  • 原文地址:https://www.cnblogs.com/assistants/p/9583946.html
Copyright © 2011-2022 走看看