[Primavera] Reemplace setXxx con la configuración de la fuente de datos de inyección

Configurar manualmente la fuente de datos

①No utilice la fuente de datos

@Test
public void test0() throws Exception {
    
    
	// 无非四个参数———驱动、数据库url、用户名、用户密码
    Class.forName("com.mysql.cj.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/loliDB", "root", "123456");
    connection.close();
}

importar manualmente la fuente de datos c3p0

@Test
public void test1() throws Exception {
    
    
    // 创建数据源
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    // 配置数据源
    dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
    dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/loliDB");
    dataSource.setUser("root");
    dataSource.setPassword("123456");
    // 获取连接
    Connection connection = dataSource.getConnection();
    // 归还连接
    connection.close();
}

Importe manualmente la fuente de datos c3p0 (cargue el archivo de configuración de propiedades)

(resource下的jdbc.properties资源文件)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/loliDB
jdbc.username=root
jdbc.password=123456
 @Test
public void test2() throws Exception {
    
    
    // 读取配置文件
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");   // 传入"基名"
    String driver = rb.getString("jdbc.driver");
    String url = rb.getString("jdbc.url");
    String username = rb.getString("jdbc.username");
    String password = rb.getString("jdbc.password");
    // 创建数据源
    ComboPooledDataSource dataSource = new ComboPooledDataSource();
    // 配置数据源
    dataSource.setDriverClass(driver);
    dataSource.setJdbcUrl(url);
    dataSource.setUser(username);
    dataSource.setPassword(password);
    // 获取连接
    Connection connection = dataSource.getConnection();
    // 归还连接
    connection.close();
}

 
 
 

Fuente de datos de configuración de Spring

Inyectar parámetros directamente

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/loliDB"></property>
    <property name="user" value="root"></property>
    <property name="password" value="20006212000d"></property>
</bean>

Pruébalo

@Test
// Spring配置数据源
public void test3() throws Exception {
    
    
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    ComboPooledDataSource dataSource  = (ComboPooledDataSource) app.getBean("dataSource");
    Connection connection = dataSource.getConnection();
    connection.close();
}

 
En el escenario de desarrollo real, también necesitamos usar el archivo de recursos de propiedades para un mayor desacoplamiento , es decir, para introducir el archivo de recursos jdbc.properties en el archivo de configuración applicationContext.xml.

Los pasos de implementación son

  1. Introducir el espacio de nombres de contexto en applicationContext xmlns:context="http://www.springframework.org/schema/context"
  2. Introducir la ruta de restricción de contexto en applicationContext http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
  3. Cargar archivo de recursos <context:property-placeholder location="classpath:xxx.properties" />
  4. Utilice la sintaxis de SPEL para inyección "${xxx}"
<?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="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"></property>
    	<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/loliDB"></property>
    	<property name="user" value="root"></property>
    	<property name="password" value="123456"></property>
    </bean>

</beans>

cambie a:

<?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">

	<!--  加载外部的properties资源文件  -->
    <context:property-placeholder location="classpath:jdbc.properties" />

	<!-- 写死的地方修改为SPEL语法 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

</beans>

 
 
 

suplemento

   No es difícil encontrar que en la configuración clásica de fuente de datos manual, setXxxse utilizan muchos formatos de API, ¿no es esto exactamente para lo que está preparada la inyección de dependencia de conjuntos de Spring?

   Además detalle es, el conjunto de conexiones c3p0 ( ComboPooledDataSource) y piscina conexión Druid ( DruidDataSourcemétodo SET), o un parámetro de configuración nombre de la fuente de datos, la diferencia es leve: setDriverClass/setDriverClassName, setJdbcUrl/setUrl, setUser/setUsername, setPassword/setPassword.

 
 
 
 

 
 
 
 

 
 
 
 

Más> _ <

Supongo que te gusta

Origin blog.csdn.net/m0_46202073/article/details/113811789
Recomendado
Clasificación