Spring入门笔记--spring-JDBC

spring JDBC

最简单的JDBC配置

<?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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        <bean id="databaseTest" class="com.alibaba.druid.pool.DruidDataSource">
                <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/mall?serverTimezone=UTC"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
        </bean>
</beans>

之前通过bean依赖注入--普通数据类型,注入了jdbc连接参数,现在将这些属性放到一个独立的properties文件中:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mall?serverTimezone=UTC
jdbc.username=root
jdbc.password=root

xml修改为:

<?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-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd">
        <!--加载外部properties文件,先引入context命名空间,第4,7,8行所示-->
        <!--加载外部properties文件-->
        <context:property-placeholder location="classpath:jdbc.properties"/>
        // DruidDataSource是阿里写的数据库连接池,用来管理数据库连接
        <bean id="databaseTest" class="com.alibaba.druid.pool.DruidDataSource">
                <property name="driverClassName" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
        </bean>
</beans>

使用时通过获取spring容器上下文获取连接,并执行sql操作:

@Test
    public void test1() throws SQLException {
        ApplicationContext app  = new ClassPathXmlApplicationContext("applicationContext.xml");
        DruidDataSource dataSource = (DruidDataSource) app.getBean("databaseTest");
        Connection connection = dataSource.getConnection();
        String sql = "select * from pms_brand;";
        /*获取PreparedStatement对象*/
        PreparedStatement preparedStatement =  connection.prepareStatement(sql);
        /*执行sql*/
        ResultSet resultSet = preparedStatement.executeQuery();
        while (resultSet.next()){
            System.out.println("id"+resultSet.getInt("id") + ", name" + resultSet.getString("name"));
        }
        connection.close();
    }

Spring MVC JdbcTemplate

    <!--加载外部properties文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <bean id="databaseProp" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driver}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="databaseProp"/>
    </bean>
    @Test
    public void test() {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        jdbcTemplate.execute("insert into account (name, money) values ('Jane', 25)");
    }
posted @ 2022-05-15 19:51  Saski&Naruto  阅读(36)  评论(0)    收藏  举报