struts2+hibernate3.3+spring3.0 实现零配置
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
@Entity
@Table(name="users")
public class Users {
private int id;// 编号
private String name;//姓名
private int age;//年龄
private String sex;//性别 男 女
private Department department;//部门
private String telphone;//电话
public Users(){
}
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String getTelphone() {
return telphone;
}
public void setTelphone(String telphone) {
this.telphone = telphone;
}
}
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Department {
private int depid;//编号
private String depname;//名称
private String address;//地址
public Department(){
}
@Id
@GeneratedValue
public int getDepid() {
return depid;
}
this.depid = depid;
}
return depname;
}
this.depname = depname;
}
return address;
}
this.address = address;
}
************************************************
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config /><!-- 注解 -->
<context:component-scan base-package="com.cn.nnny.ssh*.*" /><!-- 扫描包 -->
<aop:aspectj-autoproxy /><!-- 代理 -->
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=hibernate">
</property>
<property name="username" value="sa"></property>
<property name="password" value="sa"></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<value>com.cn.nnny.usermanager.pojo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- 定义事务管理器 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!--<tx:annotation-driven transaction-manager="txManager"/> 申明annotation 加载事务驱动 -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="list*" read-only="true" /><!-- 加事务的方法 -->
<tx:method name="save*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
<tx:method name="delete*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
<tx:method name="update*" propagation="REQUIRED"/><!-- 事务的生成特性 -->
</tx:attributes>
</tx:advice>
<aop:pointcut id="bussinessService" expression="execution(public * com.cn.nnny.ssh.service..*.*(..))" />
<aop:advisor pointcut-ref="bussinessService" advice-ref="txAdvice" />
</aop:config>
</beans>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping></web-app>
****************************************************
改成:
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.opensymphony.xwork2.ActionSupport;
@Scope(value = "prototype")
@Namespace(value = "/department")
public class DepartmenAction extends ActionSupport {
@Autowired
private IDepartmentService service;//拿到服务
@Autowired
private DepartmentVo departmentVo;
private List<Department> departmentlist;
@Action(value = "index", results = {
@Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
@Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
public String execute() throws Exception {
return super.execute();
}
@Action(value = "save", results = {
@Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
@Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
public String save() throws Exception {
System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwww");
service.save(departmentVo);
return super.execute();
}
@Action(value = "list", results = {
@Result(name = "success", location = "/WEB-INF/jsp/department/index.jsp"),
@Result(name = "errors", location = "/WEB-INF/jsp/department/index.jsp") })
public String list() throws Exception {
departmentlist=service.list();
return super.execute();
}
public DepartmentVo getDepartmentVo() {//必须有set方法 才能收集值
return departmentVo;
}
this.departmentVo = departmentVo;
}
public List<Department> getDepartmentlist() {
return departmentlist;
}
public void setDepartmentlist(List<Department> departmentlist) {
this.departmentlist = departmentlist;
}
}
****************dao中**************只附关键的*********88
public class DepartmentDAO extends HibernateDaoSupport implements IDepartmentDAO {
private static final Logger log = LoggerFactory.getLogger(DepartmentDAO.class);
protected void initDao() {
// do nothing
}
* @see com.cn.nnny.ssh.dao.IDepartmentDAO#save(com.cn.nnny.ssh.pojo.Department)
*/
@Autowired
public void setSessionFactoryOverride(SessionFactory sessionFactory) {
super.setSessionFactory(sessionFactory);
}
public void save(Department transientInstance) {
log.debug("saving Department instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
*********************service中************************************
import java.util.List;
import com.cn.nnny.ssh.pojo.Department;
import com.cn.nnny.ssh.service.IDepartmentService;
import com.cn.nnny.ssh.vo.DepartmentVo;
import com.sun.org.apache.commons.beanutils.BeanUtils;
public class DepartmentService implements IDepartmentService {
@Autowired
private IDepartmentDAO departmentDAO;
public boolean save(DepartmentVo departmentVo){
boolean ok=true;
Department department=new Department();
try {
BeanUtils.copyProperties(department, departmentVo);//值复制对应名字的
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
departmentDAO.save(department);
System.out.println(departmentVo.getDepname());
return ok;
}
public List<Department> list(){
List<Department> departmentlist=departmentDAO.findAll();
return departmentlist;
}
************************ok结束**********************************
转载于:https://blog.51cto.com/talentb/507068
struts2+hibernate3.3+spring3.0 实现零配置相关推荐
- ppst技术视频——spring3.0 注解定时任务配置
ppst 视频-- spring3.0 注解定时任务配置:请访问ppst 技术视频分享平台 , www.ppst.cc,上面有最新的技术视频 1.在spring的配置文件中加入配置 <conte ...
- Java Web笔记之Struts2.1 +Hibernate3.3 +Spring3.0
2019独角兽企业重金招聘Python工程师标准>>> 1.Struts2 1.1.了解Struts2 Struts2是基于MVC设计模式的Java Web框架技术之一,按照MVC设 ...
- 基于james3.0 的邮件系统(struts2.3.2 +spring3.0.1+jpa(hibernate3.6.5)实现)b/s模式--java邮件系统...
2019独角兽企业重金招聘Python工程师标准>>> 米林(ljg)邮箱使用说明 java邮件系统 下载地址一 https://code.google.com/p/java-mai ...
- struts2.1.8+hibernate3.2+Spring3.0+ExtJs4.2+MySql+WebSocket 更新
软件更名MyDesktop 增加在线和隐身两个用户状态 增加屏蔽群消息功能 增加修改密码功能 增加主界面加载进度条: 增加解散群恢复群功能: 增加了禁用和启用用户: 增加聊天窗口,管理中心,页签的右键 ...
- 简述Struts2 Convention零配置
从struts2.1开始,struts2不再推荐使用Codebehind作为零配置插件,而是改为使用Convention插件来支持零配置,和Codebehind相比,Convention插件更彻底,该 ...
- SSH (Struts2+Spring3.0+Hibernate3)框架(二) 框架的配置
一.准备工作: 1. JDK -> jdk1.6.0_17 安装(环境变量配置): JAVA_HOME = C:\ jdk1.6.0_17; PATH = %JAVA_HOME%\bin; %J ...
- Myeclipse10下搭建SSH框架(图解)Struts2.1+Spring3.0+Hibernate3.3
一.建立一个Web Project ①点击File→New→Web Project 新建一个Web工程. ②在Project Name中输入项目名ssh,在J2EE Specificatio ...
- Spring3, Hibernate3.6与Proxool连接池配置
为什么80%的码农都做不了架构师?>>> Proxool连接池Spring3.1Hibernate3.6 鉴于Spring3.0不采用Servlet启动,改用listener, ...
- struts2 零配置
一.新建一个web项目,命名为:struts2 二.导入strut2所需的jar包 所需jar下载:http://pan.baidu.com/s/1dDxP4Z3 三.配置struts2的启动文件,在 ...
最新文章
- CodeGen编写自定义表达式标记
- 推荐10个百度,阿里,美团系公众号,第2个化学专业,半路出家!
- 钱趣多风控新举措:源头选择与物理隔离
- 从技术角度讨论微服务
- Manacher 算法模板
- 腾讯云安全专家 Killer 告诉你,企业上云怎么做更安全
- 高效的企业测试-工作流和代码质量(4/6)
- 带你用Python玩转PPT
- python django开发新闻聚合检索推荐_Django查询以检索符合相关选项卡上聚合条件的行...
- 23 android多线程
- vmware之VMware Remote Console (VMRC) SDK(一)
- DB9串口定义及含义(全)
- 2G维码器-以及4G维码器--基于合宙air202 air720sl luat
- 20170627——【肿瘤】肿瘤的异质性
- JDK1.8帮助文档 chm格式中英文
- 4.前端注册表单验证 表单回填
- 那些曾经被央视拿来跟韩寒作对比的三好学生们,十年之后,再看看(转)
- matlab中的spline,在MATLAB中与spline(x,y,xi)插值效果相同的命令是( )
- ChatGPT教程之 01 什么是ChatGPT革命性的对话生成新工具
- 【Python_PyQtGraph 学习笔记(五)】基于PyQtGraph和GraphicsLayoutWidget动态绘图并实现窗口模式,且保留全部绘图信息
热门文章
- MATLAB xlswrite 写数据 到 Excel文件
- mysql的远程服务开启_mysql开启远程服务
- java撕裂_屏幕撕裂与卡顿分析
- 河北机电职业技术学院计算机分数线,河北机电职业技术学院历年分数线 2021河北机电职业技术学院录取分数线...
- 传说之下音乐计算机版,传说之下同人音乐
- Shape Correspondence and Functional Maps
- 文本数据标注工具doccano【介绍最详细的一遍文章】
- 安利几个优质nlp开源项目
- python中输出某段文字_Python如何输出字符串中字符出现的个数
- Typecho安装后后台界面和文章链接均为404错误的解决方法