Spring{Jdbc-Template}基本使用

 概述:

 开发步骤:

基础方式:

第一步:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
    <version>0.9.2.1</version>

<!--        spring集成的jdbc包start-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>5.0.7.RELEASE</version>
        </dependency>
<!--        spring集成的jdbc包end-->

第二部:

public class JdbcTemplateTest {
    @Test
    public void test1() throws PropertyVetoException {
        //创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("123456");
        //创建数据库表实体
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //设置数据源
        jdbcTemplate.setDataSource(dataSource);
        //执行操作
        jdbcTemplate.update("insert into tb_user values(?,?)","马云",100);
    }
}

 Spring产生JdbcTemplate对象方式:

 

 配置:

<!--    配置数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
<!--    jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

 使用:

    @Test
    public void test2() throws PropertyVetoException {
        ApplicationContext app = new  ClassPathXmlApplicationContext("applicationContext.xml");
        JdbcTemplate jdbcTemplate = app.getBean(JdbcTemplate.class);
        //执行操作
        jdbcTemplate.update("insert into tb_user values(?,?)","马化腾",100);
    }

分离配置信息到.properties文件

jdbc.properties文件内容:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/test
jdbc.username = root
jdbc.password = 123456

 配置:

<?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声明-->
    <context:property-placeholder location="jdbc.properties"/>
<!--    配置数据源对象-->
    <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>
<!--    jdbc模板对象-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
</beans>

 使用和上面一样。

数据库操作的使用:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUTDTest {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //更新
    @Test
    public void test1(){
        int update = jdbcTemplate.update("update tb_user set age=? where  name =?", 12, "马云");
        System.out.println(update);
    }

    //查询多个 自动封装成对象
    @Test
    public void test2(){
        List<User> users = jdbcTemplate.query("select * from tb_user",new BeanPropertyRowMapper<User>(User.class));
        System.out.println(users);
    }
    //查询单个 自动封装成对象
    @Test
    public void test3(){
        User user = jdbcTemplate.queryForObject("select * from tb_user where name=?",new BeanPropertyRowMapper<User>(User.class),"马云");
        System.out.println(user);
    }
    //聚合查询,总人数
    @Test
    public void tes4(){
        long  count = jdbcTemplate.queryForObject("select count(*) from tb_user",long.class);//指定返回的类型
        System.out.println(count);
    }

    //删除
    @Test
    public void test5(){
        int update = jdbcTemplate.update("delete from tb_user  where  name =?",  "马云");
        System.out.println(update);
    }

    //添加
    @Test
    public void test6(){
        int update = jdbcTemplate.update("insert into  tb_user  values(?,?)",  "马云",100);
        System.out.println(update);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值