2012年11月1日

Spring JDBC支持

在applicationContext中配置数据连接


 <!-- 配置数据库连接信息 -->
 <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
  <property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver" />
  <property name="url" value="jdbc:sqlserver://localhost:1433;databaseName=testdb" />
  <property name="username" value="sa" />
  <property name="password" value="123456" />
 </bean>

然后给目标对象注入dataSource参数     目标对象类型继承自(extends) jdbcTemplate

查询方法示例:
 public List<dept> queryAll(){
    
    return super.query("sql语句", new RowMapper(){
            //实现该类(RowMapper)中方法 
            return 返回实体   
            public Dept mapRow(ResultSet rs, int index) throws SQLException {
    
            Dept d = new Dept();
    
            d.setDid(rs.getInt("did"));
            d.setDname(rs.getString("dname"));
    
            return d;
        
    });

}

posted @ 2012-11-01 22:09 劳者多能 阅读(79) | 评论 (0)编辑 收藏

2012年10月31日


Hibernate使用getCurrentSession用法:需要在hibernate中配置:
<property name="hibernate.current_session_context_class">thread</property>

posted @ 2012-10-31 16:57 劳者多能 阅读(114) | 评论 (0)编辑 收藏

仅列出标题