`
xiaojunhu
  • 浏览: 30110 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

struts+spring+ibatis示例(附:源代码)

 
阅读更多

下面这篇文章介绍的是struts+spring+ibatis示例,笔者将从他们的配置直到部署都尽可能的介绍清楚:

1 首先创建数据库(以oracle为例),数据库脚本如下:

CREATETABLEEMPLOYEE(EMPLOYEEIDINTEGERNOTNULLPRIMARYKEY,FIRSTNAMEVARCHAR(256),LASTNAMEVARCHAR(256),AGEINTEGER,DEPARTMENTIDINTEGER)
CREATETABLEDEPARTMENT(DEPARTMENTIDINTEGER,NAMEVARCHAR(256))
INSERTINTOEMPLOYEEVALUES(1,'John','Doe',36,100);
INSERTINTOEMPLOYEEVALUES(2,'Bob','Smith',25,300);
INSERTINTOEMPLOYEEVALUES(3,'Carrie','Heffernan',32,200);
INSERTINTOEMPLOYEEVALUES(4,'Doug','Heffernan',34,100);
INSERTINTOEMPLOYEEVALUES(5,'Arthur','Spooner',64,300);
INSERTINTODEPARTMENTVALUES(100,'Accounting');
INSERTINTODEPARTMENTVALUES(200,'R&D');
INSERTINTODEPARTMENTVALUES(300,'Sales');

接下来要为EMPLOYEE表的主键建立自动增长,方法参见前面的一篇文章,再此不在重复了,如果不建自动增长的话会报OAR-00001错误。

2 为项目添加上述三种框架,建议用myeclipse,这样就可以直接加载了,关于ibatis的下载参考下面的联接: http://ibatis.apache.org/ 程序里的jar包如下:

3 建立POJO类:

packagecss.web.demo.model;

importjava.io.Serializable;

publicclassEmployeeimplementsSerializable...{
/***//**
*
*/

privatestaticfinallongserialVersionUID=4704328446890394252L;
privateIntegeremployeeId;
privateIntegerage;
privateStringfirstName;
privateStringlastName;
privateIntegerdepartmentId;

publicEmployee()...{
}


publicEmployee(IntegeremployeeId,StringfirstName,StringlastName,Integerage,IntegerdepartmentId)...{
this.employeeId=employeeId;
this.firstName=firstName;
this.lastName=lastName;
this.age=age;
this.departmentId=departmentId;
}


publicIntegergetDepartmentId()...{
returndepartmentId;
}


publicvoidsetDepartmentId(IntegerdepartmentId)...{
this.departmentId=departmentId;
}


publicIntegergetEmployeeId()...{
returnemployeeId;
}


publicvoidsetEmployeeId(IntegeremployeeId)...{
this.employeeId=employeeId;
}


publicIntegergetAge()...{
returnage;
}


publicvoidsetAge(Integerage)...{
this.age=age;
}


publicStringgetFirstName()...{
returnfirstName;
}


publicvoidsetFirstName(StringfirstName)...{
this.firstName=firstName;
}


publicStringgetLastName()...{
returnlastName;
}


publicvoidsetLastName(StringlastName)...{
this.lastName=lastName;
}


}

packagecss.web.demo.model;

importjava.io.Serializable;

publicclassDepartmentimplementsSerializable...{
/***//**
*
*/

privatestaticfinallongserialVersionUID=9077129830166112946L;
IntegerdepartmentId;
Stringname;

publicDepartment()...{
}


publicDepartment(IntegerdepartmentId,Stringname)...{
this.departmentId=departmentId;
this.name=name;
}


publicIntegergetDepartmentId()...{
returndepartmentId;
}


publicvoidsetDepartmentId(IntegerdepartmentId)...{
this.departmentId=departmentId;
}


publicStringgetName()...{
returnname;
}


publicvoidsetName(Stringname)...{
this.name=name;
}


}

4 DAO层的实现:

packagecss.web.demo.persistence;

importjava.util.List;

publicinterfaceDepartmentDao...{
publicListgetAllDepartments();
}

packagecss.web.demo.persistence;

importorg.springframework.orm.ibatis.SqlMapClientTemplate;

importjava.util.List;

publicclassDepartmentIbatisDaoextendsSqlMapClientTemplateimplementsDepartmentDao...{
publicListgetAllDepartments()...{
returnqueryForList("Department.getAll",null);
}

}

packagecss.web.demo.persistence;

importjava.util.List;

importcss.web.demo.model.Employee;

publicinterfaceEmployeeDao...{

publicListgetAllEmployees();

publicEmployeegetEmployee(Integerid);

publicintupdate(Employeeemp);

publicIntegerinsert(Employeeemp);

publicintdelete(Integerid);
}

packagecss.web.demo.persistence;

importorg.apache.commons.logging.Log;
importorg.apache.commons.logging.LogFactory;
importorg.springframework.orm.ibatis.SqlMapClientTemplate;

importcss.web.demo.model.Employee;

importjava.util.List;

publicclassEmployeeIbatisDaoextendsSqlMapClientTemplateimplementsEmployeeDao...{
Loglogger
=LogFactory.getLog(this.getClass());

publicListgetAllEmployees()...{
returnqueryForList("Employee.getAll",null);
}


publicEmployeegetEmployee(Integerid)...{
return((Employee)queryForObject("Employee.getById",id));
}


publicintupdate(Employeeemp)...{
returnupdate("Employee.update",emp);
}


publicIntegerinsert(Employeeemp)...{
return(Integer)insert("Employee.insert",emp);
}


publicintdelete(Integerid)...{
returndelete("Employee.delete",id);
}

}

5sqlConfig文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEsqlMapConfig
PUBLIC"-//iBATIS.com//DTDSQLMapConfig2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd"
>

<sqlMapConfig>
<!--thereareplentyofotheroptionalsettings,seetheibatis-sql-mapsdoc-->
<settings
enhancementEnabled="true"
useStatementNamespaces
="true"
/>
<sqlMapresource="css/web/demo/persistence/Employee.xml"/>
<sqlMapresource="css/web/demo/persistence/Department.xml"/>
</sqlMapConfig>

6 service层的实现(见源代码):

7 spring配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd"
>

<beans>

<beanid="propertyConfigurer"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<propertyname="location"value="classpath:spring.properties"/>
</bean>

<beanname="/employeeSetUp"class="css.web.demo.action.EmployeeAction">
<constructor-argindex="0"ref="employeeService"/>
<constructor-argindex="1"ref="departmentService"/>
</bean>

<beanname="/employeeProcess"class="css.web.demo.action.EmployeeAction">
<constructor-argindex="0"ref="employeeService"/>
<constructor-argindex="1"ref="departmentService"/>
</bean>

<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
<propertyname="driverClassName"value="${jdbc.driverClassName}"/>
<propertyname="url"value="${jdbc.url}"/>
<propertyname="username"value="${jdbc.username}"/>
<propertyname="password"value="${jdbc.password}"/>
</bean>

<beanid="sqlMapClient"
class
="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<propertyname="configLocation">
<value>classpath:css/web/demo/persistence/SqlMapConfig.xml</value>
</property>
<propertyname="useTransactionAwareDataSource">
<value>true</value>
</property>
<propertyname="dataSource">
<refbean="dataSource"/>
</property>
</bean>

<beanid="sqlMapClientTemplate"
class
="org.springframework.orm.ibatis.SqlMapClientTemplate">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>

<beanid="employeeDao"class="css.web.demo.persistence.EmployeeIbatisDao">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>

<beanid="employeeService"class="css.web.demo.service.EmployeeDaoService">
<constructor-argindex="0"ref="employeeDao"/>
</bean>

<beanid="departmentDao"class="css.web.demo.persistence.DepartmentIbatisDao">
<propertyname="sqlMapClient">
<refbean="sqlMapClient"/>
</property>
</bean>

<beanid="departmentService"class="css.web.demo.service.DepartmentDaoService">
<constructor-argindex="0"ref="departmentDao"/>
</bean>



</beans>

8 页面及ACTION的实现(见源代码):

9 总结

上面的步骤全是我经验之谈,但我从小语文没学好,所以不知道该怎么表述才能让大家明白,不过有兴趣的朋友,可以到我的资源去下载原代码学习,如果有什么好的建议的话还请告诉我,呵呵,只要按照上面的步骤,在对照源代码就一定能得到你想要的结果。希望我们一起进步!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics