zoukankan      html  css  js  c++  java
  • Spring Boot学习进阶笔记(二)-JdbcTemplate访问数据库

    基于上次一版本的代码,本篇只添加需要添加的代码

    1、添加maven依赖

    <!-- 数据库依赖 -->

    <dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-jdbc</artifactId>

    </dependency>

    <dependency>

    <groupId>mysql</groupId>

    <artifactId>mysql-connector-java</artifactId>

    </dependency>

     

    2、创建UserService接口和实现类

    public interface UserService {

     

        Integer getUserCount();

     

    }

     

    package com.zh.SpringBootDemo.service.imp;

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.stereotype.Service;

    import com.zh.SpringBootDemo.service.UserService;

    @Service
    public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public Integer getUserCount() {
    return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    }

    3、在配置文件“application.properties”添加数据的链接信息

    spring.datasource.url=jdbc:mysql://192.168.2.111:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false

    spring.datasource.username=root

    spring.datasource.password=123456

    spring.datasource.driver-class-name=com.mysql.jdbc.Driver

     

    完成!

  • 相关阅读:
    C#的显式接口和隐式接口
    Working with XML in a Classic COM Application
    规格单位换算
    C#压缩解压缩(文件夹里包含文件夹)
    在线编辑器原理
    右键新建文本文档没有了。
    MemoryStream读写
    gacutil.exe ,RegAsm.exe 和全局缓存(GAC)
    OData services入门使用ASP.NET Web API描述
    Readonly和Disabled
  • 原文地址:https://www.cnblogs.com/lovechengyu/p/7099321.html
Copyright © 2011-2022 走看看