使用Spring JDBCTemplate简化JDBC的操作(二)

继上一篇文章,主要是在xml中配置需要继承JdbcDaoSupport类的一系列dao层的配置,下面记录下通过注解操作数据库的方法:
方法一:
TestConnMysqlDataSource.java
在class前面的@Component一定不能少;
在xml中只需要配置数据源属性就行,然后再在java中创建JdbcTemplate,继而操作数据库

<!-- 加载内网系统配置 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
    </bean>

    <bean id="configProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:sys.properties</value>
            </list>
        </property>
    </bean>

    <!-- mybatis 配置及jdbc 配置 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
package com.mdl.excel.mysql.test;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.stereotype.Component;

import com.mdl.excel.mysql.bean.User;

@Component
public class TestConnMysqlDataSource{

    @Autowired
    private DriverManagerDataSource dataSource;

    public static void main(String[] args) {
//      //或者ClassPathXmlApplicationContext
//      ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:excel-servlet.xml");

        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:jdbc-servlet.xml");

        TestConnMysqlDataSource icm = ac.getBean(TestConnMysqlDataSource.class);

        User user = icm.Test();

        System.out.println(user.getUsername() + "---" + user.getEmail());
    }


    private User Test(){

        String sql="select * from user where id=?";

        JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);

        return jdbcTemplate.queryForObject(sql, new UserRowMapper(), 1);

    }

    class UserRowMapper implements RowMapper<User> {

        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            return user;
        }

    }

}

方法二:
TestConnMysqlJdbcTemplate.java
在xml中直接配置好jdbcTemplate,然后再java中通过spring注解直接注入调用,与上面方法相比,在此xml中需要多添加一个jdbcTemplate的配置

<!-- 加载内网系统配置 -->
    <bean id="propertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="properties" ref="configProperties" />
    </bean>

    <bean id="configProperties"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="locations">
            <list>
                <value>classpath:sys.properties</value>
            </list>
        </property>
    </bean>

    <!-- mybatis 配置及jdbc 配置 -->
    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${driverClassName}" />
        <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="dataSource"></property>
    </bean>
package com.mdl.excel.mysql.test;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import com.mdl.excel.mysql.bean.User;

@Component
public class TestConnMysqlJdbcTemplate {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public static void main(String[] args) {
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath*:jdbc-servlet.xml");

        TestConnMysqlJdbcTemplate icm = ac.getBean(TestConnMysqlJdbcTemplate.class);

        User user = icm.Test();

        System.out.println(user.getUsername() + "---" + user.getEmail());

    }

    private User Test() {

        String sql = "select * from user where id=?";

        return jdbcTemplate.queryForObject(sql, new UserRowMapper(), 1);

    }

    class UserRowMapper implements RowMapper<User> {

        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setUsername(rs.getString("username"));
            user.setEmail(rs.getString("email"));
            return user;
        }

    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值