关闭

SSH(JFIS)

659人阅读 评论(0) 收藏 举报
 
 
applicationContext.xml(树叶或白菜)
 
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
 
<beans>
 
       <bean id="DataSource"
              class="org.apache.commons.dbcp.BasicDataSource">
              <property name="driverClassName">
                     <value>com.microsoft.jdbc.sqlserver.SQLServerDriver</value>
              </property>
              <property name="url">
<!--                value中间不能有空格-->
                     <value>jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=DB_Wuliu</value>
              </property>
              <property name="username">
                     <value>sa</value>
              </property>
       </bean>
       <bean id="SessionFactory"
              class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
              <property name="dataSource">
                     <ref bean="DataSource" />
              </property>
              <property name="hibernateProperties">
                     <props>
                            <prop key="hibernate.dialect">
                                   org.hibernate.dialect.SQLServerDialect
                            </prop>
                            <prop key="hibernate.show_sql">
                                   true
                            </prop>
                     </props>
              </property>
              <property name="mappingResources">
                     <list>
                            <value>com/Lianxi.hbm.xml</value>
                            <value>com/Luser.hbm.xml</value>
                     </list>
              </property>
              </bean>
      
       <bean id="LuserDAO" class="com.LuserDAO">
              <property name="sessionFactory">
                     <ref bean="SessionFactory" />
              </property>
       </bean>
<!--     struts中action的配置。"iuser"为UserAction类中声明的接口变量,"LuserDAO"为接口的实现类-->
       <bean name="/user"
             class="com.struts.action.UserAction">
             <property name="iuser">
              <ref bean="LuserDAO"/>
           </property>
    </bean>
  
       </beans>
 
 
 
 
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
 <data-sources />
 <form-beans >
   <form-bean name="userForm" type="com.struts.form.UserForm" />
    <form-bean name="lianxirenForm" type="com.struts.form.LianxirenForm" />
 </form-beans>
 <global-exceptions />
 <global-forwards />
 <action-mappings >
<!-- type属性里写代理类-->
    <action
      attribute="userForm"
      input="/user.jsp"
      name="userForm"
      parameter="method"
      path="/user"
      scope="request"
      type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="user" path="/user.jsp" />
    </action>
 </action-mappings>
<!-- 需要加插件-->
 <message-resources parameter="com.struts.ApplicationResources" />
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/classes/applicationContext.xml" />
 </plug-in>
</struts-config>
 
一个DAO
 
package com.hp.hibernate.bean;
 
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.hp.struts.dao.IPlacard;
 
publicclass DbPlacardDAO extends HibernateDaoSupport implements IPlacard {      
       protectedvoid initDao() {
              // do nothing
       }
       // save
       publicvoid hp_sava(DbPlacard dbplacard) {
              getHibernateTemplate().save(dbplacard);
       }
       // selectAll
       public List hp_findAll() {
              List list = getHibernateTemplate().find("from DbPlacard");
              return list;
       }
       // delete by Id
       publicvoid hp_delete(DbPlacard dbplacard) {
              getHibernateTemplate().delete(dbplacard);
       }
       // update by Id
       publicvoid hp_update(DbPlacard dbplacard) {
              getHibernateTemplate().update(dbplacard);
       }
       // select by Id
       public DbPlacard hp_findById(Integer id) {
              DbPlacard placard = (DbPlacard) (getHibernateTemplate().get(
                            "com.hp.hibernate.bean.DbPlacard", id));
              return placard;
       }
}
 
Junit测试
package com.bean;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import junit.framework.TestCase;
 
publicclass BUserDAOTest extends TestCase {
 
       ApplicationContext ctx = new FileSystemXmlApplicationContext("src/applicationContext.xml");
       //实例化一个接口对象
       IUser dao = (IUser)ctx.getBean("BUserDAO");
              publicvoid testSelectAll() {
              List list;
              list = dao.selectAll();
              System.out.println(list.size());
       }
}
 
 
 
 
 
 
模糊查询
public List powerFuzzyQuery(String powerName) {
List list = getHibernateTemplate().find(
"from jfis.bean.JfisPowerSys where powerName like ?","%" + powerName + "%");
return list;
}
 
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:438845次
    • 积分:5517
    • 等级:
    • 排名:第4806名
    • 原创:141篇
    • 转载:120篇
    • 译文:4篇
    • 评论:30条
    最新评论