//業(yè)務(wù)類,用來對注冊信息進行篩選,調(diào)用dao實現(xiàn)類完成數(shù)據(jù)庫保存user數(shù)據(jù)
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就不再展示了。
注冊用戶結(jié)果:
![
![注冊用戶](http://upload-images.jianshu.io/upload_images/2352668-2eddd8f31ac7d2e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
](http://upload-images.jianshu.io/upload_images/2352668-5cf2f195e5ad1fd0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
點擊注冊之后我們發(fā)現(xiàn)在數(shù)據(jù)庫里成功插入了一條記錄了也祠。證明整合成功昙楚。當然在容器第一次運行時就已經(jīng)為我們創(chuàng)建了對應(yīng)的表。
在整合過程中遇到了一個小問題是我之前沒有發(fā)現(xiàn)的诈嘿,也算是撿漏了堪旧,所以自己親自實踐是非常有必要的。
<!-- //加載實體類的映射文件位置及名稱 -->
<property name="mappingLocations">
<list>
<value>classpath:resources/hibernate/*.hbm.xml</value>
</list>
</property>
錯誤就是在上面配置文件發(fā)生的奖亚,當然現(xiàn)在的配置是改過來之后的了淳梦。當時我寫的是mappingResources,在運行時一直報找不到對應(yīng)hbm.xml文件昔字。當時我就納悶了路徑?jīng)]有寫錯啊爆袍,為何會找不到呢首繁?度娘了一下原來發(fā)現(xiàn)原因出在<property name="mappingLocations"> 這里。原來不同的屬性有不同的限制的陨囊。我想這個點估計也是剛起步的人容易掉的坑吧弦疮。他們的具體區(qū)別如下:
mappingResources、mappingLocations蜘醋、mappingDirectoryLocations胁塞、mappingJarLocations
他們的區(qū)別:
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框架的整合就已經(jīng)完成了伺糠。