开发工具:Eclipse Juno
文件结构及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编辑区点击右键 > Run As > 2 Java Application
图片尺寸:1920x1080