搭建ssh后的简化

对于SSh框架的简化,我们可以从下面几个方面来剖析:

1、实体类entity

2.注入

3.类注解

 

下面,我来一一为大家讲解。ps:写的不好还请多多指教,欢迎大家”来找茬“。

关于实体类的简化,我们一般是将.hbm.xml文件删除,然后配着注解来实现简化,下面我们举一个简单的例子来体验实体类的简化给我们带来的便利

如下:

以前我们需要编写实体类.hbm.xml来完成映射

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="news.entity">
    <class name="News" table="news">
        <id name="id">
            <generator class="increment"/>
        </id>
        <property name="title" type="string" length="50" column="title" not-null="true"></property>
        <property name="content"></property>
        <property name="begintime"></property>
        <property name="usename"></property>
    </class>
</hibernate-mapping>

而现在,我们用了注解就不需要了,代码如下:

package news.entity;

import java.util.Date;

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



/**
 * @author Administrator
 *
 */
@Entity
@Table(name="news")//这里的name对应的是数据库那边的表名
public class News {
    private Integer id;
    private String title;
    private String content;
    private Date begintime;
    private String usename;
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    
    @Column
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    
    @Column
    public String getContent() {
        return content;
    }
    public void setContent(String content) {
        this.content = content;
    }
    
    @Column
    public Date getBegintime() {
        return begintime;
    }
    public void setBegintime(Date begintime) {
        this.begintime = begintime;
    }
    
    @Column
    public String getUsename() {
        return usename;
    }
    public void setUsename(String usename) {
        this.usename = usename;
    }
    
}

接下来我们来看注入。

对于注入的简化,我的理解就是不需要new,把它交给spring去完成

而在这之前,我们都是定义一个私有属性,并给他设置set方法,然后在applicationContext.xml中设置如下

<bean id="newsAction" class="news.action.NewsAction">
        <property name="ns" ref="myNewsService"/>
</bean>
<bean id="myNewsService"class="news.service.NewsServiceImpl">
        <property name="nd" ref="myNewsDao"/>
</bean>
<bean id="myNewsDao" class="news.dao.NewsDaoImpl">
    <property name="sf" ref="sessionFactory"></property>
</bean>

然而我们用上注解的话,applicationContext.xml文件就变成了这样

<bean id="newsAction" class="news.action.NewsAction"/>
<bean id="myNewsService"class="news.service.NewsServiceImpl"/>
<bean id="myNewsDao" class="news.dao.NewsDaoImpl"/>

删除完映射文件,我们还需要在applicationContext.xml里面配置,如下:

这样才能知道她找到对应的实体类

当然我们所对应的Action,dao.service类都需要加上注解,如下:

package news.action;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.opensymphony.xwork2.ActionSupport;

import news.entity.News;
import news.service.NewsService;

public class NewsAction extends ActionSupport {
    //@Resource(name="myNewsService") 这是jdk自带注解
    
    @Autowired
    @Qualifier//这两个是spring的注解
    private NewsService ns;
    
    private List<News> listAll;
    
    private Integer id;
    
    private String message;
    public List<News> getListAll() {
    
        return listAll;
    }

    
    public void setId(Integer id) {
        this.id = id;
    }
    
    
    public Integer getId() {
        return id;
    }


    public String getMessage() {
        return message;
    }


    public void setMessage(String message) {
        this.message = message;
    }


    public String showAll(){
        listAll=ns.showAll();
//        System.out.println(ns);
        return "success";
    }
    public String deleteSingleNews(){
        message=ns.deleteSingleNews(id);
        return message;
    }
}
package news.dao;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import news.entity.News;

public class NewsDaoImpl implements NewsDao {
    //@Resource(name="sessionFactory")
    @Autowired
    @Qualifier
    private SessionFactory sf;
    
    public List<News> showAll(){
        Session session =sf.openSession();
        //Session session=sf.getCurrentSession();
        Query query=session.createQuery("from News");
        List<News> listAll=query.getResultList();
        return listAll;
    }
    public String deleteSingleNews(Integer id){
        Session session =sf.openSession();
        //Session session=sf.getCurrentSession();
        Query query =session.createQuery("from News where id=:myid");
        query.setParameter("myid", id);
        List<News> deleteList=query.getResultList();
        
        String returnValue="fail";
        if(deleteList.size()==1){
            News news=deleteList.get(0);
            session.getTransaction().begin();
            session.delete(news);
            returnValue="deleteList";
            session.getTransaction().commit();
            session.close();
        }
        return returnValue;
    }
    
}
package news.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import news.dao.NewsDao;
import news.entity.News;

public class NewsServiceImpl implements NewsService {
    //@Resource(name="myNewsDao")
    @Autowired
    @Qualifier
    private NewsDao nd=null;
    
    public List<News> showAll(){
        List<News> listAll=nd.showAll();
        return listAll;
        
    }
    
    public String deleteSingleNews(Integer id){
        String returnValue="fail";
        returnValue=nd.deleteSingleNews(id);
        return returnValue;
    }
    
}

上面的代码,之所以没用jdk自带的注解是因为?

我稍后给大家公布答案~.~

 最后,我来给大家说说类注解吧。类注解是什么呢?

用我自己的话来说就是标签的类自动注册到spring容器。

接下来我们来看代码:

其他的都一样,所以后面的就不做批注了。

 

这了这一步,我们就来公布答案为什么不用jdk自带注解而用spring注解。

写的不好的地方还请大家多多指教~。~

 

posted on 2016-10-14 15:31  皮皮聪  阅读(362)  评论(0编辑  收藏  举报

导航