日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring3 集成 Hibernate3

發(fā)布時間:2025/3/19 javascript 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring3 集成 Hibernate3 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

開發(fā)工具:Eclipse Juno

文件結(jié)構(gòu)及Jar包:

?

src\livon\Test.java

1 package livon; 2 3 import java.util.List; 4 5 import org.springframework.context.ApplicationContext; 6 import org.springframework.context.support.ClassPathXmlApplicationContext; 7 8 public class Test { 9 10 public static void main( String[] args){ 11 12 /*Resource r = new ClassPathResource("beans.xml"); 13 BeanFactory factory = new XmlBeanFactory(r);*/ 14 15 @SuppressWarnings("resource") 16 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 17 18 UserDao dao = (UserDao) context.getBean("userDao"); 19 User u = (User) context.getBean("user"); 20 21 //保存 22 dao.save(u); 23 24 List list = dao.list(); 25 26 for( int i=0; i<list.size(); i++){ 27 28 User list_i = (User) list.get(i) ; 29 30 System.out.println( list_i.getId()); 31 System.out.println( list_i.getAge()); 32 System.out.println( list_i.getUserName()); 33 } 34 35 } 36 37 }

?

?src\livon\User.java

1 package livon; 2 3 public class User { 4 5 6 7 private Integer id ; 8 private String userName ; 9 private int age ; 10 11 12 13 public Integer getId() { 14 return id; 15 } 16 public void setId(Integer id) { 17 this.id = id; 18 } 19 public int getAge() { 20 return age; 21 } 22 public void setAge(int age) { 23 this.age = age; 24 } 25 public String getUserName() { 26 return userName; 27 } 28 public void setUserName(String userName) { 29 this.userName = userName; 30 } 31 32 33 34 35 }

?

src\livon\UserDao.java

1 package livon; 2 3 4 import java.util.List; 5 6 public interface UserDao { 7 8 public void save( User u ); 9 public List list(); 10 11 12 }

?

src\livon\UserDaoImpl.java

1 package livon; 2 3 import java.util.List; 4 5 import org.springframework.orm.hibernate3.HibernateTemplate; 6 7 public class UserDaoImpl extends HibernateTemplate implements UserDao { 8 9 @Override 10 public void save(User u) { 11 // TODO Auto-generated method stub 12 super.save(u); 13 14 } 15 16 @Override 17 public List<User> list() { 18 // TODO Auto-generated method stub 19 String hql = "from User"; 20 return super.find( hql ); 21 } 22 23 } 24

?

src\livon\User.hbm.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <!-- 4 ~ Hibernate, Relational Persistence for Idiomatic Java 5 ~ 6 ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as 7 ~ indicated by the @author tags or express copyright attribution 8 ~ statements applied by the authors. All third-party contributions are 9 ~ distributed under license by Red Hat Inc. 10 ~ 11 ~ This copyrighted material is made available to anyone wishing to use, modify, 12 ~ copy, or redistribute it subject to the terms and conditions of the GNU 13 ~ Lesser General Public License, as published by the Free Software Foundation. 14 ~ 15 ~ This program is distributed in the hope that it will be useful, 16 ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 17 ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License 18 ~ for more details. 19 ~ 20 ~ You should have received a copy of the GNU Lesser General Public License 21 ~ along with this distribution; if not, write to: 22 ~ Free Software Foundation, Inc. 23 ~ 51 Franklin Street, Fifth Floor 24 ~ Boston, MA 02110-1301 USA 25 --> 26 27 <!DOCTYPE hibernate-mapping PUBLIC 28 "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 29 "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 30 31 <hibernate-mapping> 32 33 <class name="livon.User" table="usertbl"> 34 <id name="id" column="id"> 35 <generator class="native"/> 36 </id> 37 <property name="userName"/> 38 <property name="age"/> 39 </class> 40 41 </hibernate-mapping>

?

src\beans.xml

1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE beans PUBLIC "" "http://www.springframework.org/dtd/spring-beans.dtd" > 3 4 5 <beans> 6 7 8 9 10 <!-- user --> 11 12 <bean id="user" class="livon.User"> 13 <property name="userName" value="Livon" /> 14 <property name="age" value="38" /> 15 </bean> 16 17 18 <!-- userDao --> 19 20 <bean id="userDao" class="livon.UserDaoImpl"> 21 <property name="sessionFactory" ref="sessionFactory" /> 22 </bean> 23 24 25 <!-- sessionFactory --> 26 27 <bean id="sessionFactory" 28 class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 29 30 <property name="dataSource" ref="dataSource" /> 31 <property name="mappingResources"> 32 <list> 33 <value>livon/User.hbm.xml</value> 34 </list> 35 </property> 36 37 </bean> 38 39 40 <!-- dataSource --> 41 42 <bean id="dataSource" 43 class="org.apache.commons.dbcp.BasicDataSource"> 44 45 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 46 <property name="url" value="jdbc:mysql://localhost:3306/spring_db" /> 47 <property name="username" value="Livon" /> 48 <property name="password" value="Livon_2012" /> 49 50 </bean> 51 52 </beans>

Eclipse 工程源碼(含 jar 包)下載地址:

http://ishare.iask.sina.com.cn/f/36836371.html

?

運行:在Test.java編輯區(qū)點擊右鍵 > Run As > 2 Java Application

圖片尺寸:1920x1080

轉(zhuǎn)載于:https://www.cnblogs.com/livon/archive/2013/04/24/3039690.html

總結(jié)

以上是生活随笔為你收集整理的Spring3 集成 Hibernate3的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。