1.配置数据源
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
2.使用JDBC模板JdbcTemplate类
<!-- 使用JDBC模板(JdbcTemplate) -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!-- DAO配置 -->
<bean id="rantDao" class="com.springinaction.chapter05.dao.impl.JdbcRantDao">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
public class JdbcRantDao implements RantDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private static final String MOTORIST_INSTERT = "insert into motorist(id,email,password,firstName,lastName) value (null,?,?,?,?)";
private static final String MOTORIST_SELECT = "select id,email,password,firstName,lastName from motorist";
private static final String MOTORIST_BY_ID_SELECT = MOTORIST_SELECT
+ " where id=?";
public void saveMotories(Motorist motorist) {// 插入motorist数据
jdbcTemplate.update(MOTORIST_INSTERT, motorist.getEmail(),
motorist.getPassword(), motorist.getFirstName(),
motorist.getLastName());
}
public Motorist getMotoristById(int id) {//根据ID获取motorist数据
List<Motorist> motorists = jdbcTemplate.query(MOTORIST_BY_ID_SELECT,
new Object[] { id }, new RowMapper<Motorist>() {
public Motorist mapRow(ResultSet rs, int rowNum)
throws SQLException {
Motorist motorist = new Motorist();
motorist.setId(rs.getInt("id"));
motorist.setEmail(rs.getString("email"));
motorist.setPassword(rs.getString("password"));
motorist.setFirstName(rs.getString("firstName"));
motorist.setLastName(rs.getString("lastName"));
return motorist;
}
});
return motorists.size() > 0 ? motorists.get(0) : null;
}
}
3.使用JDBC模版的NamedParameterJdbcTemplate
特别注意的是NamedParameterJdbcTemplate要使用构造器注入
<!-- 使用JDBC模板(NamedParameterJdbcTemplate) -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="dataSource"/>
</bean>
public class NamedParameterRantDao implements RantDao {
private NamedParameterJdbcTemplate jdbcTemplate;
public void setJdbcTemplate(NamedParameterJdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
private static final String MOTORIST_INSTERT = "insert into motorist(id,email,password,firstName,lastName) value (null,:email,:password,:firstName,:lastName)";
private static final String MOTORIST_SELECT = "select id,email,password,firstName,lastName from motorist";
private static final String MOTORIST_BY_ID_SELECT = MOTORIST_SELECT
+ " where id=:id";
public void saveMotories(Motorist motorist) {// 插入motorist数据
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("email", motorist.getEmail());
parameters.put("password", motorist.getPassword());
parameters.put("firstName", motorist.getFirstName());
parameters.put("lastName", motorist.getLastName());
jdbcTemplate.update(MOTORIST_INSTERT, parameters);
}
public Motorist getMotoristById(int id) {//根据ID获取motorist数据
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("id", id);
List<Motorist> motorists = jdbcTemplate.query(MOTORIST_BY_ID_SELECT,
parameters, new RowMapper<Motorist>() {
public Motorist mapRow(ResultSet rs, int rowNum)
throws SQLException {
Motorist motorist = new Motorist();
motorist.setId(rs.getInt("id"));
motorist.setEmail(rs.getString("email"));
motorist.setPassword(rs.getString("password"));
motorist.setFirstName(rs.getString("firstName"));
motorist.setLastName(rs.getString("lastName"));
return motorist;
}
});
return motorists.size() > 0 ? motorists.get(0) : null;
}
}
4.使用Spring对JDBC的DAO支持类(以JdbcDaoSupport为例)
public class SupportJdbcRantDao extends JdbcDaoSupport implements RantDao {
private static final String MOTORIST_INSTERT = "insert into motorist(id,email,password,firstName,lastName) value (null,?,?,?,?)";
private static final String MOTORIST_SELECT = "select id,email,password,firstName,lastName from motorist";
private static final String MOTORIST_BY_ID_SELECT = MOTORIST_SELECT
+ " where id=?";
public void saveMotories(Motorist motorist) {// 插入motorist数据
getJdbcTemplate().update(MOTORIST_INSTERT, motorist.getEmail(),
motorist.getPassword(), motorist.getFirstName(),
motorist.getLastName());
}
public Motorist getMotoristById(int id) {//根据ID获取motorist数据
List<Motorist> motorists = getJdbcTemplate().query(MOTORIST_BY_ID_SELECT,
new Object[] { id }, new RowMapper<Motorist>() {
public Motorist mapRow(ResultSet rs, int rowNum)
throws SQLException {
Motorist motorist = new Motorist();
motorist.setId(rs.getInt("id"));
motorist.setEmail(rs.getString("email"));
motorist.setPassword(rs.getString("password"));
motorist.setFirstName(rs.getString("firstName"));
motorist.setLastName(rs.getString("lastName"));
return motorist;
}
});
return motorists.size() > 0 ? motorists.get(0) : null;
}
}
XML配置中,可以在DAO配置当中直接注入数据源:
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
<property name="initialSize" value="5"/>
<property name="maxActive" value="10"/>
</bean>
<!-- DAO配置 -->
<bean id="rantDao" class="com.springinaction.chapter05.dao.impl.SupportJdbcRantDao">
<property name="dataSource" ref="dataSource"/>
</bean>
初次之外不需要任何其他配置5.在Spring里集成Hibernate
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResources">
<list>
<value>com/springinaction/chapter05/po/Motorist.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties" value="org.hibernate.dialect.MySQLDialect"/>
</bean>
<!-- DAO配置 -->
<bean id="rantDao" class="com.springinaction.chapter05.dao.impl.HibernateRantDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
下面的HibernateRantDao是使用HibernateTemplate实现,HibernateTemplate具有一定的侵入性,会将DAO耦合到Spring API.
public class HibernateRantDao extends HibernateDaoSupport implements RantDao {
public void saveMotories(Motorist motorist) {
getHibernateTemplate().saveOrUpdate(motorist);
}
public Motorist getMotoristById(int id) {
return getHibernateTemplate().get(Motorist.class, id);
}
}
可以使用Hibernate 3引入的上下文回话管理事务的一个会话,下面为Spring解耦版本的HibernateRantDao
public class HibernateRantDao extends HibernateDaoSupport implements RantDao {
public void saveMotories(Motorist motorist) {
getSessionFactory().getCurrentSession().saveOrUpdate(motorist);
}
public Motorist getMotoristById(int id) {
return (Motorist) getSessionFactory().getCurrentSession().get(Motorist.class, id);
}
}
上面代码虽然是和Spring解耦,但是需要在事务中运行才可以,否则会抛出如下异常:
org.hibernate.HibernateException: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
错误原因:通过定义好的sessionFactory的getCurrentSession()方法获取当前线程中绑定的session,而当前线程绑定的session是通过当前的事务产生的,产生如上的错误是因为没有配置事务。当前线程中没创建session,则出现以上信息。