首页 技术 正文
技术 2022年11月17日
0 收藏 834 点赞 4,065 浏览 4908 个字

对于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里面配置,如下:

搭建ssh后的简化

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

当然我们所对应的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容器。

接下来我们来看代码:

搭建ssh后的简化

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

搭建ssh后的简化

搭建ssh后的简化

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

搭建ssh后的简化

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,086
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,561
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,411
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,184
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,820
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,904