Spring JDBC支持
在applicationContext中配置數(shù)據(jù)連接
<!-- 配置數(shù)據(jù)庫連接信息 -->
<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>
然后給目標(biāo)對象注入dataSource參數(shù) 目標(biāo)對象類型繼承自(extends) jdbcTemplate
查詢方法示例:
public List<dept> queryAll(){
return super.query("sql語句", new RowMapper(){
//實現(xiàn)該類(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;
});
}
Hibernate使用getCurrentSession用法:需要在hibernate中配置:
<property name="hibernate.current_session_context_class">thread</property>