//業務類,用來對注冊信息進行篩選,調用dao實現類完成數據庫保存user數據
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public boolean register(String name, String password) {
if(!"".equals(name) && !"".equals(password) && name != null && password != null){
userDao.addUser(name, password);
return true;
}
return false;
}
}
//外部配置文件,配置了連接池所需要的一些屬性,便于修改維護
jdbc.url = jdbc:mysql://localhost:3306/dbTest?useUnicode=true&characterEncoding=utf8
jdbc.username = root
jdbc.password=123456
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.maxPoolSize = 40
jdbc.minPoolSize = 1
jdbc.initialPoolSize = 1
jdbc.maxIdleTime = 20
//struts2配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd" >
<struts>
<constant name="struts.i18n.encoding" value="utf-8"></constant>
<package name="test" extends="struts-default">
<action name="*Action" class="testAction" method="{1}">
<result name="success">/WEB-INF/success.jsp</result>
<result name="fail">/WEB-INF/fail.jsp</result>
<result name="register">/WEB-INF/registerSuccess.jsp</result>
<result name="registerFail">/WEB-INF/registerFail.jsp</result>
</action>
</package>
</struts>
其余jsp就不再展示了。
注冊用戶結果:

](http://upload-images.jianshu.io/upload_images/2352668-5cf2f195e5ad1fd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
點擊注冊之后我們發現在數據庫里成功插入了一條記錄了。證明整合成功。當然在容器第一次運行時就已經為我們創建了對應的表。
在整合過程中遇到了一個小問題是我之前沒有發現的,也算是撿漏了,所以自己親自實踐是非常有必要的。
<!-- //加載實體類的映射文件位置及名稱 -->
<property name="mappingLocations">
<list>
<value>classpath:resources/hibernate/*.hbm.xml</value>
</list>
</property>
錯誤就是在上面配置文件發生的,當然現在的配置是改過來之后的了。當時我寫的是mappingResources,在運行時一直報找不到對應hbm.xml文件。當時我就納悶了路徑沒有寫錯啊,為何會找不到呢?度娘了一下原來發現原因出在<property name="mappingLocations"> 這里。原來不同的屬性有不同的限制的。我想這個點估計也是剛起步的人容易掉的坑吧。他們的具體區別如下:
mappingResources、mappingLocations、mappingDirectoryLocations、mappingJarLocations
他們的區別:
1. mappingResources:指定classpath下具體映射文件名
<property name="mappingResources">
<value>petclinic.hbm.xml </value>
</property>
2. mappingLocations:可以指定任何文件路徑,并且可以指定前綴:classpath、file等
<property name="mappingLocations">
<value>/WEB-INF/petclinic.hbm.xml </value>
</property>
<property name="mappingLocations">
<value>classpath:/com/company/domain/petclinic.hbm.xml </value>
</property>
也可以用通配符指定,'*'指定一個文件(路徑)名,'**'指定多個文件(路徑)名,例如:
<property name="mappingLocations">
<value>classpath:/com/company/domainmaps/*.hbm.xml </value>
</property>
上面的配置是在com/company/domain包下任何maps路徑下的hbm.xml文件都被加載為映射文件
3. mappingDirectoryLocations:指定映射的文件路徑
<property name="mappingDirectoryLocations"> <list>
<value>WEB-INF/HibernateMappings</value>
</list>
</property>
也可以通過classpath來指出
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/XXX/package/</value>
</list>
</property>
到這里ssh框架的整合就已經完成了。