spring 2.5.6 and hibernate3.2.2 Integration (3)

Recently looked at springside, learn a lot, from the ideological to the coding style, here re-nonsense look, thought this thing quite true, but not only realize unspeakable, a number of things you want to understand, and be able to explain to others, and let others came to understand, That on behalf of your thoughts to a certain state, of course, this is my understanding of this phase, the so-called constant is change things, ideas, too, pulled far away.

The continuation of the previous demo can be considered on a project I have a personal improvement. Here is a simple improvement, using the first demo of the comment, to taste annotations in reducing the role of configuration.

1. Databases and tables still in use demo2

2. Demo2 the same entity class, and change the look package name.

3. Interface class remain unchanged.

4. Implementation class using the annotation

package com.isa.demo3.service;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.isa.demo3.domain.User;

@Service
// The default is all the functions in the class included in the transaction management  .
@Transactional
public class UserServiceImpl implements UserService {

        @Autowired
        private SessionFactory sessionFactory;
        
        @Override
        @Transactional(readOnly = true)
        public List<User> getAllUsers() {
                Session session = sessionFactory.getCurrentSession();
                String hql = "from User";
                List<User> list = session.createQuery(hql).list();
                return list;
        }

        @Override
        @Transactional(readOnly = true)
        public User getUserById(long id) {
                Session session = sessionFactory.getCurrentSession();
                User user = (User) session.get(User.class, id);
                return user;
        }

}

5. Relatively large changes in the configuration file, the first one to save the database associated with the application.properties configuration, where reference springside

#h2 version database settings
#jdbc.driver=org.h2.Driver
#jdbc.url=jdbc:h2:tcp://localhost/~/mini-web
#jdbc.username=sa
#jdbc.password=
#hibernate.dialect=org.hibernate.dialect.H2Dialect

#oracle version database settings
#jdbc.driver=oracle.jdbc.driver.OracleDriver
#jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:XE
#jdbc.username=miniweb
#jdbc.password=miniweb
#hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

#mysql version database setting
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost/test?useUnicode=true&amp;characterEncoding=utf-8
jdbc.username=root
jdbc.password=123456
hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect

#hibernate settings
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.ehcache_config_file=/ehcache/ehcache-hibernate-local.xml

Second applicationContext.xml as follows

<?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:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
        default-lazy-init="true">

        <description>Spring The common configuration  </description>

        <!--  Define environmentally volatile variables  -->
        <bean>
                <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
                <property name="ignoreResourceNotFound" value="true" />
                <property name="locations">
                        <list>
                                <!--  The standard configuration  -->
                                <value>classpath*:demo3/application.properties</value>
                                <!--  The cluster node configuration  -->
                                <value>classpath*:demo3/application.cluster.properties</value>
                                <!--  The local development environment configuration  -->
                                <value>classpath*:demo3/application.local.properties</value>
                                <!--  Server production environment configuration  -->
                                <!-- <value>file:/var/myapp/application.server.properties</value> -->
                        </list>
                </property>
        </bean>

        <!--  Use the annotation to automatically register the bean  , And make sure that the  @Required,@Autowired The property was injected  -->
        <context:component-scan base-package="com.isa.demo3" />

        <!--  The data source configuration  , Use the apply DBCP within the database connection pool  -->
        <bean destroy-method="close">
                <!-- Connection Info -->
                <property name="driverClassName" value="${jdbc.driver}" />
                <property name="url" value="${jdbc.url}" />
                <property name="username" value="${jdbc.username}" />
                <property name="password" value="${jdbc.password}" />

                <!-- Connection Pooling Info -->
                <property name="initialSize" value="5" />
                <property name="maxActive" value="100" />
                <property name="maxIdle" value="30" />
                <property name="maxWait" value="1000" />
                <property name="poolPreparedStatements" value="false" />
                <property name="defaultAutoCommit" value="false" />
        </bean>

        <!--  The data source configuration  , Use the application server's database connection pool  -->
        <!--<jee:jndi-lookup jndi-name="java:comp/env/jdbc/ExampleDB" />-->

        <!-- Hibernate Configuration  -->
        <bean>
                <property name="dataSource" ref="dataSource" />
                <property name="namingStrategy">
                        <bean />
                </property>
                <property name="hibernateProperties">
                        <props>
                                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
                                <prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
                                <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider
                                </prop>
                                <prop key="hibernate.cache.provider_configuration_file_resource_path">${hibernate.ehcache_config_file}</prop>
                        </props>
                </property>
                <property name="packagesToScan" value="com.isa.demo3.domain" />
        </bean>

        <!--  The transaction manager configuration  , A single data source transaction  -->
        <bean>
                <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <!--  To define a transaction using the annotation  -->
        <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true" />
</beans>

6. Simple tests

package com.isa.demo3.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.isa.demo3.service.UserService;
import com.isa.demo3.service.UserServiceImpl;

public class Demo3Test {

        /**
         * @param args
         */
        public static void main(String[] args) {
                ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "demo3/*.xml" });
                UserService service = (UserServiceImpl)context.getBean("userServiceImpl");
                System.out.println(service.getUserById(1L).toString());
        }
}

7. Corresponds to the configuration file should be noted, sessionFactory nodes packagesToScan of writing, to the packet domain can be followed by no increase asterisk.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of spring 2.5.6 and hibernate3.2.2 Integration (3)

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and oth ...

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

Leave a Reply

Recent
Recent Entries
Tag Cloud
Random Entries