跳至主要内容

博文

目前显示的是 七月, 2013的博文

Use native JPA API

Use native JPA API Since Spring 3.0, Spring embrace JPA quickly, and JPA is the first class citizen in Spring core framework. And in the latest Spring, JPA support is improved and the usage of JPA is more friendly than Hibernate. Configuration An example of XML format configuration is shown below: <jdbc:embedded-database id="dataSource" > </jdbc:embedded-database> <bean class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" id="entityManagerFactory"> <property name="persistenceUnitName" value="persistenceUnit" /> <property name="dataSource" ref="dataSource" /> <property name="persistenceProviderClass" value="org.hibernate.ejb.HibernatePersistence"></property> <property name="packagesToScan"> <array> <value>com.hantsylabs.example.spring.model</value> ...

Use native Hibernate 4 API

Use native Hibernate 4 API In Spring 3.1, a new package named org.springframework.orm.hibernate4 is included( in spring-orm maven dependency). With this new APIs, using Hibernate 4 in Spring projects becomes more easy than before, you are not required to extend the HiberanteDaoSupport class or use HibernateTemplate in your implemnetation class. Only a simple registration is required in Spring configuration, you can use the Hibernate native Session API freely in your Spring projects. Register a DataSource bean <jdbc:embedded-database id="dataSource" > </jdbc:embedded-database> Declare Spring specific LocalSessionFactoryBean bean. <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="packagesToScan"> <list> <value>com.hantsylabs.e...

Simplified Jdbc operations

Simplified Jdbc operations As I stated in the last post, Spring DaoSupport and Template API is not recommended in new projects. But for Jdbc API, how to simplify the data operations? Spring jdbc support(spring-jdbc maven dependency) provides some simplified APIs for Jdbc operations. Overview Unlike the approach of JdbcDaoSupport, it is no need to subclass the JdbcDaoSupport class. A SQL query(and modifying query) execution will be wrapped in a single object. All you need to do is creating a new class(by subclassing the related operation object) for every SQL query execution. In these operation classes, MappingSqlQuery , SqlUpdate , SimpleJdbcInsert are used frequently. Follow the following steps to use them. Compose sql query statement, and declare parameters used in the sql statement. Call compile() method from super class to prepare the sql statement. Call execute methods to execute the query. MappingSqlQuery MappingSqlQuery is designated for e...

Legacy DaoSupport And Template API

Legacy DaoSupport And Template API In the Spring core framework(provided in the spring-jdbc and spring-orm maven dependency), it has provided a series of standard DaoSupport APIs and Template pattern to help user to retrieve data from database and save data back into database. Jdbc support There is a JdbcDaoSupport class and JdbcTempalte class provided for Jdbc operations. Using these APIs in your project is very simple and stupid. Firstly register a DataSource bean, and also declare a Jdbc transaction manager(we will discuss Spring transaction in future posts). <jdbc:embedded-database id="dataSource" type="H2"> <jdbc:script location="classpath:/com/hantsylabs/example/spring/config/schema.sql" /> </jdbc:embedded-database> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="da...

Spring Data Access overview

This is the first post of my Spring Data Persistence series, all are based on my personal idea about Spring, it is NOT the official guide. If something is described incorrectly, please let me know. Thanks. Since Spring 1.x, in the core framework, Spring provided a generic DaoSupport API to simplify the data operations, and now a new Spring Data project was born which provides a modern way for data operations. DaoSupport and Template API The legacy DaoSupport and Template API tries to simplify data access of DBRMS, it supports Hibernate, Jdbc, iBatis and JDO etc. In the new Spring, JDO support is deprecated and removed, and the third party project MyBatis (the successor of iBatis) hands over the Spring data access support. Spring provides lots of encapsulation for the Jdbc operations, and tries to free developers from the tedious Jdbc APIs. Till now, Jdbc support is still considered valuable when Jdbc is chosen in your project. Besides this, Spring provides a abstraction...