Skip to content

Instantly share code, notes, and snippets.

@juliano
Last active December 11, 2015 06:08
Show Gist options
  • Save juliano/4556731 to your computer and use it in GitHub Desktop.
Save juliano/4556731 to your computer and use it in GitHub Desktop.
SimpleDev - Integrando Spring e Hibernate
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">
<context:component-scan base-package="br.com.simpledev.springhibernate" />
<context:property-placeholder location="classpath:jdbc.properties" />
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"
p:driverClassName="com.mysql.jdbc.Driver" p:url="${jdbc.url}"
p:username="${jdbc.username}" p:password="${jdbc.password}">
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven />
</beans>
apply plugin: 'eclipse'
apply plugin: 'jetty'
apply plugin: 'maven'
version = '0.1-SNAPSHOT'
group = 'br.com.simpledev'
repositories {
mavenCentral()
}
[compileJava, compileTestJava]*.options*.encoding = 'UTF-8'
configurations {
provided
}
sourceSets {
main { compileClasspath += configurations.provided }
test { runtimeClasspath += configurations.provided }
}
eclipse.classpath.plusConfigurations += configurations.provided
dependencies {
compile 'org.springframework:spring-asm:3.1.3.RELEASE'
compile 'org.springframework:spring-beans:3.1.3.RELEASE'
compile 'org.springframework:spring-context:3.1.3.RELEASE'
compile 'org.springframework:spring-core:3.1.3.RELEASE'
compile 'org.springframework:spring-expression:3.1.3.RELEASE'
compile 'org.springframework:spring-orm:3.1.3.RELEASE'
compile 'org.springframework:spring-tx:3.1.3.RELEASE'
compile 'org.springframework:spring-web:3.1.3.RELEASE'
compile 'org.hibernate:hibernate-core:4.1.9.Final'
compile 'org.hibernate:hibernate-entitymanager:4.1.9.Final'
compile 'org.hibernate:hibernate-annotations:3.5.6-Final'
compile 'mysql:mysql-connector-java:5.1.21'
}
[jettyRunWar, jettyRun]*.contextPath = '/'
package br.com.simpledev.springhibernate.dao;
import java.util.List;
import br.com.simpledev.springhibernate.model.Carro;
public interface CarroDao {
List<Carro> lista();
void adiciona(Carro carro);
}
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<mapping class="br.com.simpledev.springhibernate.model.Carro" />
</session-factory>
</hibernate-configuration>
package br.com.simpledev.springhibernate.dao.hibernate;
import java.util.List;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.simpledev.springhibernate.dao.CarroDao;
import br.com.simpledev.springhibernate.model.Carro;
@Repository
public class HibernateCarroDao implements CarroDao {
private final SessionFactory factory;
@Autowired
public HibernateCarroDao(final SessionFactory factory) {
this.factory = factory;
}
@Transactional(readOnly = true)
public List<Carro> lista() {
return factory.getCurrentSession().createCriteria(Carro.class).list();
}
@Transactional
public void adiciona(final Carro carro) {
factory.getCurrentSession().save(carro);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment