关闭

[置顶] spring 简单操作数据库

标签: spring jdbctemplate
48人阅读 评论(0) 收藏 举报
分类:
package lq.dao.impl;

import lq.dao.ShopingDao;
import org.springframework.jdbc.core.JdbcTemplate;

public class ShoppingDaoImpl implements ShopingDao {
	JdbcTemplate jdbctemplate;
	
	

	public void setJdbctemplate(JdbcTemplate jdbctemplate) {
		this.jdbctemplate = jdbctemplate;
	}

	@Override
	public void updatemoney(String uname, int money) {
		String sql="update tx_users set money=money-? where uname=?";
		jdbctemplate.update(sql,money,uname);
	}

	@Override
	public void updatestorck(int id) {
		String sql="update tx_bstorck set storck=storck-1 where bid=?";
		jdbctemplate.update(sql,id);

	}

	@Override
	public int getbmoneybyid(int id) {
		String sql=" select price from tx_book where bid=?";
		System.out.println(jdbctemplate+"************************");
		return jdbctemplate.queryForObject(sql, Integer.class,id);
	}

}
package lq.service.impl;


import lq.dao.ShopingDao;
import lq.service.ShoppingService;


public class ShoppingServiceImpl implements ShoppingService {
<span style="white-space:pre">	</span>ShopingDao shopdao;
<span style="white-space:pre">	</span>public void setShopdao(ShopingDao shopdao) {
<span style="white-space:pre">		</span>this.shopdao = shopdao;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>public void shopping(String uname, int bid) {
<span style="white-space:pre">		</span>int money=shopdao.getbmoneybyid(bid);
<span style="white-space:pre">		</span>shopdao.updatemoney(uname, money);
<span style="white-space:pre">		</span>shopdao.updatestorck(bid);


<span style="white-space:pre">	</span>}


}

applicationContext.xml:

<?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:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
	
	<bean id="datasourse" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="username" value="scott"></property>
		<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"></property>
		<property name="password" value="tiger"></property>
	</bean>
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="datasourse"></property>
	</bean>
	<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="datasourse"></property>
	</bean>
	
	<bean id="shopingDao" class="lq.dao.impl.ShoppingDaoImpl">
		<property name="jdbctemplate" ref="jdbcTemplate"></property>
	</bean>
	<bean id="shoppingService" class="lq.service.impl.ShoppingServiceImpl" >
		<property name="shopdao" ref="shopingDao"></property>
	</bean>
	
	<!-- 事务增强配置 定义事务的传播方式,事务的隔离方式 -->
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="save*" propagation="REQUIRED"/>
			<tx:method name="insert*" propagation="REQUIRED" isolation="READ_COMMITTED"/>
			<tx:method name="delete*"/>
			<tx:method name="update*"/>
			<tx:method name="find*"/>
			<tx:method name="get*" propagation="SUPPORTS"/>
			<tx:method name="*"/>
		
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut expression="execution(* lq.service..*.*(..))" id="point"/>
		<aop:advisor advice-ref="txadvice" pointcut-ref="point"/>
	</aop:config>
	
</beans>

出现控制着那个异常,原因:
<property name="jdbctemplate" ref="jdbcTemplate"></property>
jdbctemplate 注入失败

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:144次
    • 积分:34
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档