- 编写业务层类
tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService
:
……
public class _01_UserMgrService
{
private I_UserDAO userDAO;
……
public void setUserDAO(I_UserDAO userDAO)
{
this.userDAO = userDAO;
}
……
@Override
public String toString()
{
return "_02_UserMgrService [userDAO=" + userDAO + "]";
}
}
- 在 ApplicationContext 配置文件
src/main/resources/tfw/integration_demo/_04_spring_web_mvc/applicationContext.xml
中注册此业务层对象、指定
ID
,并以 setter
方式向其注入 DAO :
<?xml version="1.0" encoding="UTF-8"?>
<beans ……>
……
<bean id="crud.basic_jdbc.userDAO"
……>
……
</bean>
<bean id="crud.basic_jdbc.userMgrSvc"
class="tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService">
<property name="userDAO" ref="crud.basic_jdbc.userDAO"/>
</bean>
</beans>
- 改造测试用例
tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.UserMgrTestCase
,测试此业务层类及 ApplicationContext 配置文件的正确性:
……
public class UserMgrTestCase
{
……
private I_UserDAO userDAO;
@Test
public void testObjects()
throws FileNotFoundException, SQLException
{
……
// I_UserDAO userDAO = new UserDAO(ds);
I_UserDAO userDAO =
actxt.getBean("crud.basic_jdbc.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.basic_jdbc.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()
{
……
}
// @After
public void close()
{
……
}
……
}
- 所有密码在写入前均需要加密,编写单向加密工具类
tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.service._01_UserMgrService
:
……
import org.apache.commons.codec.digest.DigestUtils;
public class EncryptionTool
{
private static final String SALT = ……;
……
public static String encrypt(String strIn)
{
……
String strEncrypted = DigestUtils.md5Hex(strIn + SALT);
return strEncrypted;
}
}
- 在业务层类中添加方法,调用
DAO :
注意“起始页号
+ 页大小” →
“起始行索引 +
选取行数”的换算:
……
import tfw.integration_demo.util.EncryptionTool;
……
public class _01_UserMgrService
{
……
……
public void setUserDAO(I_UserDAO userDAO)
{
……
}
……
public User createUser(String strUserName, String strUserPasswd,
Byte bYUserStat, String strUserRemark) throws SQLException
{
strUserPasswd = EncryptionTool.encrypt(strUserPasswd);
User user = new User(null, strUserName, strUserPasswd, bYUserStat,
strUserRemark);
int intCode = userDAO.create(user);
System.out.println(intCode);
return user;
}
……
public User findUserById(Integer itgUserId) throws SQLException
{
……
}
……
……
public List<User> listAllUsers() throws SQLException
{
……
}
……
public List<User> listUsersByPage(int intPageSeq, int intPageSize)
throws SQLException
{
int intStartRowIdx = (intPageSeq - 1) * intPageSize;
return userDAO.listByRange(intStartRowIdx, intPageSize);
}
……
public User updateUser(Integer itgUserId, String strUserName,
String strUserPasswd, Byte bYUserStat, String strUserRemark)
throws SQLException
{
……
}
……
……
@Override
public String toString()
{
……
}
}
- 改造测试用例
tfw.integration_demo._04_spring_web_mvc._02_crud._01_basic_jdbc_crud.UserMgrTestCase
,测试此业务层类中的各方法。
……
public class UserMgrTestCase
{
……
private I_UserDAO userDAO;
private _01_UserMgrService userMgrSvc;
@Test
public void testObjectExtracting()
throws FileNotFoundException, SQLException
{
……
}
// @Before
public void init()
{
……
userDAO = actxt.getBean("crud.basic_jdbc.userDAO", I_UserDAO.class);
System.out.println("DAO:\n\t" + userDAO);
userMgrSvc = actxt.getBean("crud.basic_jdbc.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
{
// Preparing.
System.out.println("Service:\n\t" + userMgrSvc);
String strDate =
DateToolE.simpleFormat(new Date(), "yyyy-MM-dd_HH-mm-ss.SSS");
// Main.
User user = userMgrSvc.createUser("user_" + strDate, "password",
(byte) 2, "remark_" + strDate);
System.out.println("User:\n\t" + user);
}
@Test
public void testUserMgrSvc_findUserById() throws SQLException
{
……
}
……
private void showUsers(List<User> lstUsers)
{
……
}
}