spring-jdbc

一、从数据库中获取一条记录,实际得到对应的一个对象

调用queryForObject(String sql ,RowMapper<T> rowmapper,Object args)方法,而不是调用queryForObject(String sql ,Class<T>  requiredType,Object args)

其中RowMapper指定如何去映射结果集的行,常用的实现类为BeanPropertyRowMapper

二、查询实体类的集合

调用query()方法,而不是queryForList()

三、获取单个列的值或统计查询

使用queryForObject(String sql ,Class<T>  requiredType)

 

例:

.xml(配置文件)

  <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>
    <!-- 配置C3P0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="initialPoolSize" value="${jdbc.initalPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>
    
    <!--配置NamedParameterJdbcTemplate,该对象可以使用具名参数,其没有无参数的构造器,必须为其构造器指定参数  -->
    <bean id="namedParameterJdbcTemplate" 
        class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
        <constructor-arg ref="dataSource"></constructor-arg>
    </bean>

.properties

jdbc.user=root
jdbc.password=123123
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql:///mysql-spring4
jdbc.initalPoolSize=5
jdbc.maxPoolSize=10

.java

public class JDBCTest {

    private ApplicationContext ctx = null;
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    
    {
        ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        namedParameterJdbcTemplate = (NamedParameterJdbcTemplate)ctx.getBean(NamedParameterJdbcTemplate.class);
    }
    
     /**
     * 使用具名参数时,可以使用update(String sql, SqlParameterSource paramSource) 方法进行更新操作
     * 1、SQL语句中的参数名和类的属性一致
     * 2、使用SqlParameterSource的BeanPropertySqlParameterSource实现类作为参数
     */
    @Test
    public void testNamedParameterJdbcTemplate() {
        try {
            String sql ="insert into employees(ID,last_name,email,dept_id) "
                    + "values(:ID,:lastName,:email,:deptId)";
            Employee employee = new Employee();
            employee.setID("3334");
            employee.setLastNme("Lee");
            employee.setEmail("123456@qq.com");
            employee.setDeptId("111");
            
            SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(employee);
            namedParameterJdbcTemplate.update(sql, parameterSource);
        } catch (DataAccessException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }
}    

类文件:

public class Employee {
    private String ID;
    private String lastName;
    private String email;
    private String deptId;
    
    public String getID() {
        return ID;
    }
    public void setID(String iD) {
        ID = iD;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastNme(String lastName) {
        this.lastName = lastName;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getDeptId() {
        return deptId;
    }
    public void setDeptId(String deptId) {
        this.deptId = deptId;
    }
}

二:

private static ApplicationContext ctx = null;
public static NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public User findUserByLoginName(String loginName,String passWord) 
{
if (ctx == null) { ctx = new ClassPathXmlApplicationContext("spring-mybatis.xml"); } namedParameterJdbcTemplate = (NamedParameterJdbcTemplate)ctx.getBean(NamedParameterJdbcTemplate.class); String sql = "select * from user where loginName = :loginName and passWord = :passWord"; User user = new User(); user.setLoginName(loginName); user.setPassWord(passWord); SqlParameterSource parameterSource = new BeanPropertySqlParameterSource(user); BeanPropertyRowMapper<User> rowMapper = new BeanPropertyRowMapper<User>(User.class); NamedParameterJdbcTemplate namedParameterJdbcTemplate = new JDBCMySql().namedParameterJdbcTemplate; try { user = namedParameterJdbcTemplate.queryForObject(sql,parameterSource,rowMapper); } catch (DataAccessException e) { e.printStackTrace(); } return user; }

 

posted on 2018-04-12 23:59  LJD泊水  阅读(201)  评论(0编辑  收藏  举报