CRUD with Spring MVC & Hibernate

9. Update project configurations

There are several adjustments to make our application working, which consist of:

  • Declaring component scan
  • JDBC setting
  • Declaring Hibernate session factory
  • Hibernate transaction manager
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:beans="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<pre><code>&amp;lt;!-- DispatcherServlet Context: defines this servlet's request-processing 
    infrastructure --&amp;gt;

&amp;lt;!-- Enables the Spring MVC @Controller programming model --&amp;gt;
&amp;lt;annotation-driven /&amp;gt;

&amp;lt;!-- Handles HTTP GET requests for /resources/** by efficiently serving 
    up static resources in the ${webappRoot}/resources directory --&amp;gt;
&amp;lt;resources mapping=&amp;quot;/resources/**&amp;quot; location=&amp;quot;/resources/&amp;quot; /&amp;gt;

&amp;lt;!-- Resolves views selected for rendering by @Controllers to .jsp resources 
    in the /WEB-INF/views directory --&amp;gt;
&amp;lt;beans:bean
    class=&amp;quot;org.springframework.web.servlet.view.InternalResourceViewResolver&amp;quot;&amp;gt;
    &amp;lt;beans:property name=&amp;quot;prefix&amp;quot; value=&amp;quot;/WEB-INF/views/&amp;quot; /&amp;gt;
    &amp;lt;beans:property name=&amp;quot;suffix&amp;quot; value=&amp;quot;.jsp&amp;quot; /&amp;gt;
&amp;lt;/beans:bean&amp;gt;

&amp;lt;!-- Component scan --&amp;gt;
&amp;lt;context:component-scan base-package=&amp;quot;org.munif.bookstore&amp;quot; /&amp;gt;
&amp;lt;context:component-scan base-package=&amp;quot;org.munif.domain&amp;quot; /&amp;gt;
&amp;lt;context:component-scan base-package=&amp;quot;org.munif.repository&amp;quot; /&amp;gt;
&amp;lt;context:component-scan base-package=&amp;quot;org.munif.service&amp;quot; /&amp;gt;
</code></pre>

&lt;/beans:beans&gt;

 

&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;beans:beans xmlns=&quot;http://www.springframework.org/schema/mvc&quot;
    xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:context=&quot;http://www.springframework.org/schema/context&quot;
    xmlns:p=&quot;http://www.springframework.org/schema/p&quot; xmlns:tx=&quot;http://www.springframework.org/schema/tx&quot;
    xmlns:util=&quot;http://www.springframework.org/schema/util&quot; xmlns:aop=&quot;http://www.springframework.org/schema/aop&quot;
    xmlns:task=&quot;http://www.springframework.org/schema/task&quot; xmlns:beans=&quot;http://www.springframework.org/schema/beans&quot;
    xsi:schemaLocation=&quot;http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd&quot;&gt;

<pre><code>&amp;lt;!-- Root Context: defines shared resources visible to all other web components --&amp;gt;

&amp;lt;!-- JDBC setting --&amp;gt;
&amp;lt;beans:bean id=&amp;quot;dataSource&amp;quot; name=&amp;quot;dataSource&amp;quot;
    class=&amp;quot;org.apache.commons.dbcp.BasicDataSource&amp;quot;&amp;gt;
    &amp;lt;beans:property name=&amp;quot;driverClassName&amp;quot; value=&amp;quot;com.mysql.jdbc.Driver&amp;quot; /&amp;gt;
    &amp;lt;beans:property name=&amp;quot;url&amp;quot; value=&amp;quot;jdbc:mysql://localhost:3306/j2ee&amp;quot; /&amp;gt;
    &amp;lt;beans:property name=&amp;quot;username&amp;quot; value=&amp;quot;j2ee&amp;quot; /&amp;gt;
    &amp;lt;beans:property name=&amp;quot;password&amp;quot; value=&amp;quot;j2ee&amp;quot; /&amp;gt;
    &amp;lt;beans:property name=&amp;quot;validationQuery&amp;quot; value=&amp;quot;SELECT 1&amp;quot; /&amp;gt;
&amp;lt;/beans:bean&amp;gt;

&amp;lt;!-- Hibernate session factory --&amp;gt;
&amp;lt;beans:bean id=&amp;quot;sessionFactory&amp;quot; name=&amp;quot;sessionFactory&amp;quot;
    class=&amp;quot;org.springframework.orm.hibernate4.LocalSessionFactoryBean&amp;quot;&amp;gt;
    &amp;lt;beans:property name=&amp;quot;dataSource&amp;quot; ref=&amp;quot;dataSource&amp;quot; /&amp;gt;

    &amp;lt;beans:property name=&amp;quot;hibernateProperties&amp;quot;&amp;gt;
        &amp;lt;beans:props&amp;gt;
            &amp;lt;beans:prop key=&amp;quot;hibernate.show_sql&amp;quot;&amp;gt;true&amp;lt;/beans:prop&amp;gt;
            &amp;lt;beans:prop key=&amp;quot;hibernate.dialect&amp;quot;&amp;gt;org.hibernate.dialect.MySQLDialect
            &amp;lt;/beans:prop&amp;gt;
            &amp;lt;beans:prop key=&amp;quot;hibernate.connection.charSet&amp;quot;&amp;gt;UTF-8&amp;lt;/beans:prop&amp;gt;
        &amp;lt;/beans:props&amp;gt;
    &amp;lt;/beans:property&amp;gt;

    &amp;lt;beans:property name=&amp;quot;packagesToScan&amp;quot;&amp;gt;
        &amp;lt;beans:array&amp;gt;
            &amp;lt;beans:value&amp;gt;org.munif.bookstore&amp;lt;/beans:value&amp;gt;
            &amp;lt;beans:value&amp;gt;org.munif.domain&amp;lt;/beans:value&amp;gt;
            &amp;lt;beans:value&amp;gt;org.munif.repository&amp;lt;/beans:value&amp;gt;
            &amp;lt;beans:value&amp;gt;org.munif.service&amp;lt;/beans:value&amp;gt;
        &amp;lt;/beans:array&amp;gt;
    &amp;lt;/beans:property&amp;gt;
&amp;lt;/beans:bean&amp;gt;

&amp;lt;!-- Hibernate transaction manager --&amp;gt;
&amp;lt;beans:bean id=&amp;quot;transactionManager&amp;quot; name=&amp;quot;transactionManager&amp;quot;
    class=&amp;quot;org.springframework.orm.hibernate4.HibernateTransactionManager&amp;quot;&amp;gt;
    &amp;lt;beans:property name=&amp;quot;sessionFactory&amp;quot; ref=&amp;quot;sessionFactory&amp;quot; /&amp;gt;
&amp;lt;/beans:bean&amp;gt;

&amp;lt;tx:annotation-driven transaction-manager=&amp;quot;transactionManager&amp;quot; /&amp;gt;
</code></pre>

&lt;/beans:beans&gt;
Advertisements

3 thoughts on “CRUD with Spring MVC & Hibernate

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s