belajar integrasi spring mvc hibernate entity manager spring data jpa

Belajar mengintergrasikan spring mvc, hibernate entity manager, spring data jpa menggunakan database mysql

Bismillah

q> Lanjutan dari :
https://saifiahmada.wordpress.com/2014/12/08/contoh-addattribute-spring-mvc/

q> Langkah-langkah yang akan dipraktekan :
1. pada web.xml tambahkan listener
2. buat file applicationContext
3. tambahkan namespace context pada applicationContext
4. tambahkan context:component-scan + context:exclude-filter pada applicationContext
5. tambahkan dependency hibernate entity manager 4.3.4
6. tambahkan dependency spring data jpa  1.5.1-RELEASE
7. tambahkan dependency mysql-connector 5.1.34
8. tambahkan bean dataSource pada applicationContext
9. tambahkan file jdbc.properties
10. tambahkan context:property-placeholder pada applicationContext
11. tambahkan bean class LocalContainerEntityManager pada applicationContext
12. tambahkan namespace tx pada applicationContext
13. tambahkan tx:annotation-driven @transaction-manager= transactionManager pada applicationContext
14. tambahkan bean class JpaTransactionManager id = “transactionManager” pada applicationContext
15. buat class Siswa dalam package com.saifiahmada.maven.entity
16. jalankan project menggunakan jetty
17. cek tabel yang terbentuk setelah project ini dijalankan

q> pada web.xml tambahkan listener

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

q> buat file applicationContext
pada file applicationContext.xml pada folder WEB-INF

q> tambahkan namespace context pada applicationContext
Selection_591

q> tambahkan context:component-scan + context:exclude-filter pada applicationContext

<context:component-scan base-package="com.saifiahmada.maven">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

q> tambahkan dependency hibernate entity manager 4.3.4

<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.3.4.Final</version>
</dependency>

q> tambahkan dependency spring data jpa  1.5.1-RELEASE

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>

q> tambahkan dependency mysql-connector 5.1.34

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>

q> tambahkan dependency c3p0

<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.0.4</version>
</dependency>

q> tambahkan bean dataSource pada applicationContext

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.jdbcUrl}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean>

q> tambahkan file jdbc.properties
buat file jdbc.properties pada folder WEB-INF

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/belajarzhs
jdbc.user=root
jdbc.password=isi_dengan_password_root


untuk nilai url,user, dan password harap sesuikan

q> tambahkan context:property-placeholder pada applicationContext

<context:property-placeholder location="/WEB-INF/jdbc.properties"/>

q> tambahkan bean class LocalContainerEntityManager pada applicationContext

<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.saifiahmada.maven.entity" />
<property name="dataSource" ref="dataSource" />
<property name="jpaProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider" />
</property>
</bean>

q> tambahkan namespace tx pada applicationContext

Selection_590

q> tambahkan tx:annotation-driven transaction-manager= transactionManager pada applicationContext

<tx:annotation-driven transaction-manager="transactionManager"/>

q> tambahkan bean class JpaTransactionManager id = “transactionManager” pada applicationContext

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

q> buat class Siswa dalam package com.saifiahmada.maven.entity


package com.saifiahmada.maven.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity
public class Siswa {

@Id
@GeneratedValue
private int id;

private String nama;

private String alamat;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
this.alamat = alamat;
}

}

q> jalankan project menggunakan jetty
Selection_593
q> cek tabel yang terbentuk setelah project ini dijalankan
Selection_592

Alhamdulillah

 

Published by

saifiahmada

Begin a big Process with a bit trying

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