美文网首页
spring整合hibernate(三)

spring整合hibernate(三)

作者: FTOLsXD | 来源:发表于2017-02-23 18:44 被阅读71次
//业务类,用来对注册信息进行筛选,调用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就不再展示了。

注册用户结果:

![ ![注册用户](https://img.haomeiwen.com/i2352668/2eddd8f31ac7d2e0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ](https://img.haomeiwen.com/i2352668/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框架的整合就已经完成了。

相关文章

网友评论

      本文标题:spring整合hibernate(三)

      本文链接:https://www.haomeiwen.com/subject/fqwbwttx.html