首页 > 数据库 >Spring_2023_11_22_3 Spring--连接数据库

Spring_2023_11_22_3 Spring--连接数据库

时间:2023-11-22 20:00:10浏览次数:33  
标签:11 22 Spring springframework import org com public

Spring--连接数据库

Spring提供了JdbcTemplate模板类

依赖的引入:

i. Spring-context

ii. Spring-jdbc

iii. Mysql

iv. dbcp(连接池)

        <!--spring基础依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.20</version>
        </dependency>
        <!--spring连接数据库-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.30</version>
        </dependency>
        <!--mysql-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.29</version>
        </dependency>
        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.30</version>
        </dependency>
        <!--数据库连接池-->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-dbcp2</artifactId>
            <version>2.11.0</version>
        </dependency>

创建实体类对象(表对应的对象)

package com.bboy.pojo;

import lombok.Data;

@Data
/*
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/22 0022 17:39:54
 */
public class Student {
    private int id ;
    private String name ;
    private String birth ;
    private String sex ;
    private String password ;
}

创建dao层的内容

i. StudentDao

package com.bboy.dao;

import com.bboy.pojo.Student;

import java.util.List;

public interface StudentDao {
    public List<Student> listStudents();
}

ii. StudentDaoImpl 需要继承JdbcDaoSupport类

package com.neuedu.dao.impl;

import com.neuedu.dao.StudentDao;
import com.neuedu.pojo.Student;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImpl implements StudentDao {
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {

        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public List<Student> listStudents() {
        List<Student> lists = new ArrayList<>();
        //-使用jdbc模板执行sql语句
        jdbcTemplate.query("select * from t_student", new RowMapper<Student>() {
            @Override
            public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
                Student stu = new Student();
                stu.setId(rs.getInt("id"));
                stu.setName(rs.getString("name"));
                stu.setBirth(rs.getString("birth"));
                stu.setSex(rs.getString("sex"));
                lists.add(stu);
                return stu;
            }
        });
        return lists;
    }
}

核心配置文件中进行配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    <!--配置数据源-->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="url" value="jdbc:mysql://localhost:3306/qinmanage"/>
        <property name="username" value="root"/>
        <property name="password" value="127003"/>
    </bean>
    <!--将数据源配置到JdbcTemplate模板中-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--将JdbcTemplate注入到dao中-->
    <bean id="studentDao" class="com.bboy.dao.impl.StudentDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"/>
    </bean>
</beans>

测试类

package com.bboy.demo;

import com.bboy.dao.StudentDao;
import com.bboy.pojo.Student;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * @类描述:
 * @作者:秦帅
 * @时间:2023/11/22 0022 18:06:46
 */
public class Demo {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        StudentDao studentDao = (StudentDao) context.getBean("studentDao");
        List<Student> stus = studentDao.listStudents();
        System.out.println(stus);
    }
}

运行结果

image

标签:11,22,Spring,springframework,import,org,com,public
From: https://www.cnblogs.com/Qinyyds/p/17850167.html

相关文章

  • 2023-11-22 Invariant Violation: [app.model] namespace should be unique ==》模块
     如上图,报错原因:存在多个名为demoDataSource的模块名称导致报错解决方案:修改模块名称即可,把demoDataSource改为demoDataSource2就不会报错了扩展:该问题是由rudex引起的,redex要求数据模型(models)命名(namespace)必须不同,否则在注入该数据模型时就会报错......
  • springboot如何监控各种指标?
    以springboot2.7.17为例: 1:新增如下依赖:<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency> 然后,在你的配置文件(如 application.properties)中添加以下配......
  • Spring+Vue修改功能
    后端数据回显,根据ID回显@GetMapping("/findById/{id}")publicUsersfindById(@PathVariable("id")Integerid){returnusersRepository.findById(id).get();}修改数据,根据回显的数据回显@PutMapping("/update")publicStringupd......
  • 2023-11-22诸法无我,无我即是空
    2023-11-22什么是空?无我即是空。什么是我?常,一,主宰,同时符合这三项条件的就是我。不是你我他,代词我。常,就是恒常不变。比如认为身体里有一个我,从小到大,思想行为待人接物都变了,我没变,那个灵魂恒常不变。一,就是只有一个我。我要是有两个我,我就不是我了。一神论,里面只有一个神我。主......
  • springboot tomcat连接数监控
     直接问文心一言:  ......
  • 雅礼信奥 2023.11.22 习题课记录(讲解版)
    雅礼信奥\(2023.11.22\)习题课记录(讲解版)都是CF题,不如AT。剧情版后面会更。A-YarikandArray(CF1899C)dp题,作为学OI\(3\)年的萌新OIer,后面才想到dp真是太蒟蒻了,时间复杂度\(O(tn)\)。初始\(f_1=1\),其他为\(0\)。状态转移方程:\(\begin{cases}\text{if}&......
  • mujoco安装报错:mujoco_py/gl/eglplatform.h:99:10: fatal error: X11/Xlib.h: 没有那
     安装mujoco报错:mujoco_py/gl/eglplatform.h:99:10:fatalerror:X11/Xlib.h:没有那个文件或目录 修复方法:sudoaptinstalllibx11-dev   ......
  • Spring Cloud
    组件版本关系SpringCloudAlibabaVersionSentinelVersionNacosVersionRocketMQVersionDubboVersionSeataVersion2.2.1.RELEASEor2.1.2.RELEASEor2.0.2.RELEASE1.7.11.2.14.4.02.7.61.2.02.2.0.RELEASE1.7.11.1.44.4.02.7.4.11.0.02.1.1.RELEASEor2.0.1.RELEASEor1.5......
  • 基于增强型ARM Cortex M0+内核平台的MSPM0G1106TRHBR、MSPM0G1507SRHBR混合信号微控制
    一、MSPM0G1106TRHBR 基于增强型Arm®Cortex®-M0+32位内核,具有64KB闪存、80MHz频率MSPM0G110x微控制器(MCU)属于MSP高度集成的超低功耗32位MCU系列,该MCU系列基于增强型Arm®Cortex®-M0+32位内核平台,工作频率最高可达80MHz。这些成本优化型MCU提供高性能模......
  • Spring Cloud +UniApp +MySql框架开发的智慧工地云平台源码
    智慧工地是指通过信息化技术、物联网、人工智能技术等手段,对建筑工地进行数字化、智能化、网络化升级,实现对施工全过程的实时监控、数据分析、智能管理和优化调控。智慧工地的建设可以提高工地的安全性、效率性和质量,降低施工成本,是建筑行业数字化转型升级的重要抓手。主要围绕“人......