<project ……> …… <dependencies> …… <dependency> <!-- For [Spring 3] JDBC Template. --> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>3.2.8.RELEASE</version> </dependency> </dependencies> </project>
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
<bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"/>
</bean>
</beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <import resource="sub_files/02_basic_jdbc_crud.xml"/> <import resource="sub_files/03_jdbc_template_crud.xml"/> </beans>
……
/**
* Either "@Component(...)" or "@Repository(...)" is OK!<br />
*
* ……
*/
@Component("crud.spring_jdbc_template.userDAO")
// @Repository("crud.spring_jdbc_template.userDAO")
public class UserDAO implements I_UserDAO
{
@Resource(name = "jt")
private JdbcTemplate jt;
private RowMapper<User> rowMapper = new RowMapper<User>()
{
……
public User mapRow(ResultSet rs, int rowNum) throws SQLException
{
User user = new User();
Object obj = rs.getObject("id");
user.setId(NumberToolE.o2I(obj, null));
obj = rs.getObject("name");
user.setName(TextToolE.o2s(obj, null));
obj = rs.getObject("passwd");
user.setPasswd(TextToolE.o2s(obj, null));
obj = rs.getObject("stat");
user.setStat((null != obj && obj instanceof Number)
? ((Number) obj).byteValue() : null);
obj = rs.getObject("remark");
user.setRemark(TextToolE.o2s(obj, null));
return user;
}
};
……
public int create(User user) throws SQLException
{
……
try
{
KeyHolder keyHolder = new GeneratedKeyHolder();
final String strSql =
"insert into tfw_dash_user(id, name, passwd, stat, remark) values(tfw_seq_dash_user_id.nextval, ?, ?, ?, ?)";
System.out.println(strSql);
final Object[] o1dValues = new Object[] { user.getName(),
user.getPasswd(), user.getStatus(), user.getRemark() };
System.out.println(ArrayToolE.arrayForConsole(o1dValues));
PreparedStatementCreator psc = new PreparedStatementCreator()
{
……
public PreparedStatement createPreparedStatement(
Connection cnct) throws SQLException
{
PreparedStatement pstmt = cnct.prepareStatement(strSql,
new String[] { "id" });
for (int intIdx = 0; intIdx < o1dValues.length; intIdx++)
{
pstmt.setObject(intIdx + 1, o1dValues[intIdx]);
}
return pstmt;
}
};
int intJdbcCode = jt.update(psc, keyHolder);
Object oCol1 = keyHolder.getKeys().get("id");
user.setId(NumberToolE.o2I(oCol1, null));
return intJdbcCode;
}
catch (Exception e)
{
e.printStackTrace();
if (e instanceof SQLException)
{
throw (SQLException) e;
}
else
{
throw new RuntimeException(e);
}
}
}
……
public User findById(Integer itgUserId) throws SQLException
{
try
{
String strSql =
"select id, name, passwd, stat, remark from tfw_dash_user where id"
+ ((null == itgUserId) ? " is null" : "=?");
System.out.println(strSql);
final Object[] o1dValues = (null == itgUserId) ? new Object[0]
: new Object[] { itgUserId };
List<User> lstUsers = jt.query(strSql, o1dValues, rowMapper);
User user = null;
……
return user;
}
catch (Exception e)
{
……
}
}
……
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> <bean id="jt" ……> …… </bean> <context:component-scan base-package="tfw.integration_demo._04_spring_web_mvc._02_crud._02_spring_jdbc_template_crud"/> </beans>
……
public class UserMgrTestCase
{
private String strConfFile =
"tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml";
private AbstractApplicationContext actxt;
private I_UserDAO userDAO;
@Test
public void testObjects()
throws FileNotFoundException, SQLException
{
……
AbstractApplicationContext actxt =
new ClassPathXmlApplicationContext(strClasspathConfFile);
System.out.println("Application Context:\n\t" + actxt);
JdbcTemplate jt = actxt.getBean("jt", JdbcTemplate.class);
System.out.println("JdbcTemplate:\n\t" + jt);
// I_UserDAO userDAO = new UserDAO(jt); // [X] No such constructor!
I_UserDAO userDAO = actxt.getBean("crud.spring_jdbc_template.userDAO",
I_UserDAO.class);
System.out.println("DAO:\n\t" + userDAO);
actxt.close();
System.out.println("Application Context Closed:\n\t" + actxt);
}
@Before
public void init()
{
actxt = new ClassPathXmlApplicationContext(strConfFile);
System.out.println("Application Context:\n\t" + actxt);
userDAO = actxt.getBean("crud.spring_jdbc_template.userDAO",
I_UserDAO.class);
System.out.println("DAO:\n\t" + userDAO);
}
@After
public void close()
{
……
}
@Test
public void testUserDAO_create() throws SQLException
{
……
}
@Test
public void test……() ……
{
……
}
……
private void showUsers(List<User> lstUsers)
{
……
}
}
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <context:component-scan ……/> <bean id="crud.spring_jdbc_template.userMgrSvc" class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService"> <property name="userDAO" ref="crud.spring_jdbc_template.userDAO"/> </bean> </beans>
…… public class UserMgrTestCase { …… private I_UserDAO userDAO; private _01_UserMgrService userMgrSvc; @Test public void testObjects() throws FileNotFoundException, SQLException { …… // I_UserDAO userDAO = new UserDAO(jt); // [X] No such constructor! I_UserDAO userDAO = actxt.getBean("crud.spring_jdbc_template.userDAO", I_UserDAO.class); System.out.println("DAO:\n\t" + userDAO); // _01_UserMgrService userMgrSvc = new _01_UserMgrService(); // userMgrSvc.setUserDAO(userDAO); _01_UserMgrService userMgrSvc = actxt.getBean("crud.spring_jdbc_template.userMgrSvc", _01_UserMgrService.class); System.out.println("Service:\n\t" + userMgrSvc); actxt.close(); System.out.println("Application Context Closed:\n\t" + actxt); } @Before public void init() { …… userDAO = actxt.getBean("crud.spring_jdbc_template.userDAO", I_UserDAO.class); System.out.println("DAO:\n\t" + userDAO); userMgrSvc = actxt.getBean("crud.spring_jdbc_template.userMgrSvc", _01_UserMgrService.class); System.out.println("Service:\n\t" + userMgrSvc); } @After public void close() { …… } …… @Test public void testUserDAO_discardById() throws SQLException { …… } @Test public void testUserMgrSvc_createUser() throws SQLException { …… } …… private void showUsers(List<User> lstUsers) { …… } }
…… public class _01_UserMgrController implements Controller { …… public void setUserMgrSvc(_01_UserMgrService userMgrSvc) { …… } private String strTitle; …… public void setPageTitle(String strTitle) { this.strTitle = strTitle; } private Properties propsTarget; …… public void setJumpTarget(Properties propsTarget;) { this.propsTarget = propsTarget; } …… public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { …… String strTarget = propsTarget.getProperty("result"); // "/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/result.jsp"; if ("do_add".equals(strCmd)) { …… } else if ("show_detail".equals(strCmd)) { …… strTarget = propsTarget.getProperty("detail"); // "/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/detail.jsp"; } else if ("do_edit".equals(strCmd)) { …… } else if ("discard".equals(strCmd)) { …… } else if ("to_login".equals(strCmd)) { strTarget = propsTarget.getProperty("login"); // "/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/login.html"; } else if ("do_login".equals(strCmd)) { …… } else { …… strTarget = propsTarget.getProperty("list"); // "/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/list.jsp"; } request.setAttribute("page_title", strTitle); return new ModelAndView(strTarget); } }
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean id="crud.basic_jdbc.userMgrSvc" ……> …… </bean> <bean id="crud.basic_jdbc.userMgrCtrl" class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.controller._01_UserMgrController"> <property name="userMgrSvc" ref="crud.basic_jdbc.userMgrSvc"/> <property name="pageTitle" value="Integration&nbsp;Demo / 04&nbsp;Spring&nbsp;Web&nbsp;MVC / 02&nbsp;Create,&nbsp;Read,&nbsp;Update&nbsp;&amp;&nbsp;Discard / 01&nbsp;Basic&nbsp;JDBC&nbsp;CRUD / 01&nbsp;User&nbsp;Management - "/> <property name="jumpTarget"> <props> <prop key="result">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/result.jsp</prop> <prop key="detail">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/detail.jsp</prop> <prop key="login">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/login.html</prop> <prop key="list">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/list.jsp</prop> </props> </property> </bean> <!-- [########################################] --> <bean id="crud.basic_jdbc.itemDAO" ……> …… </bean> …… </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean id="crud.spring_jdbc_template.userMgrSvc" ……> …… </bean> <bean id="crud.spring_jdbc_template.userMgrCtrl" class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.controller._01_UserMgrController"> <property name="userMgrSvc" ref="crud.spring_jdbc_template.userMgrSvc"/> <property name="pageTitle" value="Integration&nbsp;Demo / 04&nbsp;Spring&nbsp;Web&nbsp;MVC / 02&nbsp;Create,&nbsp;Read,&nbsp;Update&nbsp;&amp;&nbsp;Discard / 02&nbsp;Spring&nbsp;JDBC&nbsp;Template&nbsp;CRUD / 01&nbsp;User&nbsp;Management - "/> <property name="jumpTarget"> <props> <prop key="result">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/result.jsp</prop> <prop key="detail">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/detail.jsp</prop> <prop key="login">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/02_spring_jdbc_template_crud/01_user_mgr/login.html</prop> <prop key="list">/WEB-INF/pages/tfw/integration_demo/04_spring_web_mvc/02_crud/01_basic_jdbc_crud/01_user_mgr/list.jsp</prop> </props> </property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans ……> …… <bean id="crud.spring_jdbc_template.userMgrCtrl" ……> …… </bean> <bean id="crud.spring_jdbc_template.urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/tfw/integration_demo/04_spring_web_mvc/02_crud/02_spring_jdbc_template_crud/01_user_mgr/action.spr">crud.spring_jdbc_template.userMgrCtrl</prop> </props> </property> </bean> </beans>