嗨,這是我第一次使用spring和hibernate的應用程序。所以請耐心等待我的愚蠢問題:)。 我在netbeans 6.7中創建了一個簡單的java應用程序。 這裏是我的daos接口 UsersDAO使用Spring DAO和Hibernate的數據訪問層
package Dao;
import Entities.Users;
public interface UsersDAO {
public Long GetIdByUsernameAndPasswor(String username, String password);
public Users GetAllByID(Long id);
public boolean Create(Users user);
public boolean Delete(Users user);
public boolean Edit(Users user);
}
和的ContactDAO
package Dao;
import Entities.Contacts;
import java.util.List;
public interface ContactsDAO {
public List GetAll();
public Contacts GetAllById(Long Id);
public boolean Create(Contacts contact);
public boolean Delete(Contacts contact);
public boolean Edit(Contacts contact);
}
及其實現
package Dao.DaoImpl;
import Dao.UsersDAO;
import Entities.Users;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UserDAOImpl extends HibernateDaoSupport implements UsersDAO {
// private SessionFactory sessionFactory;
public UserDAOImpl(){}
public Long GetIdByUsernameAndPasswor(String username, String password)
{
try
{
return DataAccessUtils.longResult(getHibernateTemplate().find("select u.user_id from Users u where u.username=? and u.password", new Object[] {username, password}));
}
catch(Exception ex)
{
ex.printStackTrace();
return Long.parseLong("0");
}
}
public Users GetAllByID(Long id) {
try
{
return (Users) getHibernateTemplate().get(Users.class, id);
}
catch(Exception ex)
{
ex.printStackTrace();
return new Users();
}
}
public boolean Create(Users user) {
try
{
getHibernateTemplate().save(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Delete(Users user) {
try
{
getHibernateTemplate().delete(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Edit(Users user) {
try
{
getHibernateTemplate().saveOrUpdate(user);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
package Dao.DaoImpl;
import Dao.ContactsDAO;
import Entities.Contacts;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class ContactsDAOImpl extends HibernateDaoSupport implements ContactsDAO{
public ContactsDAOImpl(){}
public List GetAll() {
try
{
return getHibernateTemplate().find("from Contacts");
}
catch(Exception ex)
{
ex.printStackTrace();
return null;
}
}
public Contacts GetAllById(Long Id) {
return (Contacts) getHibernateTemplate().get(Contacts.class, Id);
}
public boolean Create(Contacts contact) {
try
{
getHibernateTemplate().save(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Delete(Contacts contact) {
try
{
getHibernateTemplate().delete(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
public boolean Edit(Contacts contact) {
try
{
getHibernateTemplate().saveOrUpdate(contact);
return true;
}
catch(Exception ex)
{
ex.printStackTrace();
return false;
}
}
}
我的spring配置網絡連接文件是文件夾Resources.so通常路徑是Resouces/contactmanagement.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/ContactsMan" />
<property name="username" value="root" />
<property name="password" value="letmein" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.SessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingResouces">
<list>
<value>Resources/users.hbm.xml</value>
<value>Resources/contacts.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
</bean>
<bean id="usersdao" class="Dao.DaoImpl.UserDAOImpl">
<property name="sessionFactory" ref="sessionFactory">
</bean>
<bean id="contactsdao" class="Dao.DaoImpl.ContactDAOImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
</beans>
映射文件是相同的資源文件夾下下users.hbm.xml contacts.hbm.xml
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Entities.Contacts" table="contacts">
<id name="contact_id">
<generator class="increment"/>
</id>
<many-to-one cascade="" class="Users" name="user"/>
<property name="firstname" />
<property name="lasstname" />
<property name="cellphone1" />
<property name="cellphone2" />
<property name="telephone" />
<property name="email" />
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<class name="Entities.Users" table="users">
<id name="users_id">
<generator class="increment"/>
</id>
<bag name="contacts" inverse="true" lazy="true">
<key column="user_id"/>
<one-to-many class="Contacts"/>
</bag>
<property name="username"/>
<property name="passsword"/>
<property name="city"/>
<property name="country"/>
</class>
</hibernate-mapping>
這是我的最後的主類
package main;
import Dao.UsersDAO;
import Entities.Users;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext
public class contactmanagement {
public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext("Resources/contactmanagement.xml");
UsersDAO usersdao = (UsersDAO) ctx.getBean("usersdao");
Users user = new Users();
user.setUsername("me");
user.setPassword("mypass");
user.setCity("somecity");
user.setCountry("somecountry");
usersdao.Create(user);
System.out.println("created");
}
當我運行這個它說要給出一個總結「沒有名爲'usersdao'的bean被定義爲」 請問我做錯了什麼? 這裏是關於DAO實現類的另一個問題。我應該設置屬性setSessionFactory?或者通過getHibernateTemplate()來處理每一件事情? 請讓我通過this.Thanks閱讀。我知道這是很長的;)
你的格式是一團糟。我甚至不會去嘗試閱讀。 – skaffman 2009-07-10 15:24:41
修復格式,我們來看看。 – 2009-07-10 15:29:04