Spring配置内置的连接池DriverManagerDataSource

applicationContext.xml配置


<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">
        <bean id="driverManagerDateSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql:///springtest" />
        <property name="username" value="root" />
        <property name="password" value="abc">property>
    bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="driverManagerDateSource">property>
    bean>
beans>

测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class JdbcTemplateTest2 {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void test1() {

        // 使用JdbcTemplate来完成操作
        jdbcTemplate.execute("update t_user set sex='女'");// execute方法可以执行任意sql

    }
}

你可能感兴趣的:(spring)