Skip to content
This repository has been archived by the owner on Apr 16, 2024. It is now read-only.

Commit

Permalink
[ch07] Use XmlSqlService for getting sql statement by key.
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Oct 11, 2017
1 parent bc9e655 commit f7ab453
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
44 changes: 44 additions & 0 deletions src/main/java/ch07/springbook/sql/XmlSqlService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package ch07.springbook.sql;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import ch01.springbook.user.dao.UserDao;

public class XmlSqlService implements SqlService {
private Map<String, String> sqlMap = new HashMap<>();

public XmlSqlService() {
String contextPath = Sqlmap.class.getPackage().getName();

try {
JAXBContext context = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = context.createUnmarshaller();
InputStream inputStream = UserDao.class.getResourceAsStream("/sql/sqlmap.xml");
Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(inputStream);

for (SqlType sql : sqlmap.getSql()) {
sqlMap.put(sql.getKey(), sql.getValue());
}

} catch (JAXBException e) {
throw new RuntimeException(e);
}
}

@Override
public String getSql(String key) throws SqlRetrievalFailureException {
String sql = sqlMap.get(key);

if (sql == null) {
throw new SqlRetrievalFailureException("Can not find appropriate sql statement, key: " + key);
}

return sql;
}
}
12 changes: 8 additions & 4 deletions src/main/resources/applicationContext.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<tx:annotation-driven />

<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost/tobystudy?useUnicode=true&amp;characterEncoding=UTF-8&amp;useSSL=true&amp;verifyServerCertificate=false" />
Expand All @@ -37,6 +38,9 @@

<bean id="testUserService" class="ch01.springbook.user.UserServiceTest$TestUserService" parent="userService" />

<bean id="sqlService" class="ch07.springbook.sql.XmlSqlService" >
</bean>
<!--
<bean id="sqlService" class="ch07.springbook.sql.SimpleSqlService">
<property name="sqlMap">
<map>
Expand All @@ -49,8 +53,8 @@
</map>
</property>
</bean>
-->

<tx:annotation-driven />
<!--
<bean id="transactionAdvice" class="ch01.springbook.user.TransactionAdvice">
<property name="transactionManager" ref="transactionManager"/>
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/sql/sqlmap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<sqlmap xmlns="http://www.epril.com/sqlmap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.epril.com/sqlmap ../../../../sqlmap.xsd ">

<sql key="userAdd">INSERT INTO users(id, name, password, level, login, recommend, email) VALUES(?, ?, ?, ?, ?, ?, ?)</sql>
<sql key="userGet">SELECT * FROM users WHERE id = ?</sql>
<sql key="userGetAll">SELECT * FROM users</sql>
<sql key="userDeleteAll">DELETE FROM users</sql>
<sql key="userGetCount">SELECT COUNT(*) FROM users</sql>
<sql key="userUpdate">UPDATE users SET name = ?, password = ?, level = ?, login = ?, recommend = ?, email = ? WHERE id = ?</sql>

</sqlmap>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" ?>
<sqlmap xmlns="http://www.epril.com/sqlmap"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.epril.com/sqlmap ../../../sqlmap.xsd ">
xsi:schemaLocation="http://www.epril.com/sqlmap ../../../../sqlmap.xsd ">

<sql key="add">insert</sql>
<sql key="get">select</sql>
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/ch07/springbook/JaxbTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void readSqlMap() throws JAXBException, IOException {
JAXBContext context = JAXBContext.newInstance(contextPath);
Unmarshaller unmarshaller = context.createUnmarshaller();

Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(getClass().getResourceAsStream("/sqlmap_test.xml"));
Sqlmap sqlmap = (Sqlmap)unmarshaller.unmarshal(getClass().getResourceAsStream("/sql/sqlmap_test.xml"));
List<SqlType> sqlList = sqlmap.getSql();

assertThat(sqlList.size(), is(3));
Expand Down

0 comments on commit f7ab453

Please sign in to comment.