KKB : spring_JDBC操作数据库

1、导入pom依赖

<dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
    </dependencies>

2、配置spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd">

3、使用properties文件 配置数据库的基本信息

jdbc.properties

特别注意:这里的用户名禁止使用username !!!因为程序内部已经使用,username变成了关键字!

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/express?useUnicode=true&charsetEncoding=utf-8
username1=root
password=123456

4、spring配置文件中配置 数据库properties配置文件

使用的是context标签,property-placeholder属性

5、编写基本的bean包,以及dao包

项目结构

接口需要实现的功能,插入数据,查询数据

实现接口的class类,需要继承 JdbcDaoSuport

插入数据的方法编写

根据Id查找信息

查询所有的数据

完善 spring.xml

上图的依据:

测试

猜你喜欢

转载自blog.csdn.net/awodwde/article/details/112851830