麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

Spring

2019-11-14 08:54:44
字體:
供稿:網(wǎng)友

SPRing的相關(guān)概念:

Spring是一個(gè)開源的,輕量級的框架,輕量級的意思是依賴的東西比較少。spring的核心功能是aop(面向切面編程),ioc(控制反轉(zhuǎn)) 2.1 aop:在不改源代碼的情況下去增加功能。 2.2 ioc:不需要自己去new對象了,可以交給spring去創(chuàng)建。spring是一站式框架:spring在javaee三層中都提供了相應(yīng)的解決方案。 3.1 web層 springMvc 3.2 service ioc 3.3 jdbcTemplatespring的IOC操作: 4.1 把對象的創(chuàng)建交給ioc進(jìn)行管理。 4.2 ioc可以通過配置文件或者注解的方式的實(shí)現(xiàn)。IOC的底層原理: 5.1 使用xml的配置文件。 5.2 利用dom4j去解析xml文件。 5.3 利用工廠設(shè)計(jì)模式。 5.4 使用反射技術(shù)。 這里寫圖片描述

入門案例:

導(dǎo)入相關(guān)的jar包 1.1 spring相關(guān)的功能 這里寫圖片描述 1.2 一般只要導(dǎo)入核心的jar就好了,最后還要導(dǎo)入支持日志輸出的jar包。創(chuàng)建類,并添加相應(yīng)的方法 這里寫圖片描述

創(chuàng)建spring相關(guān)的配置文件 3.1 名字可以自己取,建議使用applicationContext,位置也是不固定的,建議位于src下。 3.2 配置對象創(chuàng)建

<?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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <bean id="user" class="com.renwen.user.User"></bean></beans>進(jìn)行測試 這里寫圖片描述

spring bean管理:

bean實(shí)例化的三種方式: 1.1 使用無參的構(gòu)造函數(shù)進(jìn)行創(chuàng)建(重點(diǎn)): 這里寫圖片描述 1.2 使用靜態(tài)工廠進(jìn)行創(chuàng)建:即靜態(tài)方法返回類對象 這里寫圖片描述 這里寫圖片描述 1.3 使用實(shí)例工程進(jìn)行創(chuàng)建:即實(shí)例方法返回類對象 這里寫圖片描述 這里寫圖片描述

bean標(biāo)簽常用的屬性: 2.1 id屬性:可以通過id來得到某個(gè)類的實(shí)例對象。 2.2 class屬性:配置對象所在的全路徑。 2.3 name屬性:和id的功能基本相同,但是id的命名不可以有特殊符號(hào),但是name屬性可以,且name屬性可以為同一個(gè)bean配置多個(gè)名字(基本不用了)。 2.4 scope屬性:singleton:單例的且是默認(rèn) prototype:多例

屬性注入:創(chuàng)建對象的時(shí)候,向類里面的屬性設(shè)置相應(yīng)的值。 3.1 使用屬性的setter方法進(jìn)行注入(重點(diǎn)) 這里寫圖片描述 這里寫圖片描述 3.2 有參數(shù)的構(gòu)造函數(shù)的方法進(jìn)行注入 這里寫圖片描述 這里寫圖片描述

對象注入:

public class Dao { public void add() { System.out.println("dao add"); }}public class Service { private Dao dao; public void add() { System.out.println("add...."); dao.add(); } public void setDao(Dao dao) { this.dao = dao; }}<bean id="dao" class="com.renwen.test.Dao"></bean><bean id="service" class="com.renwen.test.Service"> <property name="dao" ref="dao"></property></bean>

p名稱空間的注入: 5.1 第一步引入p名稱空間的約束

public class User { private String uname; public void show(){ System.out.println(uname); } public void setUname(String uname){ this.uname=uname; }}<bean id="service" class="com.renwen.test.Service"> <property p:uname="zhao"></property></bean>

注入負(fù)責(zé)數(shù)據(jù)類型(數(shù)組,list,map,properties)

public class User { private String[]array; private Map<String,String>map; private List<String>list; private Properties properties; public void setArray(String[] array) { this.array = array; } public void setMap(Map<String, String> map) { this.map = map; } public void setList(List<String> list) { this.list = list; } public void setProperties(Properties properties) { this.properties = properties; } public void test() { for(String str:array){ System.out.println(str); } for(String str:list){ System.out.println(str); } Set<String>keySet=map.keySet(); Iterator<String>it=keySet.iterator(); while(it.hasNext()){ System.out.println(map.get(it.next())); } System.out.println(properties); }}<bean id="user" class="com.renwen.user.User"> <!-- 數(shù)組注入 --> <property name="array"> <array> <value>zhangsan1</value> <value>zhangsan2</value> <value>zhangsan3</value> </array> </property> <!-- list注入 --> <property name="list"> <list> <value>lisi1</value> <value>lisi2</value> <value>lisi3</value> </list> </property> <!-- map注入 --> <property name="map"> <map> <entry key="lisi1" value="zhangsan1"></entry> <entry key="lisi2" value="zhangsan2"></entry> <entry key="lisi3" value="zhangsan3"></entry> </map> </property> <!--property注入--> <property name="properties"> <props> <prop key="url">jdbc:MySQL:/127.0.0.1</prop> <prop key="username">root</prop> </props> </property></bean>

IOC 和DI的區(qū)別:

IOC:控制反轉(zhuǎn),把創(chuàng)建對象交給spring。DI: 依賴注入,例如在進(jìn)行對象創(chuàng)建的時(shí)候,我們可以為屬性設(shè)置相應(yīng)的值。關(guān)系:依賴不能單獨(dú)存在,需要在ioc基礎(chǔ)上完成操作。

Spring整合web項(xiàng)目的原理:

加載spring核心配置文件,根據(jù)核心配置文件來進(jìn)行創(chuàng)建對象。

ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");

但是效率比較差,解決方法可以在服務(wù)器啟動(dòng)的時(shí)候進(jìn)行創(chuàng)建,并加載spring配置文件。

實(shí)現(xiàn)原理: 2.1 使用ServletContext對象 2.2 使用監(jiān)聽器 2.3 具體使用: 在服務(wù)器啟動(dòng)的時(shí)候會(huì)為我們創(chuàng)建一個(gè)唯一的servletContext對象,然后通過ServletContextListener來監(jiān)聽servletContext的產(chǎn)生,在該對象創(chuàng)建之前,可以創(chuàng)建配置文件加載的對象,然后將該對象放入context域中。

Spring 注解管理bean

注解介紹: 1.1 代碼里面的特殊標(biāo)記,完成特定的功能。 1.2 注解的寫法 @注解名(屬性名稱=屬性值)。 1.3 注解可以使用在類上面,方法上面,屬性上面。注解開發(fā)案例: 2.1 導(dǎo)入注解開發(fā)相應(yīng)的jar包,引入相應(yīng)得約束文件

注解創(chuàng)建對象:

@Component(value="user")@Scope(value="prototype")//多實(shí)例public class User { public void show(){ System.err.println("show...."); }}public class TestSpring { @Test public void test(){ ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml"); User user=(User) ac.getBean("user"); user.show(); }}

4 創(chuàng)建對象4個(gè)常用注解: 4.1 @Component 4.2 @Controller WEB層 4.3 @Service service層 4.4 @Respository dao層 目前這四個(gè)注解的功能都一樣,創(chuàng)建對象

屬性注入: 5.1 使用注解的方法進(jìn)行注入的時(shí)候,不需要setter方法 5.2 autowired進(jìn)行注入

@Repository(value="dao")public class Dao { public void add() { System.out.println("dao add"); }}@Component("service")public class Service { @Autowired private Dao dao; public void add() { System.out.println("service add...."); dao.add(); }}

5.3 Resource進(jìn)行注入

@Repository(value="dao") public class Dao { public void add() { System.out.println("dao add"); }}@Component("service")public class Service { @Resource(name="dao") private Dao dao; public void add() { System.out.println("service add...."); dao.add(); }}

5.4 注解與配置文件混合使用:

public class Dao { public void add() { System.out.println("dao add");}public class OrderDao { public void buy() { System.out.println("Order buy...."); }}public class Service { @Resource(name="dao") private Dao dao; @Resource(name="orderDao") private OrderDao orderDao; public void add() { System.out.println("service add...."); dao.add(); orderDao.buy(); }}<bean id="dao" class="com.renwen.test.Dao"></bean><bean id="orderDao" class="com.renwen.test.OrderDao"></bean><bean id="service" class="com.renwen.test.Service"></bean>

AOP :

AOP概述:面向切面編程,擴(kuò)展功能不通過修改源代碼來實(shí)現(xiàn)。AOP采取橫向抽取機(jī)制,取代了傳統(tǒng)縱向繼承體系的重復(fù)性代碼。相關(guān)的原理: 這里寫圖片描述 這里寫圖片描述

AOP 操作的相關(guān)術(shù)語:

這里寫圖片描述

Spring相關(guān)的AOP操作:

在spring中進(jìn)行aop操作,使用aspectj來實(shí)現(xiàn),aspecj是一個(gè)面向切面的框架,使用之前應(yīng)該導(dǎo)入相應(yīng)的jar包,且導(dǎo)入aop的相關(guān)約束。aspecj并不是spring的一部分。

使用aspectj實(shí)現(xiàn)aop的兩種方式 3.1 基于xml 這里寫圖片描述

<bean id="book" class="com.renwen.aop.Book"></bean><bean id="mybook" class="com.renwen.aop.MyBook"></bean><aop:config> <!--配置切入點(diǎn)--> <aop:pointcut expression="execution(* com.renwen.aop.Book.add(..))" id="addcut"/> <!--配置切面--> <aop:aspect ref="mybook"> <aop:before method="Before1" pointcut-ref="addcut"/> </aop:aspect></aop:config>

環(huán)繞增強(qiáng):

public class MyBook { public void around(ProceedingJoinPoint point) throws Throwable{ System.out.println("前"); point.proceed(); System.out.println("后"); }}<aop:config><!--配置切入點(diǎn)--><aop:pointcut expression="execution(* com.renwen.aop.Book.add(..))" id="addcut"/><!--配置切面--><aop:aspect ref="mybook"> <!-- <aop:before method="before1" pointcut-ref="addcut"/> --> <aop:around method="around" pointcut-ref="addcut"/></aop:aspect>

3.2 基于注解

log4J的介紹:

通過log4j可以看到程序運(yùn)行中更加相信的信息。可以通過它來查看日志需要導(dǎo)入jar包并且導(dǎo)入配置文件到src下

log4j.properties

log4j.rootLogger=INFO, CONSOLE, FILE ## for console log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern=%d{MM-ddHH:mm:ss}[%c-%L][%t][%-4r] - %m%n ## for file log4j.appender.FILE=org.apache.log4j.RollingFileAppender log4j.appender.FILE.File=D:/logs/log4j.log log4j.appender.FILE.MaxFileSize=1MB log4j.appender.FILE.Append = true log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-ddHH:mm:ss} [%t] %-5p %c(line-%L) %-4r %x - %m%n

注解AOP配置

創(chuàng)建相關(guān)配置文件,創(chuàng)建對象。

開啟aop掃描。

<aop:aspectj-autoproxy></aop:aspectj-autoproxy>在增強(qiáng)的類上面使用注解來完成aop的操作。

注解設(shè)置

@Aspectpublic class MyBook { @Before("execution(* com.renwen.aop.Book.add(..))") public void before1(){ System.out.println("前置增強(qiáng)..."); }}

Spring 的jdbcTemplate操作:

jdbctemplate對jdbc做了封裝配置數(shù)據(jù)源:

相關(guān)操作

public class DataSource { private static DriverManagerDataSource dataSource=null; static{ dataSource=new DriverManagerDataSource(); dataSource.setDriverClassName("com.mysql.jdbc.Driver"); dataSource.setUrl("jdbc:mysql://localhost:3306/hibernate"); dataSource.setPassWord("zhaoliyou"); dataSource.setUsername("root"); } public static DriverManagerDataSource getDataSource(){ return dataSource; }}3. 相關(guān)操作: public class JdbcTemplateTest { private static DriverManagerDataSource dataSource=DataSource.getDataSource(); @Test public void add(){ JdbcTemplate template=new JdbcTemplate(dataSource); String sql="insert into user(uname,pwd) values(?,?)"; int rows=template.update(sql,"zhangsan","123"); System.out.println(rows); } @Test public void update(){ JdbcTemplate template=new JdbcTemplate(dataSource); String sql="update user set uname=? where uname=?"; int rows=template.update(sql,"lisi","zhangsan"); System.out.println(rows); } @Test public void delete(){ JdbcTemplate template=new JdbcTemplate(dataSource); String sql="delete from user where uname=?"; int rows=template.update(sql,"lisi"); System.out.println(rows); } //對于結(jié)果集的處理,jdbctemplate有自己的接口RowMapper,但是需要自己進(jìn)行實(shí)現(xiàn) @Test public void query(){ JdbcTemplate template=new JdbcTemplate(dataSource); //查詢返回一個(gè)值 String sql="select count(*) from user"; int rows=template.queryForObject(sql, Integer.class); System.out.println(rows); //返回某個(gè)對象 String sql2="select * from user where uname=?"; User user=template.queryForObject(sql2, new UserMapper(),"zhangsan"); System.out.println(user); //返回List集合 String sql3="select * from user"; List<User>list=template.query(sql2, new UserMapper()); System.out.println(list); }}class UserMapper implements RowMapper<User>{ @Override public User mapRow(ResultSet rs, int arg1) throws SQLException { String uname=rs.getString("uname"); String pwd=rs.getString("pwd"); User user=new User(); user.setPwd(pwd); user.setUname(uname); return user; }}

Spring 配置c3p0連接池,dao使用jdbcTemplate

原始配置c3p0: 這里寫圖片描述

public class User { private String uname; private String pwd; public String getUname() { return uname; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } public void setUname(String uname) { this.uname = uname; } @Override public String toString() { return "User [uname=" + uname + ", pwd=" + pwd + "]"; }}public class UserDao { private JdbcTemplate jdbcTemplate; public void add(User user){ String sql="insert into user values(?,?)"; jdbcTemplate.update(sql,user.getUname(),user.getPwd()); } public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; }}public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add(User user) { System.out.println("service add"); userDao.add(user); }}<!-- 創(chuàng)建數(shù)據(jù)源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="zhaoliyou"></property> </bean> <!--創(chuàng)建jdbcTemplate并且注入數(shù)據(jù)源 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 創(chuàng)建UserDao,且注入jdbc模板--> <bean id="userDao" class="com.renwen.test.UserDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <!-- 創(chuàng)建userService并且注入U(xiǎn)serdao--> <bean id="userService" class="com.renwen.test.UserService"> <property name="userDao" ref="userDao"></property> </bean>

Spring事務(wù)管理:

事務(wù)管理的兩種方式: 1.1 編程式事務(wù)管理 1.2 聲明式事務(wù)管理:基于xml或者注解方式實(shí)現(xiàn)。

spring進(jìn)行事務(wù)管理的一些api操作: 2.1 PlatformTransactionManger:事務(wù)管理器 這里寫圖片描述 不同的持久層框架有不同的事務(wù)管理器 2.2 TransactionDefinition:事務(wù)定義信息 2.3 TransactionStatus :事務(wù)運(yùn)行狀態(tài) 3.xml事務(wù)管理:

public class Accountant { private int id; private String uname; private double balance; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}public class OrderDao { private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void addMoney(Accountant accountant,double money){ String sql="update accountant set balance=balance+? where id=?"; jdbcTemplate.update(sql,money,accountant.getId()); } public void lessMoney(Accountant accountant,double money){ String sql="update accountant set balance=balance-? where id=?"; jdbcTemplate.update(sql,money,accountant.getId()); }}public class OrderService { private OrderDao orderDao; public void setOrderDao(OrderDao orderDao) { this.orderDao = orderDao; } public void changeMoney(Accountant accountant1,Accountant accountant2,double money){ orderDao.addMoney(accountant1, money); int num=1/0; orderDao.lessMoney(accountant2, money); }}<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 創(chuàng)建數(shù)據(jù)源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="zhaoliyou"></property> </bean> <!--創(chuàng)建jdbcTemplate并且注入數(shù)據(jù)源 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="orderDao" class="com.renwen.test.OrderDao"> <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> <bean id="orderService" class="com.renwen.test.OrderService"> <property name="orderDao" ref="orderDao"></property> </bean> <!-- 配置事務(wù)管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置事務(wù)增強(qiáng)--> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="changeMoney" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--配置切面 --> <aop:config> <aop:pointcut expression="execution(* com.renwen.test.OrderService.*(..))" id="mycut"/> <aop:advisor advice-ref="txadvice" pointcut-ref="mycut"/> </aop:config></beans>

注解配置事務(wù): 4.1 配置事務(wù)管理器 4.2 開始事務(wù)注解掃描 4.3 在需要事務(wù)處理的方法或者類上加 @Transational

<!-- 配置事務(wù)管理器 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property></bean><!--開啟事務(wù)注解 --><tx:annotation-driven transaction-manager="transactionManager"/>@Transactionalpublic void changeMoney(Accountant accountant1,Accountant accountant2,double money){ orderDao.addMoney(accountant1, money); int num=1/0; orderDao.lessMoney(accountant2, money);}

SSH整合:

思想: 這里寫圖片描述

spring整合struts2 2.1 記得導(dǎo)入struts2.spring-plugin和spring-web包 2.2 配置web.xml

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>ssh</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param></web-app>

2.3 配置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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="userAction" class="com.renwen.action.UserAction" scope="prototype"></bean></beans>

2.4 配置struts.xml

<struts> <package name="mypack" extends="struts-default" namespace="/"> <action name="userAction" class="userAction"> <result name="ok">/index.jsp</result> </action> </package></struts>

spring 整合hibernate 3.1 把我們的數(shù)據(jù)庫的基本信息在spring進(jìn)行配置。 3.2 sessionfactory在spring中進(jìn)行創(chuàng)建。 3.3 注意需要spring-orm的jar

整合完整案例:

action

package com.renwen.action;import com.opensymphony.xwork2.ActionSupport;import com.opensymphony.xwork2.ModelDriven;import com.renwen.entity.User;import com.renwen.service.UserService;public class UserAction extends ActionSupport implements ModelDriven<User>{ private User user=new User(); private UserService userService; @Override public String execute() throws Exception { userService.change(1, 2, 50); return "ok"; } public void setUserService(UserService userService) { this.userService = userService; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } @Override public User getModel() { // TODO Auto-generated method stub return user; }}

service

package com.renwen.service;import org.springframework.transaction.annotation.Transactional;import com.renwen.dao.UserDao;import com.renwen.entity.User;public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } @Transactional public void change(int id1,int id2,int money){ userDao.add(id1, 50); int num=1/0; userDao.less(id2, 50); }}

dao

package com.renwen.dao;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.transaction.annotation.Transactional;import com.renwen.entity.Accountant;import com.renwen.entity.User;public class UserDao { private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void add(int id,int money){ Session session=sessionFactory.getCurrentSession(); Accountant accountant=(Accountant) session.get(Accountant.class, id); accountant.setBalance(accountant.getBalance()+money); session.save(accountant); } public void less(int id,int money){ Session session=sessionFactory.getCurrentSession(); Accountant accountant=(Accountant) session.get(Accountant.class, id); accountant.setBalance(accountant.getBalance()-money); session.save(accountant); }}

entity:

package com.renwen.entity;public class Accountant { private int id; private String uname; private double balance; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; }}

hibernate.cfg.xml配置文件:

<?xml version='1.0' encoding='UTF-8'?><!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"><!-- Generated by MyEclipse Hibernate Tools. --><hibernate-configuration> <session-factory> <property name="hibernate.show_sql">true</property> <!-- 格式化輸出sql語句 --> <property name="hibernate.format_sql">true</property> <!-- update如果有這個(gè)表了,就會(huì)更新,否則就會(huì)創(chuàng)建表 --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- 數(shù)據(jù)庫方言 --> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 把映射文件放到核心配置文件中 --> <mapping resource="com/renwen/entity/User.hbm.xml"/> <mapping resource="com/renwen/entity/Accountant.hbm.xml"/> </session-factory></hibernate-configuration><?xml version="1.0"?>

Accountant.hbm.xml

<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="com.renwen.entity.Accountant" table="accountant"> <id name="id" column="id"> <generator class="native"></generator> </id> <property name="uname" column="uname"></property> <property name="balance" column="balance"></property> </class></hibernate-mapping>

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: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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/hibernate"></property> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="user" value="root"></property> <property name="password" value="zhaoliyou"></property> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource"><ref bean="dataSource" /></property> <property name="configLocations" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="userService" class="com.renwen.service.UserService"> <property name="userDao" ref="userDao"></property> </bean> <bean id="userAction" class="com.renwen.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> </bean> <bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <tx:annotation-driven transaction-manager="hibernateTransactionManager" /></beans>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 国产一级性生活视频 | 欧美成人黄色小视频 | 成人免费福利 | 91精品国产一区二区在线观看 | 免费一级高清毛片 | 毛片在线免费观看完整版 | 久久久99精品视频 | 黄色片网站免费观看 | 91九色网址| 午夜精品久久久久久久久久久久久蜜桃 | 国产亚洲精品久久777777 | 黄色电影免费提供 | 国产免费午夜 | 国产在线精品一区二区 | 中文字幕 欧美 日韩 | chinese hd xxxx tube| 久久99精品视频在线观看 | 九九热精品在线播放 | 久久久久国 | 毛片一级免费看 | 精品一区二区久久久久久久网精 | 国产免费高清在线视频 | h视频免费在线 | 亚洲精品av在线 | 污片在线观看视频 | 99国产精品白浆在线观看免费 | 国产成人精品二区 | 天天夜干 | 欧美一级片 在线播放 | 日本成年免费网站 | 羞羞网站在线看 | 黄色大片免费网站 | 国产男女爽爽爽爽爽免费视频 | 欧美成人免费在线视频 | 久久久久久久国产视频 | 久久久久久久久久久综合 | av电影在线播放 | 第一区免费在线观看 | 嗯~啊~弄嗯~啊h高潮视频 | 欧美3p激情一区二区三区猛视频 | 福利在线国产 |