spring jdbcTemplate
来源:互联网 发布:知乎日报 for mac dmg 编辑:程序博客网 时间:2024/05/22 09:02
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="com.tl,com.royzhou.jdbc" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driverClassName}" />
<property name="url" value="${url}" />
<property name="username" value="${username}" />
<property name="password" value="${password}" />
<!-- 连接池启动时的初始值 -->
<property name="initialSize" value="${initialSize}" />
<!-- 连接池的最大值 -->
<property name="maxActive" value="${maxActive}" />
<!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
<property name="maxIdle" value="${maxIdle}" />
<!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
<property name="minIdle" value="${minIdle}" />
</bean>
<bean id="jdbcTemplate"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 定义事务传播属性XML配置 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" propagation="NOT_SUPPORTED" read-only="true"/>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="transactionPointCut" expression="execution(* com.royzhou.jdbc..*.*(..))"/>
<aop:advisor pointcut-ref="transactionPointCut" advice-ref="txAdvice"/>
</aop:config>
</beans>
package com.royzhou.jdbc;
public class PersonBean {
private int id;
private String name;
public PersonBean() {
}
public PersonBean(String name) {
this.name = name;
}
public PersonBean(int id, String name) {
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString() {
return this.id + ":" + this.name;
}
}
package com.royzhou.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
@SuppressWarnings("unchecked")
public class PersonRowMapper implements RowMapper {
//默认已经执行rs.next(),可以直接取数据
public Object mapRow(ResultSet rs, int index) throws SQLException {
PersonBean pb = new PersonBean(rs.getInt("id"),rs.getString("name"));
return pb;
}
}
package com.royzhou.jdbc;
import java.util.List;
public interface PersonService {
public void addPerson(PersonBean person) throws Exception;
public void updatePerson(PersonBean person);
public void deletePerson(int id);
public PersonBean queryPerson(int id);
public List<PersonBean> queryPersons();
}
package com.royzhou.jdbc;
import java.sql.Types;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@Service("personService")
public class PersonServiceImpl implements PersonService {
@Resource
private JdbcTemplate jdbcTemplate;
/**
* 通过Spring容器注入datasource
* 实例化JdbcTemplate,该类为主要操作数据库的类
* @param ds
public void setDataSource(DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
*/
public void addPerson(PersonBean person) throws Exception {
/**
* 第一个参数为执行sql
* 第二个参数为参数数据
* 第三个参数为参数类型
*/
jdbcTemplate.update("insert into person values(seq_person.nextval,?)", new Object[]{person.getName()}, new int[]{Types.VARCHAR});
//throw new RuntimeException("运行期异常支持事务回滚");
//throw new Exception("其他异常不支持事务回滚");
}
public void deletePerson(int id) {
jdbcTemplate.update("delete from person where id = ?", new Object[]{id}, new int[]{Types.INTEGER});
}
@SuppressWarnings("unchecked")
public PersonBean queryPerson(int id) {
/**
* new PersonRowMapper()是一个实现RowMapper接口的类,
* 执行回调,实现mapRow()方法将rs对象转换成PersonBean对象返回
*/
List<PersonBean> pbs = (List<PersonBean>)jdbcTemplate.query("select id,name from person where id = ?", new Object[]{id}, new PersonRowMapper());
PersonBean pb = null;
if(pbs.size()>0) {
pb = pbs.get(0);
}
return pb;
}
@SuppressWarnings("unchecked")
public List<PersonBean> queryPersons() {
List<PersonBean> pbs = (List<PersonBean>) jdbcTemplate.query("select id,name from person", new PersonRowMapper());
return pbs;
}
public void updatePerson(PersonBean person) {
jdbcTemplate.update("update person set name = ? where id = ?", new Object[]{person.getName(), person.getId()}, new int[]{Types.VARCHAR, Types.INTEGER});
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.tl,com.royzhou.jdbc" />
<!-- spring任务 调度 -->
<task:executor id="executor" pool-size="5" />
<task:scheduler id="scheduler" pool-size="10" />
<task:annotation-driven executor="executor" scheduler="scheduler" />
<!--
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <value>classpath:jdbc.properties</value>
</property> </bean>
-->
<!-- jndi连接池配置 -->
<bean id="jndiDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:comp/env/jdbc/gsjg</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="jndiDataSource" />
<property name="mappingLocations">
<list>
<value>classpath:/com/tl/bean/*.hbm.xml</value>
</list>
</property>
<!--
<property name="annotatedClasses">
<list>
<value>com.tl.bean.system.User</value>
</list>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 使用ehcache,适合项目用 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<!-- 查询方法使用二级缓存-->
<prop key="hibernate.cache.use_query_cache">true</prop>
<!-- 最优化二级缓存-->
<prop key="hibernate.cache.use_structured_entries">true</prop>
<!-- 完全禁用二级缓存开关,对那些在类的映射定义中指定cache的类,默认开启二级缓存-->
<prop key="cache.use_second_level_cache">true</prop>
<!-- prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.default_schema">
${dbunit.schema}
</prop> -->
</props>
</property>
<property name="packagesToScan">
<list>
<value>com.tl.bean</value>
</list>
</property>
</bean>
<!-- spring定时器 start-->
<bean id="dayDataJob" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass">
<value>com.servlet.DayDataQuartzTask</value>
</property>
</bean>
<!-- 调度cron工作 -->
<bean id="dayDataJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail">
<ref bean="dayDataJob"/>
</property>
<property name="cronExpression">
<value>0 30 0 * * ?</value>
</property>
</bean>
<!-- 启动工作 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="triggers">
<list>
<ref bean="dayDataJobTrigger"/>
</list>
</property>
</bean>
<!-- spring定时器 end -->
<!-- Hibernate 模板//-->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 事务处理的AOP配置 //
<bean id="txProxyTemplate" abstract="true"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="txManager" ref="txManager"/>
<property name="transactionAttributes">
<props>
<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="save">PROPAGATION_REQUIRED</prop>
<prop key="update">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>-->
<context:component-scan base-package="org.lxh" />
<tx:annotation-driven transaction-manager="txManager"/>
</beans>
@Controller("fwjkAction")
public class FwjkAction extends BaseAction implements ModelDriven<FwjkEntity>
{
private FwjkEntity model = new FwjkEntity();
@Resource
private PersonService personService;
public PersonService getPersonService() {
return personService;
}
public void setPersonService(PersonService personService) {
this.personService = personService;
}
web.xml配置
<context-param><param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/applicationContext-*.xml,classpath*:applicationContext-*.xml</param-value> -->
<param-value>classpath:beans.xml,classpath:bean-sqlserver.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
<!-- default: /WEB-INF/applicationContext.xml -->
</listener>
- spring JdbcTemplate
- spring jdbcTemplate
- Spring JdbcTemplate
- Spring JDBCTemplate
- Spring jdbctemplate
- spring JdbcTemplate
- spring jdbcTemplate
- Spring JDBCTemplate
- spring jdbctemplate
- spring jdbctemplate
- Spring JdbcTemplate
- Spring--JdbcTemplate
- spring jdbcTemplate
- Spring jdbcTemplate
- spring-jdbcTemplate
- Spring JdbcTemplate
- spring jdbcTemplate
- Spring JdbcTemplate
- 使用ssh端口映射
- 开始使用Mac OS X——写给Mac新人
- Android SDK Manager更新时”https://dl-ssl.google.com refused“异常处理办法按win微标键+R。输入C:\WINDOWS\system32\drive
- hdu 5000 Clone(dp)
- 使用dom4j操作xml文件
- spring jdbcTemplate
- spring定时器Quartz
- GC垃圾收集器 C#
- Oracle数据库升级前必要的准备工作
- jsp中freemarker 数字格式化
- QML类型说明-JumpListLink
- Java--猜数字!!
- UITabBarController与UINavigationController配合使用
- 关于代码手写UI,xib和StoryBoard