spring+jdbc

来源:互联网 发布:美工高级课程 编辑:程序博客网 时间:2024/05/03 04:10

spring+jdbc步骤:

1,在配置文件中配置dataSource来连接数据库:

<!-- 读取配置文件 -->

<context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 数据源,配置数据库的连接和连接池 -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="${driverClassName}"/><!-- 连接数据的驱动 -->

<property name="url" value="${url}"/><!-- 练级数据库的url -->

<property name="username" value="${username}"/><!-- 用户名 -->

<property name="password" value="${password}"/><!-- 密码 -->

<property name="initialSize" value="${initialSize}"/><!-- 连接池的初始连接大小 -->

<property name="maxActive" value="${maxActive}"/> <!-- 连接池的最大连接 -->

<property name="maxIdle" value="${maxIdle}"/> <!-- 释放连接后保留的连接 -->

<property name="minIdle" value="${minIdle}"/> <!-- 释放连接后的最小连接数 -->

</bean>

2,在配置文件中配置事务的配置

<!-- 事务的操作管理器 -->

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

<property name="dataSource" ref="dataSource"/>

</bean>

<!-- 注解事务操作 -->

<tx:annotation-driven transaction-manager="txManager"/>

<!-- xml方式的事务操作 -->

<!--<aop:config>

<aop:pointcut id="mypt" expression="execution(* hwt.dao.PersonDAO.*(..))"/>

<aop:advisor advice-ref="mytx" pointcut-ref="mypt"/>

</aop:config>

<tx:advice id="mytx" transaction-manager="txManager">

<tx:attributes>

<tx:method name="get*" propagation="NOT_SUPPORTED" read-only="true"/>

<tx:method name="*"/>

</tx:attributes>

</tx:advice>-->

3,写入pojo

4,写入相应的到层,并且对要加事务的方法加上事务

@Transactional

public class PersonDAO {

private JdbcTemplate jdbcTemplate ;

//得到jdbc模板,要注意一下这个在配置文件中的注册,

public void setDataSource(DataSource dataSource){

   jdbcTemplate = new JdbcTemplate(dataSource);

}

//增,删,改都是一样的,用update,只是sql语句不同

//事务默认的传播属性为REQUIRED,所以不需要加下面的话

//@Transactional(propagation=Propagation.REQUIRED,readOnly=true)

public void update(Person person){

jdbcTemplate.update("update person set person_name = ? where person_id = ?",new Object[]{person.getPerson_name(),person.getPerson_id()},new int[]{java.sql.Types.VARCHAR,java.sql.Types.INTEGER});

}

//对于查询,不需要增加事务,可以设置为只读

//对于查询与增删改的模板不一样,查询一个有queryForObject,对个用query

@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)

public Person getPerson(Integer id){

return (Person)jdbcTemplate.queryForObject("select * from person where person_id = ?"new Object[]{id}, new int[]{java.sql.Types.INTEGER}, new PersonRowMapper());

}

//注解的事务对于checked的异常不会回滚,只会对unchecked的回滚

//可以改变上面说的那种状态

//@Transactional(rollbackFor=Exception.class) //设置这个异常发生的时候也要回滚

@Transactional(noRollbackFor=Exception.class)//设置这个异常不会回滚

public void getException() throws Exception{

throw new Exception("运行期异常");

}

}

5,注意,要把DAO层的这个类交个spring来管理,一定不要疏忽了这一点

<bean id="personDAO" class="hwt.dao.PersonDAO">

<property name="dataSource" ref="dataSource"></property>

</bean>

6.在用用查询的时候可以要写的回调函数

public class PersonRowMapper implements RowMapper {

public Object mapRow(ResultSet rs, int index) throws SQLException {

Person person = new Person();

person.setPerson_id(rs.getInt("person_id"));

person.setPerson_name(rs.getString("person_name"));

return person;

}

}

原创粉丝点击