Start by creating the following packages: blog.dao and blog.service.
The next thing we will be doing is adding the Spring configuration file: add applicationContext.xml file to your WEB-INF folder.
The basic setup of this file should look like this:
This configures the namespaces we will be needing in our configuration. Next we will have a look to what we will need specifically.
The first element we'll be adding is the dataSource bean, this bean will contain the location and configuration of our mysql database. root/root isn't very ideal but since it's just an example... :) you get the idea.
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"> <property name="url" value="jdbc:mysql://localhost:3306/blog"> <property name="username" value="root"> <property name="password" value="root"> </bean>
So we now configured a database, a driver to acces it, the location of our database and a username/password. This is all we will need for this tutorial, ofcourse further customization is possible.
Next bean to cover is a really cool feature of Spring: Component scanning. Component scanning will allow Spring to find your classes without having to explicitly declare them in xml, we will be using annotations instead (like we have seen @Entity in our domain).
<context:component-scan base-package="blog.service blog.dao">
To make it even better this can be done in just one line of XML!
Now let's setup a transaction manager that will take care of the transaction management for us
<bean class="org.springframework.orm.jpa.JpaTransactionManager" name="transactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory"> </bean> <tx:annotation-driven transaction-manager="transactionManager"/>
Note: using tx:annotation driven will let Spring autodetect @Transactional annotations in our code, more info on that later.
One last thing to do now, the hearth of JPA: the entity manager configuration!
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:packagesToScan="blog.domain" p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="hibernateVendor" p:jpaPropertyMap-ref="jpaPropertyMap" /> <util:map id="jpaPropertyMap"> <entry key="hibernate.hbm2ddl.auto" value="update"/> <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/> </util:map> <bean id="hibernateVendor" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" p:showSql="true" p:generateDdl="true"/>
Quite some things to talk about here:
- packagesToScan: scans for your entities in the blog.domain package
- dataSource-ref links to your datasource
- jpaVendorAdapter & jpaPropertyMap define properties to autogenerate tables and columns and what dialect to use, etc.
note: usage of p namespace.
So far for the applicationContext. See you guys at the next Tutorial.
More about this tutorial series can be found here:
Set up a maven web project in Intellij with Spring and JPA - Part One (Project setup)
Set up a maven web project in Intellij with Spring and JPA - Part Two (Maven setup)
Set up a maven web project in Intellij with Spring and JPA - Part Three (ORM mapping)
Set up a maven web project in Intellij with Spring and JPA - Part Five (DAO)
Set up a maven web project in Intellij with Spring and JPA - Part Six (Configuring Spring MVC)
Set up a maven web project in Intellij with Spring and JPA - Part Seven (Implementing Spring MVC)
More about this tutorial series can be found here:
Set up a maven web project in Intellij with Spring and JPA - Part One (Project setup)
Set up a maven web project in Intellij with Spring and JPA - Part Two (Maven setup)
Set up a maven web project in Intellij with Spring and JPA - Part Three (ORM mapping)
Set up a maven web project in Intellij with Spring and JPA - Part Five (DAO)
Set up a maven web project in Intellij with Spring and JPA - Part Six (Configuring Spring MVC)
Set up a maven web project in Intellij with Spring and JPA - Part Seven (Implementing Spring MVC)