Created
October 19, 2012 20:13
-
-
Save saltnlight5/3920419 to your computer and use it in GitHub Desktop.
Spring Demos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hibernate.dialect=org.hibernate.dialect.H2Dialect | |
hibernate.show_sql=true | |
hibernate.hbm2ddl.auto=create |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:lang="http://www.springframework.org/schema/lang" | |
xsi:schemaLocation= | |
"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd | |
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd"> | |
<!--<bean id="myBean" class="MyBean"> | |
<property name="prop" value="testValue"/> | |
</bean>--> | |
<!--<lang:groovy id="messenger" script-source="classpath:Messenger.groovy"> | |
<lang:property name="message" value="I Can Do The RoboCop" /> | |
</lang:groovy>--> | |
<lang:groovy id="testBean"> | |
<lang:inline-script> | |
binding.variables.each {k,v -> println "$k = $v"} | |
</lang:inline-script> | |
</lang:groovy> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.data; | |
import java.util.Date; | |
import javax.persistence.Entity; | |
import javax.persistence.Id; | |
@Entity | |
public class User { | |
@Id | |
private String id; | |
private String password; | |
private String email; | |
private String displayName; | |
private Date createdOn; | |
private Boolean active; | |
public String getId() { | |
return id; | |
} | |
public void setId(String id) { | |
this.id = id; | |
} | |
public String getPassword() { | |
return password; | |
} | |
public void setPassword(String password) { | |
this.password = password; | |
} | |
public String getEmail() { | |
return email; | |
} | |
public void setEmail(String email) { | |
this.email = email; | |
} | |
public String getDisplayName() { | |
return displayName; | |
} | |
public void setDisplayName(String displayName) { | |
this.displayName = displayName; | |
} | |
public Date getCreatedOn() { | |
return createdOn; | |
} | |
public void setCreatedOn(Date createdOn) { | |
this.createdOn = createdOn; | |
} | |
public Boolean getActive() { | |
return active; | |
} | |
public void setActive(Boolean active) { | |
this.active = active; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.data.store; | |
import it.data.User; | |
import org.hibernate.Session; | |
import org.hibernate.SessionFactory; | |
import org.hibernate.exception.DataException; | |
import org.springframework.stereotype.Repository; | |
import org.springframework.transaction.annotation.Transactional; | |
@Repository | |
public class UserStore { | |
private SessionFactory sessionFactory; | |
public void setSessionFactory(SessionFactory sessionFactory) { | |
this.sessionFactory = sessionFactory; | |
} | |
@Transactional | |
public void create(User user) { | |
Session session = sessionFactory.getCurrentSession(); | |
session.save(user); | |
} | |
@Transactional | |
public User getUser(String userId) { | |
Session session = sessionFactory.getCurrentSession(); | |
return (User)session.get(User.class, userId); | |
} | |
@Transactional | |
public void update(User user) { | |
Session session = sessionFactory.getCurrentSession(); | |
session.update(user); | |
} | |
@Transactional | |
public void delete(User user) { | |
Session session = sessionFactory.getCurrentSession(); | |
session.delete(user); | |
} | |
@Transactional | |
public User authenticate(String userId, String password) throws DataException { | |
Session session = sessionFactory.getCurrentSession(); | |
User result = null; | |
try { | |
result = (User)session | |
.createQuery("from User e e.id = :id") | |
.setParameter("id", userId).uniqueResult(); | |
} catch (RuntimeException e) { | |
throw new RuntimeException("User id=" + userId + " not found.", e); | |
} | |
if (!result.getPassword().equals(password)) | |
throw new RuntimeException("User id=" + userId + "'s password not match."); | |
return result; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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:util="http://www.springframework.org/schema/util" | |
xsi:schemaLocation=" | |
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd | |
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd | |
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd | |
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> | |
<context:component-scan base-package="it.data.store"/> | |
<bean id="dataSource" | |
class="org.springframework.jdbc.datasource.DriverManagerDataSource" | |
p:driverClassName="org.h2.Driver" | |
p:url="jdbc:h2:~/test/issuetracker;MVCC=TRUE" | |
p:username="sa" | |
p:password="" /> | |
<bean id="sessionFactory" | |
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" | |
p:dataSource-ref="dataSource"> | |
<property name="annotatedClasses"> | |
<util:list> | |
<value>it.data.User</value> | |
</util:list> | |
</property> | |
<property name="hibernateProperties"> | |
<util:properties location="classpath:it/it/data/store/hibernate.properties" /> | |
</property> | |
</bean> | |
<bean id="transactionManager" | |
class="org.springframework.orm.hibernate4.HibernateTransactionManager" | |
p:sessionFactory-ref="sessionFactory" /> | |
<tx:annotation-driven transaction-manager="transactionManager" /> | |
<bean id="userStore" | |
class="it.data.store.UserStore" | |
p:sessionFactory-ref="sessionFactory" /> | |
</beans> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package it.it.data.store; | |
import it.data.User; | |
import it.data.store.UserStore; | |
import java.util.Date; | |
import org.hamcrest.Matchers; | |
import org.junit.Assert; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.test.context.ContextConfiguration; | |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; | |
@RunWith(SpringJUnit4ClassRunner.class) | |
@ContextConfiguration | |
public class UserStoreIT { | |
@Autowired | |
private UserStore userStore; | |
@Test | |
public void testCreate() { | |
User user = new User(); | |
user.setActive(true); | |
user.setCreatedOn(new Date()); | |
user.setDisplayName("Zemian Deng"); | |
user.setEmail("[email protected]"); | |
user.setId("zdeng"); | |
user.setPassword("{md5}d8e8fca2dc0f896fd7cb4cb0031ba249"); | |
userStore.create(user); | |
user = userStore.getUser("zdeng"); | |
Assert.assertThat(user.getDisplayName(), Matchers.equalTo("Zemian Deng")); | |
Assert.assertThat(user.getEmail(), Matchers.equalTo("[email protected]")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment