This repository has been archived by the owner on Apr 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ch07] Use XmlSqlService for getting sql statement by key.
- Loading branch information
Showing
5 changed files
with
67 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
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; | ||
} | ||
} |
This file contains 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
This file contains 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
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> |
2 changes: 1 addition & 1 deletion
2
src/main/resources/sqlmap_test.xml → src/main/resources/sql/sqlmap_test.xml
This file contains 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
This file contains 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