SSH全注解开发:

  (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace、@ParentPackage、@Action...)

 package com.tongji.actions;

 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.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component; import com.tongji.beans.Student;
import com.tongji.service.IStudentService; @Component
@Scope("prototype")
@Namespace("/test")
@ParentPackage("struts-default")
public class RegisterAction {
private String name;
private int age; //声明Service对象
@Autowired
private IStudentService service; 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 IStudentService getService() {
return service;
} public void setService(IStudentService service) {
this.service = service;
} @Action(value="register", results=@Result(location="/welcome.jsp"))
public String execute() {
Student student = new Student(name, age);
service.addStudent(student);
return "success";
}
}

  (2) 在 Spring 配置文件中注册组件扫描的基本包 <context:component-scan base-package="com.tongji"/>,在Dao层、Service层和Action层中注解组件和依赖注入,相应地删除在 Spring 配置文件中删除 dao、service、action 的 Bean 定义

 package com.tongji.dao;

 import java.util.List;

 import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository; import com.tongji.beans.Student; @Repository("studentDao") //表示当前Dao的Bean的id为studentDao
public class StudentDaoHbnImpl implements IStudentDao{ @Autowired
private SessionFactory sessionFactory; public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
} @Override
public void insertStudent(Student student) {
sessionFactory.getCurrentSession().save(student);
} @Override
public void deleteStudent(int id) {
Student student = sessionFactory.getCurrentSession().get(Student.class, id);
//Student student = this.selectStudentById(id);
sessionFactory.getCurrentSession().delete(student);
} @Override
public void updateStudent(Student student) {
sessionFactory.getCurrentSession().update(student);
} @Override
public String selectStudentNameById(int id) {
// Student student = this.selectStudentById(id);
// if (student != null) {
// return student.getName();
// }
// return null; String hql = "select name from Student where id= :id";
String name = (String) sessionFactory.getCurrentSession().createQuery(hql).
setInteger("id", id).
uniqueResult();
return name;
} @Override
public List<String> selectStudentNames() {
String hql = "select name from Student";
return sessionFactory.getCurrentSession().createQuery(hql).list();
} @Override
public Student selectStudentById(int id) {
return sessionFactory.getCurrentSession().get(Student.class, id);
} @Override
public List<Student> selectStudents() {
String hql = "from Student";
return sessionFactory.getCurrentSession().createQuery(hql).list();
} }

  (3) 在 Spring 配置文件中开启 Spring 事务注解驱动 <tx:annotation-driven transaction-manager="transactionManager"/>,使用 Spring 的事务注解管理事务

 package com.tongji.service;

 import java.util.List;

 import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import com.tongji.beans.Student;
import com.tongji.dao.IStudentDao; @Service("studentService") //表示当前Service的Bean的id为studentService
public class StudentServiceImpl implements IStudentService { @Autowired
private IStudentDao dao; public void setDao(IStudentDao dao) {
this.dao = dao;
} @Override
@Transactional
public void addStudent(Student student) {
dao.insertStudent(student);
} @Override
@Transactional
public void removeStudent(int id) {
dao.deleteStudent(id);
} @Override
@Transactional
public void modifyStudent(Student student) {
dao.updateStudent(student);
} @Override
@Transactional(readOnly=true)
public String findStudentNameById(int id) {
return dao.selectStudentNameById(id);
} @Override
@Transactional(readOnly=true)
public List<String> findStudentNames() {
return dao.selectStudentNames();
} @Override
@Transactional(readOnly=true)
public Student findStudentById(int id) {
return dao.selectStudentById(id);
} @Override
@Transactional(readOnly=true)
public List<Student> findStudents() {
return dao.selectStudents();
} }

  (4) 删除Hibernate映射文件,在实体类中以注解的方式表明映射关系,并在 Spring 配置文件的 SessionFactory 定义中,删除映射文件的相关配置mappingResources,添加实体包扫描器packagesToScan。

 package com.tongji.beans;

 import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table; import org.hibernate.annotations.GenericGenerator; @Entity
@Table
public class Student {
@Id
@GenericGenerator(name="xxx", strategy="native")
@GeneratedValue(generator="xxx")
private Integer id;
private String name;
private int age; public Integer getId() {
return id;
} public void setId(Integer 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 Student() {
super();
} public Student(String name, int age) {
super();
this.name = name;
this.age = age;
} @Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
} }

  Spring配置文件:

 <?xml version="1.0" encoding="UTF-8"?>
<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"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 注册数据源:C3P0数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.user}" />
<property name="password" value="${jdbc.password}" />
</bean> <!-- 注册JDBC属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/> <!-- 参考hibernate的主配置文件来配置 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate5.SpringSessionContext</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<property name="packagesToScan" value="com.tongji.beans"/>
</bean> <context:component-scan base-package="com.tongji"/> <!-- 注册事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean> <!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

  至此,全注解开发完成。

Spring笔记13--SSH--全注解开发的更多相关文章

  1. SSH全注解开发

    web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&quo ...

  2. 20.SSM整合-全注解开发

    全注解开发 1.将SpringMVC改为注解 修改spring-mvc.xml 2.将Spring改为注解 将Service改为注解,完成Dao的注入 将事务以注解方式织入到Service 1.修改s ...

  3. spring boot + vue + element-ui全栈开发入门——开篇

    最近经常看到很多java程序员朋友还在使用Spring 3.x,Spring MVC(struts),JSP.jQuery等这样传统技术.其实,我并不认为这些传统技术不好,而我想表达的是,技术的新旧程 ...

  4. spring boot + vue + element-ui全栈开发入门——基于Electron桌面应用开发

     前言 Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. Electron通过将Chromium和Node.js合并到同一个运行时环 ...

  5. spring boot + vue + element-ui全栈开发入门

    今天想弄弄element-ui  然后就在网上找了个例子 感觉还是可以用的  第一步是完成了  果断 拿过来  放到我这里这  下面直接是连接  点进去 就可以用啊 本想着不用vue   直接导入连接 ...

  6. spring+hibernate+Struts2 整合(全注解及注意事项)

    最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...

  7. spring boot + vue + element-ui全栈开发入门——spring boot后端开发

    前言 本文讲解作为后端的spring boot项目开发流程,如果您还不会配置spring boot环境,就请点击<玩转spring boot——快速开始>,如果您对spring boot还 ...

  8. 学习笔记之Python全栈开发/人工智能公开课_腾讯课堂

    Python全栈开发/人工智能公开课_腾讯课堂 https://ke.qq.com/course/190378 https://github.com/haoran119/ke.qq.com.pytho ...

  9. Spring学习04(使用注解开发)

    7.使用注解开发 说明:在spring4之后,想要使用注解形式,必须得要引入aop的包. 在配置文件当中,还得要引入一个context约束 <?xml version="1.0&quo ...

  10. spring boot整合mybatis基于注解开发以及动态sql的使用

    让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...

随机推荐

  1. path变量修改后无法保存

    Eclipse启动时出现错误: A Java Runtime Environment (JRE) or Java Development Kit(JDK) must be available in o ...

  2. BZOJ5286 HNOI/AHOI2018转盘(分块/线段树)

    显然最优走法是先一直停在初始位置然后一次性走完一圈.将序列倍长后,相当于找一个长度为n的区间[l,l+n),使其中ti+l+n-1-i的最大值最小.容易发现ti-i>ti+n-(i+n),所以也 ...

  3. python爬虫headers设置后无效解决方案

    此次遇到的是一个函数使用不熟练造成的问题,但有了分析工具后可以很快定位到问题(此处推荐一个非常棒的抓包工具fiddler) 正文如下: 在爬取某个app数据时(app上的数据都是由http请求的),用 ...

  4. Android Support Palette使用详解

    使用Palette API选择颜色 良好的视觉设计是app成功所必不可少的, 而色彩设计体系是设计的基础构成. Palette包是支持包, 能够从图片中解析出突出的颜色, 从而帮助你创建出视觉迷人的应 ...

  5. 《Linux内核设计与实现》学习总结 Chap18

    一.准备开始 1.一个确定的bug,但大部分bug通常都不是行为可靠且定义明确的. 2.一个藏匿bug的内核版本. 3.相关内核代码的知识和运气. 二.内核中的bug 1.bug的表象: 明白无误的错 ...

  6. WEB入门.六 盒子模型

    学习内容 CSS盒子模型 盒子之间的关系 页面元素定位 能力目标 理解盒子模型 理解内容与表现分离的优点 理解并掌握盒子之间的关系 理解并掌握绝对定位与相对定位的用法 本章简介 上一章节中已经讲解了页 ...

  7. Android Log详解(Log.v,Log.d,Log.i,Log.w,Log.e)

    1.Log.v 的调试颜色为黑色的,任何消息都会输出,这里的v代表verbose啰嗦的意思,平时使用就是Log.v("",""); 2.Log.d的输出颜色是蓝 ...

  8. fzyzojP3618 -- [校内训练-互测20180412]士兵的游戏

    二分图匈牙利也可以 判断必须点就看能不能通过偶数长度的增广路翻过去 代码: (最后一个点4s多才行,,,卡不过算了) 开始边数写少了RE,应该是4*N*N M-R随手开了一堆int?都要是long l ...

  9. API Authentication Error: {"error":"invalid_client","message":"Client authentication failed"}

    解决方法:https://github.com/laravel/passport/issues/221 In your oauth_clients table, do the values you h ...

  10. 数据中有NA存在,处理办法

    如果数据中有NA存在,表示这个位置数据遗失,不能进行值的类型描述.也不能用函数来计算,需要计算是可以加上na.rm=T表示忽略NA,但是这个位置并没有去除,使用length可以看到. > x&l ...