spring learning log four

A, spring support for the JDBC

About JdbcTemplate

In order to make easier to use JDBC, Spring defines an abstract layer on the JDBC the API, in order to establish a framework for access JDBC. Spring JDBC core framework, designed to provide a template JDBC templates for different types of methods JDBC operations. each template method can control the entire process, and allow certain tasks to cover process. in this way, while retaining flexibility as possible, to minimize the workload of database access.

step:

1. Add the appropriate jar package in the project

2. bean and configuration data sources in the spring profile configuration JDBCTemplate the bean

<?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"
    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">
    
    <!-- 包扫描 -->
    <context:component-scan base-package="com.zhiyou100.xz"></context:component-scan>
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置数据源:数据库交互的。c3p0 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
    </bean>
    <!-- 配置springjdbc的模板类 -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
</beans>

Properties file 3.spring profile introduced

# Data source information 
jdbc.driver = com.mysql.jdbc.Driver 
jdbc.url = JDBC: MySQL: // localhost: 3306 / Spring 
jdbc.username = the root 
jdbc.password = the root

 

Guess you like

Origin www.cnblogs.com/sitian2050/p/11488291.html