[Spring JDBC] Database Development: Basic Concepts



Spring database development: basic concepts

① Spring JdbcTemplate类

1. JdbcTemplate is a tool class provided by Spring to manipulate the database after encapsulating the original JDBC. We can use JdbcTemplate to complete all database operations, such as addition, deletion, modification, and so on.

2. The transformed JdbcTemplate mainly provides the following three types of methods:

  • executeXxx() : Execute any SQL statement, create, modify, and delete databases and tables
  • updateXxx() : Execute statements such as adding, modifying, deleting, etc.
  • queryXxx() : Execute query related statements

Back to top


② Spring JDBC configuration (package)

Spring JDBC abstraction framework consists of four different packages: core, dataSource, objectand support.

  • org.springframework.jdbc.core包It consists of the JdbcTemplate class and related callback interfaces and classes.

  • org.springframework.jdbc.datasource包It is composed of some tool classes used to simplify DataSource access, as well as simple implementations of various DataSource interfaces (mainly used for unit testing and using JDBC outside of the J2EE container). The tool class provides some static methods, such as obtaining data connections through JNDI and closing these connections if necessary. It supports the connection of bound threads, such as the connection used for DataSourceTransactionManager.

  • org.springframework.jdbc.object包It consists of classes that encapsulate queries, updates, and stored procedures. The objects of these classes are thread-safe and reusable. They are similar to JDO, and the difference from JDO is that the query results are "disconnected" from the database. They are a higher-level abstraction of JDBC based on the org.springframework.jdbc.core package.

  • org.springframework.jdbc.support包Some SQLException conversion classes and related tool classes are provided.

Exceptions thrown during JDBC processing will be converted into org.springframework.dao包exceptions defined in. Therefore, using Spring JDBC for development will not need to deal with the exceptions that JDBC or specific RDBMS can throw. All exceptions are unchecked exceptionso that we can selectively catch exceptions passed to the caller.

If you want to use JDBC, you have to configure it. The following is the basic template of the xml configuration file:

<?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:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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
                           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!-- 开启组件扫描 -->
    <context:component-scan base-package="JDBC"></context:component-scan>

    <!-- 1.配置数据库连接池   -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
        <!-- 1.1 连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost:3306/spring?useUnicode=true&amp;characterEncoding=utf8"></property>
        <!-- 1.2 连接数据库的用户名 -->
        <property name="username" value="root"></property>
        <!-- 1.3 连接数据库的密码 -->
        <property name="password" value="root"></property>
        <!-- 1.4 数据库驱动 -->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    </bean>
    
    <!-- 2.创建JdbcTemplate对象 --- jdb模板 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 2.1 注入dataSource 数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <!-- 3. 配置注入其他类 -->
    <bean id="xxx" class="xxx">
        <property name="jabcTemplate" ref="jdbcTemplate"></property>  
    </bean>
    ..........
</beans>

In the above code, three Beans are defined, namely dataSource, jdbcTemplateand the Bean that needs to be injected into the class. The dataSourcecorresponding org.springframework.jdbc.datasource.DriverManagerDataSourceclass is used to configure the data source, and the jdbcTemplatecorresponding configuration org.springframework.jdbc.core.JdbcTemplatof JdbcTemplate is defined in the corresponding class. The configuration of dataSource in the above code is the 4 properties required for JDBC to connect to the database.

For these 4 attributes, you need to set the corresponding attribute values ​​according to the database type or machine configuration. For example, ifThe database type is different, you need to change the driver name;in caseThe database is not local, you need to replace the localhost in the address with the corresponding host IP;in caseModified the port number of the MySQL database (the default is 3306), you need to add the modified port number, if not modified, the port number can be omitted;at the same timeThe user name and password for connecting to the database need to be consistent with the user name and password set when the database was createdIn this example, the user name and password of the Spring database are both root.

When defining the JdbcTemplate, you need to inject the dataSource into the JdbcTemplate, and other beans that need to use the JdbcTemplate also need to inject the JdbcTemplate into the Bean (usually injected into the Dao class, and perform database-related operations in the Dao class).

Back to top


Guess you like

Origin blog.csdn.net/qq_45797116/article/details/114241586