java - 如何在java桌面应用程序中使用spring(事务)和hibernate创建嵌入式H2 DB?

Posted

技术标签:

【中文标题】java - 如何在java桌面应用程序中使用spring(事务)和hibernate创建嵌入式H2 DB?【英文标题】:How to create emdedded H2 DB with spring(transactional) and hibernate in java desktop application? 【发布时间】:2016-10-26 11:25:15 【问题描述】:

我正在尝试创建一个带有嵌入式 h2 db 的项目,并使用带有 hibernate 的 spring 框架。如果不存在,我的数据库将在初始化时创建。我的开发平台是intellij。

问题是当我运行应用程序时

@Autowired
private IPersonService personService; // comes null?

这是我的类和配置文件。

myDB.sql:

CREATE TABLE IF NOT EXISTS personel(
id IDENTITY AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
age VARCHAR(100));

hibernate.properties:

db.driverClassName=org.h2.Driver
db.url=jdbc:h2:~/h2SpringProject/database/SpringSample;mv_store=false;mvcc=false
db.username=admin
db.password=

这是我的 hibernate-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">



    <context:component-scan base-package="com.springapp"/>
    <context:annotation-config/>
    <context:property-placeholder location="hibernate.properties"/>
    <tx:annotation-driven transaction-manager="transactionManager"/>

    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
        <property name="driverClassName" value="$db.driverClassName"></property>
        <property name="url" value="$db.url"></property>
        <property name="username" value="$db.username"/>
        <property name="password" value="$db.password"/>
        <property name="suppressClose" value="true"/>
    </bean>


    <jdbc:initialize-database data-source="dataSource" ignore-failures="DROPS">
        <jdbc:script location="myDb.sql"/>
    </jdbc:initialize-database>


    <bean id="hibernateCfgProperties"
          class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="properties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
                <prop key="hibernate.hbm2ddl.auto">update</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">false</prop>
                <prop key="hibernate.use_sql_comments">true</prop>
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">true</prop>
                <prop key="hibernate.cache.region.factory_class">
                    org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
                </prop>
            </props>
        </property>
    </bean>

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties" ref="hibernateCfgProperties"/>
        <property name="packagesToScan" value="com.springapp.model"/>
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

</beans>

人物类

@Entity
@Table(name = "personel")
public class Personel 

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private String age;
.......

IPersonDao 接口,这里是实现类

@Component
public class PersonelDaoImpl implements IPersonelDao 

    @Autowired
    private SessionFactory sessionFactory;


    public Session getCurrentSession() 
        return sessionFactory.getCurrentSession();
    

    @Override
    public void savePersonel(Personel personel) 
        getCurrentSession().saveOrUpdate(personel);
    

    @Override
    public void deletePersonel(long id) 
        getCurrentSession().delete(id);
    

    @Override
    public List<Personel> getPersonels() 
        return getCurrentSession().createQuery("from Personel").list();
    

有一个IPersonService接口,这里是实现类

@Service("PersonelService")
public class PersonelServiceImpl implements IPersonelService 

    @Autowired
    private IPersonelDao personelDao;

    @Override
    @Transactional
    public void savePersonel(Personel personel) 
        personelDao.savePersonel(personel);
    

    @Override
    @Transactional
    public void deletePersonel(long id) 
        personelDao.deletePersonel(id);
    

    @Override
    @Transactional
    public List<Personel> getPersonels() 
        return personelDao.getPersonels();
    

这是我的主要课程

public class MainApp 

    private static ApplicationContext applicationContext;

           public static void main(String[] args) 

        applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml");

    ForExample a = new ForExample();
    a.execute();
    



@Component
public class ForExample 

    @Autowired
    private IPersonelService personelService;

    public void execute()
        Personel p = new Personel();
        p.setName("thats Ok!");
        p.setAge("614345");

        personelService.savePersonel(p);
    

【问题讨论】:

class ForExample 不是 spring 托管 bean,所以不会发生 DI 我该如何解决? ForExample 类应该是spring-managed bean,从应用程序上下文中获取。在这种情况下,依赖注入将起作用。你在某处有 ApplicationContext 的实例吗?类似ApplicationContext ctx = new ClasspathApplicationContext("config.xml"); 是的,我有,在主方法 applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml"); (也是静态的) 使用 @Component 注释标记 ForExample 类,确保它落入组件扫描包中并且它应该可以工作。如果您不希望它是 Spring bean,请不要使用 @Autowired 注释并直接从上下文中获取依赖关系 ctx.getBean(IPersonService.class); 【参考方案1】:
public class MainApp 


        public static ApplicationContext applicationContext;

        private static IPersonelService personelService;


        public static void main(String[] args) 

            applicationContext = new ClassPathXmlApplicationContext("hibernate-config.xml");
            personelService = applicationContext.getBean(IPersonelService.class);

            Personel p = new Personel();
            p.setName("thatsOK!");
            p.setAge("614345");

            personelService.savePersonel(p);

        
    

因为 spring 在运行时不识别新的操作符..

【讨论】:

以上是关于java - 如何在java桌面应用程序中使用spring(事务)和hibernate创建嵌入式H2 DB?的主要内容,如果未能解决你的问题,请参考以下文章

如何使用java代码在sp值中分配文本大小

如何在Java桌面应用程序中停止无限循环以移动鼠标

如何开发java桌面程序

如何在 Java 中呈现简单的警报消息?

如何使用java将值动态传递给sp?

如何在 Windows 环境中写入安装在“C:\Program Files”中的 Java 桌面应用程序的嵌入式 derby 数据库?