我正在使用spring 4.3.4和hibernate 5.1.4创建一个简单的数据库添加,但是sessionfactory对象没有创建并通过NullPointerException。 例外
Exception in thread "main" java.lang.NullPointerException
at com.dao.AdminDao.addAdmin(AdminDao.java:39)
at com.controller.Controller.main(Controller.java:19)
这是代码
bean.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"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.data"></context:component-scan>
<context:component-scan base-package="com.dao"></context:component-scan>
<bean id="data" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/appointmentportal" />
<property name="username" value="root" />
<property name="password" value="" />
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="data"></property>
<property name="packagesToScan" value="com.data"></property>
<property name="hibernateProperties">
<props>
<prop key="dialect" >org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
模型类
@Component
@Entity
@Table(name="tbl_admin")
public class Admin {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="admin_id")
int id;
@Column(name="admin_name")
String name;
@Column(name="admin_email")
String email;
@Column(name="admin_address")
String address;
@Column(name="admin_password")
String password;
@Column(name="admin_age")
int age;
// setters and getter
}
AdminDao类
@Repository
public class AdminDao {
@Autowired
private SessionFactory sessionFactory;
public void addAdmin(Admin admin){
Session session = sessionFactory.getCurrentSession();
session.beginTransaction();
session.save(admin);
session.getTransaction().commit();
}
主要方法类
public class Controller {
public static void main(String[] args) {
Admin admin = new Admin();
admin.setAddress("asda");
admin.setAge(12);
admin.setEmail("fgggg");
admin.setName("eee");
admin.setPassword("123");
AdminDao adminDao = new AdminDao();
adminDao.addAdmin(admin);
}
}
答案 0 :(得分:0)
public class Controller {
@Autowired
AdminDao adminDao ;
仅使用adminDao作为AdminDao adminDao = new AdminDao();
adminDao.addAdmin(admin);
希望这会奏效。