【SSH小Demo】个人信息展示页面——注解方式

上两篇文章介绍了使用SSH制作的一个个人信息小Demo,本篇文章将记录使用注解来完成开发,不得不说,使用注解的的确确能减少我们的不少工作量,同时也能减少我们出错率,我之前曾经试过在xml文件配置时打错一个英文字母,导致改了好久的BUG~~

注意!struts2要想使用注解,必须要有struts2-convention-plugin-x.x.x.jar 这个jar包

 

引入jar包后就先修改applicationContext.xml

1.增加注解配置,组建扫描

<context:annotation-config />
<context:component-scan base-package="com.zdxh" />

2. 注释掉 action,service,dao


3. 替换SessionFactory类

org.springframework.orm.hibernate3.LocalSessionFactoryBean

替换为

org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean

4. 去掉 

替换为 

 <!-- 注解扫描 -->
 <property name="packagesToScan">
  <list>
     <value>com.zdxh.*</value>
  </list>
</property>

然后再修改修改struts.xml

注释掉action配置

<?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>
   
  <constant name="struts.objectFactory" value="spring"/>
   
  <package name="basicstruts" extends="struts-default">
   
  <!-- <action name="*People" class="peopleActionBean" method="{1}">
    <result name="listPeople">WEB-INF/jsp/listPeople.jsp</result>
    <result name="listPeopleAction" type="redirect">listPeople</result>
    <result name="editPeople">WEB-INF/jsp/editPeople.jsp</result>
  </action> -->
   
  </package>
</struts>

为PeopleAction添加注解

1. 配置Namespace,ParentPackage,Results

2. 使用@Autowired自动装配peopleService
3. 在list()方法前加注解 @Action("listPeople"),以映射路径,其他也照此模仿

package com.zdxh.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import com.zdxh.pojo.People;
import com.zdxh.service.PeopleService;
import com.zdxh.util.Page;


@Namespace("/")
@ParentPackage("struts-default")
@Results({
	@Result(name="listPeople", location="/WEB-INF/jsp/listPeople.jsp"),
	@Result(name="listPeopleAction",type="redirect", location="/listPeople"),
	@Result(name="editPeople", location="/WEB-INF/jsp/editPeople.jsp"),
		})
public class PeopleAction {
	List<People> ps;
	@Autowired
	PeopleService peopleService;
	People people;
	Page page;
	String keyword;
	@Action("listPeople")
	public String list(){
		if(page==null)
			page=new Page();
		int total=peopleService.getTotal();
		System.out.println(total);
		page.setTotal(total);
		ps=peopleService.listByPage(page);
		
		return "listPeople";
	}
	@Action("addPeople")
	public String add(){
		peopleService.add(people);
		return "listPeopleAction";
	}
	@Action("deletePeople")
	public String delete(){
		System.out.println("删除");
		peopleService.delete(people);
		return "listPeopleAction";
	}
	@Action("editPeople")
	public String edit(){
		people=peopleService.get(people.getId());
		return "editPeople";
	}
	@Action("updatePeople")
	public String update(){
		peopleService.update(people);
		return "listPeopleAction";
	}
	@Action("searchPeople")
	public String search(){
		ps=peopleService.search(keyword);
		int total=ps.size();
		System.out.println("total"+total);
		if(page==null)
			page=new Page();
		page.setTotal(total);
		for(int i=0;i<ps.size();i++)
			System.out.println(ps.get(i));
		return "listPeople";
	}
	public String getKeyword() {
		return keyword;
	}

	public void setKeyword(String keyword) {
		this.keyword = keyword;
	}

	/***setter and getter***/
	
}

再来为PeopleServiceImpl 添加注解

1. 使用@Service 为 PeopleServiceImpl添加注解
2. 使用@Autowired自动装配peopleDAO

就只是这两个地方修改

1. 使用Repository 为PeopleDAOImpl 添加注解
2. 重写setSessionFactory方法,并使用@Resource(name="sf")对其注解,以接受sessionFactory注入( 在 applicationContext.xml中sessionFactory所取得id是sf)
为什么要重写?
虽然PeopleDAOImpl 继承了HibernateTemplate ,有setSessionFactory方法,但是HibernateTemplate 中的setSessionFactory方法,并没有被注解,所以就不会被注入sf, 因此需要在这里重写这个方法。 

为People添加注解 

1. 使用@Entity 为 People 添加注解
2. @Table 指定对应表名
3. @id 表明主键
4. @GeneratedValue(strategy = GenerationType.IDENTITY) 使用自增长策略
5. @Column 指明属性对应的数据库中的字段名(当属性名和字段名一样时,会自动匹配,可以不写)

package com.zdxh.pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="people")
public class People {
	private int id;
	private String name;
	private String sex;
	private String phone;
	private String address;
	@Column(name="sex")
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	@Column(name="phone")
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Column(name="address")
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	
	@Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id") 
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	@Column(name="name")
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
		return "Category [id=" + id + ", name=" + name + ", sex=" + sex + ", phone=" + phone + ", address=" + address
				+ "]";
	}
	
	
}

完成之后,启动tomcat,访问http://127.0.0.1:8080/ssh/listPeople

没问题,就完成了注解开发 

猜你喜欢

转载自blog.csdn.net/weixin_41866960/article/details/84967084