spring +hibernate练习

Posted 如风105

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring +hibernate练习相关的知识,希望对你有一定的参考价值。

1.实体类用的是orcal数据库中的Users表的方向工程

2、IUserDao.java

1 package com.yh.dao;
2 
3 import com.yh.entity.Users;
4 
5 public interface IUserDao {
6      public Users findUser(Users us);
7 }

3、UserDao.java

 1 package com.yh.dao.impl;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.orm.hibernate3.HibernateTemplate;
 6 import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 7 
 8 import com.yh.dao.IUserDao;
 9 import com.yh.entity.Users;
10 
11 public class UserDao extends HibernateDaoSupport implements IUserDao {
12     
13 
14     public Users findUser(Users us) {
15         // TODO Auto-generated method stub
16         Users user=null;
17         HibernateTemplate ht=this.getHibernateTemplate();
18         List<Users> list=ht.findByExample(us);
19         if(list.size()==0){return null;}
20         return list.get(0);
21     }
22 
23 }

4、IUserBiz.java

1 package com.yh.biz;
2 
3 import com.yh.entity.Users;
4 
5 public interface IUserBiz {
6    public Users login(Users us);
7 }

5、UserBiz.java

 1 package com.yh.biz.impl;
 2 
 3 import com.yh.biz.IUserBiz;
 4 import com.yh.dao.IUserDao;
 5 import com.yh.entity.Users;
 6 
 7 public class UserBiz implements IUserBiz {
 8     public IUserDao userDao;
 9     
10     public IUserDao getUserDao() {
11         return userDao;
12     }
13 
14     public void setUserDao(IUserDao userDao) {
15         this.userDao = userDao;
16     }
17 
18     public Users login(Users us) {
19         // TODO Auto-generated method stub
20         return userDao.findUser(us);
21     }
22 
23 }

6、SSHTest.java

 1 package com.yh.Test;
 2 
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6 
 7 import com.yh.biz.IUserBiz;
 8 import com.yh.entity.Users;
 9 
10 
11 public class SSHTest {
12     @Test
13     public void login(){
14         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
15         IUserBiz biz=(IUserBiz) ac.getBean("userBiz");
16         Users us=new Users();
17         us.setName("sa");
18         us.setPassword("123");
19         Users u=biz.login(us);
20         
21         if(u!=null){
22             System.out.println("登录成功!");
23         }else{
24             System.out.println("登录失败!");
25         }
26     }
27 }

7、hibernate.cfg.xml

 1 <?xml version=\'1.0\' encoding=\'UTF-8\'?>
 2 <!DOCTYPE hibernate-configuration PUBLIC
 3           "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 4           "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
 5 
 6 <!-- Generated by MyEclipse Hibernate Tools.                   -->
 7 <hibernate-configuration>
 8 
 9     <session-factory>
10         <property name="dialect">
11             org.hibernate.dialect.Oracle9Dialect
12         </property>
13         <property name="connection.url">
14             jdbc:oracle:thin:@172.16.17.158:1521:orcl
15         </property>
16         <property name="connection.username">scott</property>
17         <property name="connection.password">tiger</property>
18         <property name="connection.driver_class">
19             oracle.jdbc.OracleDriver
20         </property>
21         <property name="myeclipse.connection.profile">yh</property>
22         <property name="show_sql">true</property>
23         <property name="format_sql">true</property>
24         <mapping resource="com/yh/entity/Users.hbm.xml" />
25 
26     </session-factory>
27 
28 </hibernate-configuration>

8、applicationContext.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans
 3     xmlns="http://www.springframework.org/schema/beans"
 4     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5     xmlns:p="http://www.springframework.org/schema/p"
 6     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
 7 
 8 
 9     <bean id="sessionFactory"
10         class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
11         <property name="configLocation"
12             value="classpath:hibernate.cfg.xml">
13         </property>
14     </bean>
15     
16     <bean id="userDao" class="com.yh.dao.impl.UserDao">
17          <property name="sessionFactory" ref="sessionFactory"></property>    
18     </bean>
19     
20     <bean id="userBiz" class="com.yh.biz.impl.UserBiz">
21          <property name="userDao" ref="userDao"></property>
22     
23     </bean>
24     
25     
26     
27     </beans>

 

以上是关于spring +hibernate练习的主要内容,如果未能解决你的问题,请参考以下文章

Spring Hibernate 单机关系

来自 AsyncUncaughtExceptionHandler(spring) 的休眠数据库操作

spring mvc+spring + hibernate 整合

ssh(sturts2_spring_hibernate) 框架搭建之spring

CannotAcquireLockException(Spring、Hibernate、MySQL)

hibernate 跟 spring 做集成