实体类:
public class Users implements java.io.Serializable {
private int id;
private String name;
private String pass;
Get set省略
}
映射文件:(表名与字段名都和实体类中一样)
<hibernate-mapping>
<class name="com.ssh.Users" table="Users" >
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" type="java.lang.String"></property>
<property name="pass" type="java.lang.String"></property>
</class>
</hibernate-mapping>接口:
public interface UsersDao {
public Users get(int id);
}
接口实现类:
public class UsersDaoHiberImpl extends HibernateDaoSupport
implements UsersDao{
public Users get(int id)
{
return (Users)super.getHibernateTemplate() .get(Users.class, id);
}
}Hibernate配置文件:
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databasename=zf
</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.username">zf</property>
<property name="connection.password">zf</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/ssh/Users.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>Spring配置文件:
<?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="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="usersDao" class="com.ssh.UsersDaoHiberImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>Main方法中代码:
UsersDao ud=new UsersDaoHiberImpl();
Users u=(Users)ud.get(1);
System.out.println(u.getName());运行后:
Exception in thread "main" java.lang.NullPointerException
at com.ssh.UsersDaoHiberImpl.get(UsersDaoHiberImpl.java:20)
at test.T.main(T.java:30)
20行指的是:
return (Users)super.getHibernateTemplate().get(Users.class, id);
public class Users implements java.io.Serializable {
private int id;
private String name;
private String pass;
Get set省略
}
映射文件:(表名与字段名都和实体类中一样)
<hibernate-mapping>
<class name="com.ssh.Users" table="Users" >
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" type="java.lang.String"></property>
<property name="pass" type="java.lang.String"></property>
</class>
</hibernate-mapping>接口:
public interface UsersDao {
public Users get(int id);
}
接口实现类:
public class UsersDaoHiberImpl extends HibernateDaoSupport
implements UsersDao{
public Users get(int id)
{
return (Users)super.getHibernateTemplate() .get(Users.class, id);
}
}Hibernate配置文件:
<hibernate-configuration>
<session-factory>
<property name="connection.url">
jdbc:sqlserver://localhost:1433;databasename=zf
</property>
<property name="connection.driver_class">
com.microsoft.sqlserver.jdbc.SQLServerDriver
</property>
<property name="connection.username">zf</property>
<property name="connection.password">zf</property>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">true</property>
<mapping resource="com/ssh/Users.hbm.xml"></mapping>
</session-factory>
</hibernate-configuration>Spring配置文件:
<?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="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>
<bean id="usersDao" class="com.ssh.UsersDaoHiberImpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>Main方法中代码:
UsersDao ud=new UsersDaoHiberImpl();
Users u=(Users)ud.get(1);
System.out.println(u.getName());运行后:
Exception in thread "main" java.lang.NullPointerException
at com.ssh.UsersDaoHiberImpl.get(UsersDaoHiberImpl.java:20)
at test.T.main(T.java:30)
20行指的是:
return (Users)super.getHibernateTemplate().get(Users.class, id);
解决方案 »
- 求解释关于struts2 json插件的param参数
- 类的强制转换
- spring获取对象如何每次都重新实例化
- Java跨数据库事务处理 请问下面的这种写法有没有什么问题
- messages_zh_CN.properties存放中文编码后的参数信息,具体怎么做
- 遇到过么?
- faint!faint!faint!200分求助commons-net实现ftp的下载功能!没做过的进来研究一下吧:)
- hibernate有什么好的?很麻烦,灵活性又差?
- tomcat配置的菜鸟问题!大侠请帮忙,解决就加入。在线等!!
- 有一个dom解析xml的问题一直不明白
- 启动Orcale服务后,为啥Tomcat服务启动出错
- Calendar 类的使用,缺点请高手进来看看
所有getHibernateTemplate()为null,建议在dao配置到spring,通过
spring容器获取dao
不new对象那我要怎么测试Dao层方法是不是对的啊
UserDao userDao = (UserDao) ctx.getBean("dao在spring中配置的名字");
UsersDao ud=new UsersDaoHiberImpl();
这个是你自己new出来的,不受spring控制,无法实现注入
要用楼上的方法来使用
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDao {
Logger logger = Logger.getLogger(this.getClass());
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml"); @Before
public void setUp() throws Exception {
System.out.println("before");
} @After
public void tearDown() throws Exception {
System.out.println("after");
} @Test
public void testUserDao()
{
UserDao udao=(VscheduleServiceImpl) ac.getBean("usersDao");
User u=udao.get(1);
//...
}
}