From ed9eb09ffdf1fae9aba34f6d76b8d8283e5f2f75 Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 18:58:18 -0700 Subject: [PATCH 1/8] Updated to use jndi --- .../classes/spring/environment.properties | 4 ---- sample_conf/jetty-env.xml | 16 ++++++++++++++++ src/main/resources/spring/beans-service.xml | 15 +++++---------- 3 files changed, 21 insertions(+), 14 deletions(-) delete mode 100644 sample_conf/classes/spring/environment.properties create mode 100644 sample_conf/jetty-env.xml diff --git a/sample_conf/classes/spring/environment.properties b/sample_conf/classes/spring/environment.properties deleted file mode 100644 index e745ea7..0000000 --- a/sample_conf/classes/spring/environment.properties +++ /dev/null @@ -1,4 +0,0 @@ -dataSource.driverClassName=com.mysql.jdbc.Driver -dataSource.url=jdbc:mysql://localhost:3306/sip02?autoReconnect=true -dataSource.username=sip -dataSource.password=sip diff --git a/sample_conf/jetty-env.xml b/sample_conf/jetty-env.xml new file mode 100644 index 0000000..12bce5d --- /dev/null +++ b/sample_conf/jetty-env.xml @@ -0,0 +1,16 @@ + + + + + + jdbc/Sip02DS + + + com.mysql.jdbc.Driver + jdbc:mysql://localhost:3306/sip02?autoReconnect=true + sip + sip + + + + diff --git a/src/main/resources/spring/beans-service.xml b/src/main/resources/spring/beans-service.xml index fc83d49..eb1262f 100644 --- a/src/main/resources/spring/beans-service.xml +++ b/src/main/resources/spring/beans-service.xml @@ -2,23 +2,18 @@ + http://www.springframework.org/schema/context/spring-context-3.1.xsd + http://www.springframework.org/schema/jee + http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> - - - + From 7f387fdc78da2cec79a0b0fb841b764adfe38527 Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 19:18:48 -0700 Subject: [PATCH 2/8] added jetty config --- pom.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/pom.xml b/pom.xml index 0e02be2..d975487 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,9 @@ org.mortbay.jetty maven-jetty-plugin + + ${chapter.conf.dir}/jetty-env.xml + From 9443e08d07a54e99fa59069b4471e35b68173a2d Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 19:34:42 -0700 Subject: [PATCH 3/8] updated descriptions --- pom.xml | 6 +++--- src/main/webapp/WEB-INF/web.xml | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index b9c2074..2091aea 100644 --- a/pom.xml +++ b/pom.xml @@ -11,11 +11,11 @@ 1.0-RELEASE - com.springinpractice.02_01 + com.springinpractice.02_02 war - Spring in Practice Section 2.1 - Shows how to use Spring's JdbcTemplate for data access. + Spring in Practice Section 2.2 + Shows how to look up a data source using JNDI. http://springinpractice.com/ diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index a1adf72..5159f48 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -11,12 +11,12 @@ recipe - 2.1 + 2.2 aboutThisRecipe Shows how to use Spring's JdbcTemplate for data access.

+

Shows how to look up a data source using JNDI.

]]>
From ec201a4817eea89f5ea4690ec2b82cc3ff282f81 Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 20:32:14 -0700 Subject: [PATCH 4/8] Adding ORM --- pom.xml | 13 +++- .../springinpractice/ch02/model/Contact.java | 22 ++++++ .../ch02/service/impl/ContactRowMapper.java | 28 -------- .../ch02/service/impl/ContactServiceImpl.java | 68 ++++++------------- src/main/resources/spring/beans-service.xml | 26 ++++++- src/main/webapp/WEB-INF/web.xml | 2 +- 6 files changed, 78 insertions(+), 81 deletions(-) delete mode 100644 src/main/java/com/springinpractice/ch02/service/impl/ContactRowMapper.java diff --git a/pom.xml b/pom.xml index 2091aea..56ec6bd 100644 --- a/pom.xml +++ b/pom.xml @@ -11,11 +11,11 @@ 1.0-RELEASE - com.springinpractice.02_02 + com.springinpractice.02_03 war - Spring in Practice Section 2.2 - Shows how to look up a data source using JNDI. + Spring in Practice Section 2.3 + Shows how to use object/relational mapping for database access. http://springinpractice.com/ @@ -29,6 +29,13 @@ com.springinpractice.api 1.0-RELEASE + + com.springinpractice + com.springinpractice.deps-hibernate + 1.0-RELEASE + pom + true + ${project.groupId} com.springinpractice.deps-web diff --git a/src/main/java/com/springinpractice/ch02/model/Contact.java b/src/main/java/com/springinpractice/ch02/model/Contact.java index 0a5226e..6ed894b 100644 --- a/src/main/java/com/springinpractice/ch02/model/Contact.java +++ b/src/main/java/com/springinpractice/ch02/model/Contact.java @@ -1,10 +1,24 @@ package com.springinpractice.ch02.model; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.Transient; + import com.springinpractice.util.StringUtils; /** * @author Willie Wheeler (willie.wheeler@gmail.com) */ +@Entity +@Table(name = "contact") +@NamedQuery( + name = "findContactsByEmail", + query = "from Contact where email = :email") public class Contact { private Long id; private String lastName; @@ -12,34 +26,42 @@ public class Contact { private String middleInitial; private String email; + @Id + @Column + @GeneratedValue(strategy = GenerationType.AUTO) public Long getId() { return id; } public void setId(Long id) { this.id = id; } + @Column(name = "last_name") public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = StringUtils.cleanup(lastName); } + @Column(name = "first_name") public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = StringUtils.cleanup(firstName); } + @Column(name = "mi") public String getMiddleInitial() { return middleInitial; } public void setMiddleInitial(String mi) { this.middleInitial = StringUtils.cleanup(mi); } + @Column public String getEmail() { return email; } public void setEmail(String email) { this.email = StringUtils.cleanup(email); } + @Transient public String getFullName() { String fullName = lastName + ", " + firstName; if (! (middleInitial == null || "".equals(middleInitial.trim()))) { diff --git a/src/main/java/com/springinpractice/ch02/service/impl/ContactRowMapper.java b/src/main/java/com/springinpractice/ch02/service/impl/ContactRowMapper.java deleted file mode 100644 index da1d981..0000000 --- a/src/main/java/com/springinpractice/ch02/service/impl/ContactRowMapper.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.springinpractice.ch02.service.impl; - -import java.sql.ResultSet; -import java.sql.SQLException; - -import org.springframework.jdbc.core.RowMapper; -import org.springframework.stereotype.Component; - -import com.springinpractice.ch02.model.Contact; - -/** - * @version $Id$ - * @author Willie Wheeler (willie.wheeler@gmail.com) - */ -@Component -public class ContactRowMapper implements RowMapper { - - @Override - public Contact mapRow(ResultSet resultSet, int rowNum) throws SQLException { - Contact contact = new Contact(); - contact.setId(resultSet.getLong(1)); - contact.setLastName(resultSet.getString(2)); - contact.setFirstName(resultSet.getString(3)); - contact.setMiddleInitial(resultSet.getString(4)); - contact.setEmail(resultSet.getString(5)); - return contact; - } -} diff --git a/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java b/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java index 92dc9a5..ce9acfb 100644 --- a/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java +++ b/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java @@ -2,17 +2,14 @@ import static org.springframework.util.Assert.notNull; -import java.util.HashMap; import java.util.List; import javax.inject.Inject; -import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; -import org.springframework.jdbc.core.namedparam.NamedParameterJdbcOperations; -import org.springframework.jdbc.core.namedparam.SqlParameterSource; -import org.springframework.jdbc.support.GeneratedKeyHolder; -import org.springframework.jdbc.support.KeyHolder; +import org.hibernate.Session; +import org.hibernate.SessionFactory; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.springinpractice.ch02.model.Contact; import com.springinpractice.ch02.service.ContactService; @@ -24,74 +21,53 @@ * @author Willie Wheeler (willie.wheeler@gmail.com) */ @Service +@Transactional public class ContactServiceImpl implements ContactService { - private static final String CREATE_SQL = - "insert into contact (last_name, first_name, mi, email) values (:lastName, :firstName, :mi, :email)"; - private static final String FIND_ALL_SQL = - "select id, last_name, first_name, mi, email from contact"; - private static final String FIND_ALL_BY_EMAIL_LIKE_SQL = - "select id, last_name, first_name, mi, email from contact where email like :email"; - private static final String FIND_ONE_SQL = - "select id, last_name, first_name, mi, email from contact where id = :id"; - private static final String UPDATE_SQL = - "update contact set last_name = :lastName, first_name = :firstName, mi = :mi, email = :email " + - "where id = :id"; - private static final String DELETE_SQL = - "delete from contact where id = :id"; - - @Inject private NamedParameterJdbcOperations jdbcTemplate; - @Inject private ContactRowMapper contactRowMapper; + @Inject private SessionFactory sessionFactory; @Override public void createContact(Contact contact) { notNull(contact, "contact can't be null"); - SqlParameterSource params = new MapSqlParameterSource() - .addValue("lastName", contact.getLastName()) - .addValue("firstName", contact.getFirstName()) - .addValue("mi", contact.getMiddleInitial()) - .addValue("email", contact.getEmail()); - KeyHolder keyHolder = new GeneratedKeyHolder(); - jdbcTemplate.update(CREATE_SQL, params, keyHolder); - contact.setId(keyHolder.getKey().longValue()); + getSession().save(contact); } @Override + @SuppressWarnings("unchecked") public List getContacts() { - return jdbcTemplate.query(FIND_ALL_SQL, new HashMap(), contactRowMapper); + return getSession() + .createQuery("from Contact") + .list(); } @Override + @SuppressWarnings("unchecked") public List getContactsByEmail(String email) { notNull(email, "email can't be null"); - SqlParameterSource params = new MapSqlParameterSource("email", "%" + email + "%"); - return jdbcTemplate.query(FIND_ALL_BY_EMAIL_LIKE_SQL, params, contactRowMapper); + return getSession() + .getNamedQuery("findContactsByEmail") + .setString("email", email) + .list(); } @Override public Contact getContact(Long id) { notNull(id, "id can't be null"); - SqlParameterSource params = new MapSqlParameterSource() - .addValue("id", id); - return jdbcTemplate.queryForObject(FIND_ONE_SQL, params, contactRowMapper); + return (Contact) getSession().get(Contact.class, id); } @Override public void updateContact(Contact contact) { notNull(contact, "contact can't be null"); - SqlParameterSource params = new MapSqlParameterSource() - .addValue("id", contact.getId()) - .addValue("lastName", contact.getLastName()) - .addValue("firstName", contact.getFirstName()) - .addValue("mi", contact.getMiddleInitial()) - .addValue("email", contact.getEmail()); - jdbcTemplate.update(UPDATE_SQL, params); + getSession().update(contact); } @Override public void deleteContact(Long id) { notNull(id, "id can't be null"); - SqlParameterSource params = new MapSqlParameterSource() - .addValue("id", id); - jdbcTemplate.update(DELETE_SQL, params); + getSession().delete(getContact(id)); + } + + private Session getSession() { + return sessionFactory.getCurrentSession(); } } diff --git a/src/main/resources/spring/beans-service.xml b/src/main/resources/spring/beans-service.xml index eb1262f..de291ae 100644 --- a/src/main/resources/spring/beans-service.xml +++ b/src/main/resources/spring/beans-service.xml @@ -4,6 +4,8 @@ xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" 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:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans @@ -11,12 +13,30 @@ http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jee - http://www.springframework.org/schema/jee/spring-jee-3.1.xsd"> + http://www.springframework.org/schema/jee/spring-jee-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/util + http://www.springframework.org/schema/util/spring-util-3.1.xsd"> - + + org.hibernate.dialect.MySQL5InnoDBDialect + false + + + + + + +
diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 5159f48..2c730b7 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -16,7 +16,7 @@ aboutThisRecipe Shows how to look up a data source using JNDI.

+

Shows how to use object/relational mapping for database access.

]]>
From e75507bb6a55fd02c612218756d24d24e6e7ce8d Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 21:07:08 -0700 Subject: [PATCH 5/8] Fixed number --- src/main/webapp/WEB-INF/web.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml index 2c730b7..c99c5b9 100644 --- a/src/main/webapp/WEB-INF/web.xml +++ b/src/main/webapp/WEB-INF/web.xml @@ -11,7 +11,7 @@ recipe - 2.2 + 2.3 aboutThisRecipe From 492feb19f2ecb7051c0dbbbf72acb86a718fee40 Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 22:39:22 -0700 Subject: [PATCH 6/8] Fixed search bug --- src/main/java/com/springinpractice/ch02/model/Contact.java | 2 +- .../springinpractice/ch02/service/impl/ContactServiceImpl.java | 2 +- src/main/resources/spring/beans-service.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/springinpractice/ch02/model/Contact.java b/src/main/java/com/springinpractice/ch02/model/Contact.java index 6ed894b..a4ed7ee 100644 --- a/src/main/java/com/springinpractice/ch02/model/Contact.java +++ b/src/main/java/com/springinpractice/ch02/model/Contact.java @@ -18,7 +18,7 @@ @Table(name = "contact") @NamedQuery( name = "findContactsByEmail", - query = "from Contact where email = :email") + query = "from Contact where email like :email") public class Contact { private Long id; private String lastName; diff --git a/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java b/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java index ce9acfb..321927d 100644 --- a/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java +++ b/src/main/java/com/springinpractice/ch02/service/impl/ContactServiceImpl.java @@ -45,7 +45,7 @@ public List getContactsByEmail(String email) { notNull(email, "email can't be null"); return getSession() .getNamedQuery("findContactsByEmail") - .setString("email", email) + .setString("email", "%" + email + "%") .list(); } diff --git a/src/main/resources/spring/beans-service.xml b/src/main/resources/spring/beans-service.xml index de291ae..936d4d8 100644 --- a/src/main/resources/spring/beans-service.xml +++ b/src/main/resources/spring/beans-service.xml @@ -38,5 +38,5 @@ - + From bd9d34fb8a5878c016c3d34adab69fca2acaa85f Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Sun, 5 Aug 2012 22:48:46 -0700 Subject: [PATCH 7/8] removed unneeded dependency --- pom.xml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/pom.xml b/pom.xml index 56ec6bd..e05f9bc 100644 --- a/pom.xml +++ b/pom.xml @@ -50,10 +50,6 @@ - - org.springframework - org.springframework.jdbc - com.mysql.jdbc com.springsource.com.mysql.jdbc From 76fa3156cb5c9b114122a880d4d57e7c8cbb54b3 Mon Sep 17 00:00:00 2001 From: Willie Wheeler Date: Wed, 1 May 2013 01:39:36 -0700 Subject: [PATCH 8/8] Updated README --- README | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README b/README index 9af179f..13c055c 100644 --- a/README +++ b/README @@ -1,5 +1,5 @@ ======================================================================================================================== -README - Spring in Practice Recipe 2.1 +README - Spring in Practice Recipe 2.2 Copyright (c) 2013 Manning Publications Co. @@ -10,4 +10,4 @@ Blog: http://springinpractice.com/ Code: https://github.com/springinpractice ======================================================================================================================== -Shows how to use Spring's JdbcTemplate for data access. +Shows how to look up a data source using JNDI.