JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java Application Framework » Spring  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Spring+Hibernate是否可以应用于桌面程序呢?
unser





發文: 26
積分: 0
於 2006-03-26 12:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
配置环境:Eclipse 3.1.2 + MyEclipse 4.1
工程:Java Project(不是Web Project)
问题:如何初始化applicationContext.xml文件,当我初始化applicationContext.xml,执行Hibernate就出错,返回空指针,请问在桌面程序中如何初始化applicationContext.xml,并执行Hibernate。

文件目录:
1
2
3
4
5
6
mainTest.java
applicationContext.xml
User.hbm.xml
UserDAOImp.java
User.java
SessionFactory.java


applicationContext.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
 
  <bean id="springPro" class="org.apache.commons.dbcp.BasicDataSource">
  <!-- bean id="springPro" class="org.springframework.jdbc.datasource.DriverManagerDataSource"-->  
    <property name="driverClassName">
      <value>com.mysql.jdbc.Driver</value>
    </property>
    <property name="url">
      <value>jdbc:mysql://localhost:3306/springdb</value>
    </property>
    <property name="username">
      <value>root</value>
    </property>
    <property name="password">
      <value>654123</value>
    </property>
  </bean>
  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
      <ref bean="springPro" />
    </property>
    <property name="hibernateProperties">
      <props>
        <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
      </props>
    </property>
    <property name="mappingResources">
      <list>
        <value>web/Hibernate/User.hbm.xml</value>
      </list>
    </property>
  </bean>
</beans>


mainTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.io.*;
 
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate3.HibernateTemplate;
 
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
 
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.struts.ContextLoaderPlugIn;
 
import web.Hibernate.User;
import web.Hibernate.UserDAOImp;
 
//import com.test.UserDAOImp;
//import com.test.Hibernate.User;
public class mainTest {
 
  /**
   * @param args
   */
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ContextLoaderPlugIn clpi=new ContextLoaderPlugIn();
    clpi.setContextConfigLocation("D:/My Works/workspace/springPro/web/applicationContext.xml");
 
    //System.out.println(clpi.getContextClass());
    //System.out.println(clpi.getContextConfigLocation());
    UserDAOImp userDAO=new UserDAOImp();
    List users = userDAO.findAllUser();
    //System.out.println(users);
    for (int i=0;i<users.size();i++) {
      System.out.println(((User)users.get(i)).toString());
    }
 
  }
 
}


UserDAOImp.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package web.Hibernate;
 
import java.util.List;
 
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
public class UserDAOImp extends HibernateDaoSupport {
 
  private static String hql = "from User u where u.username=? and u.password=?";
 
  public boolean isValidUser(String username, String password) {
 
    String[] userlist = new String[2];
    userlist[0] = username;
    userlist[1] = password;
    // List userList = this.getHibernateTemplate().find(hql,
    // (Object)username);
    List userList = this.getHibernateTemplate().find(hql, userlist);
 
    if (userList.size() > 0) {
 
      return true;
 
    }
 
    return false;
 
  }
  
  public boolean addUser(User user) {
    try {
      this.getHibernateTemplate().save(user);
    } catch (Exception e) {
      System.out.println(e.getMessage());
      return false;
    }
    return true;
  }
  
  public List findAllUser() {
    HibernateTemplate h= this.getHibernateTemplate();
    List users =h.find("from User u");
    if ( users!=null && users.size()>0 )
      return users;
    return null;
  }
  
  /* (non-Javadoc)
   * @see com.test.UserDAO#delUser(java.lang.Integer)
   */
  public boolean delUser(Integer lid) {
    try {
      User users = (User)this.getHibernateTemplate().load(User.class,lid);
      this.getHibernateTemplate().delete(users);  
    } catch (Exception e) {
      System.out.print(e.getMessage());
      return false;  
    }
    return true;
  }
  
  public User editUser(Integer lid) {
    
    User users = null;
    try {
      users = (User)this.getHibernateTemplate().load(User.class,lid);
    } catch (Exception e) {
      System.out.print(e.getMessage());
      return null;  
    }
    return users;
  }
 
}
 


reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
4996 Spring+Hibernate是否可以应用于桌面程序呢? unser 4690 2006-03-26 12:48
3930 Re:Spring+Hibernate是否可以应用于桌面程序呢? caterpillar 118 2006-03-26 15:15
3943 Re:Spring+Hibernate是否可以应用于桌面程序呢? unser 103 2006-03-26 15:18
3891 Re:Spring+Hibernate是否可以应用于桌面程序呢? caterpillar 78 2006-03-26 15:31
4346 Re:Spring+Hibernate是否可以应用于桌面程序呢? unser 29 2006-03-26 15:35
» JWorld@TW »  Java Application Framework » Spring

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8