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. ACdream1092

    题意是给出某个地鼠的出现位置以及出现时间,人有一个移动速度,求此人最多可以打多少个地鼠? 我们根据时间把所有的地鼠排序,如果两个地鼠之间的距离不超过时间只差与速度的乘积,那说明打完上一只地鼠还可以打到 ...

  2. 【uoj#213】[UNR #1]争夺圣杯 单调栈+差分

    题目描述 给出一个长度为 $n$ 的序列,对于 $1\sim n$ 的每一个数 $i$ ,求这个序列所有长度为 $i$ 的子区间的最大值之和,输出每一个 $i$ 的答案模 $998244353$ 后异 ...

  3. Ubuntu修改中文目录为英文

    1.安装需要的软件 sudo apt install xdg-user-dirs-gtk 2.临时转换系统语言为英文,重启后会自动恢复原值的 export LANG=en_US 3.执行转换命令,弹出 ...

  4. MT【124】利用柯西求最值

    已知 \(a\) 为常数,函数\(f(x)=\dfrac{x}{\sqrt{a-x^2}-\sqrt{1-x^2}}\) 的最小值为\(-\dfrac{2}{3}\),则 \(a\) 的取值范围___ ...

  5. 洛谷 P3190 [HNOI2007]神奇游乐园 解题报告

    P3190 [HNOI2007]神奇游乐园 Description 给你一个 \(m * n\) 的矩阵,每个矩阵内有个权值\(V(i,j)\) (可能为负数),要求找一条回路,使得每个点最多经过一次 ...

  6. 洛谷 P1783 海滩防御 解题报告

    P1783 海滩防御 题目描述 WLP同学最近迷上了一款网络联机对战游戏(终于知道为毛JOHNKRAM每天刷洛谷效率那么低了),但是他却为了这个游戏很苦恼,因为他在海边的造船厂和仓库总是被敌方派人偷袭 ...

  7. Mysql分页显示

    第一部分:看一下分页的基本原理:   mysql explain SELECT * FROM message ORDER BY id DESC LIMIT 10000, 20************* ...

  8. DataTables实现rowspan思路

    直接看例子吧 <table id="example" class="display table table-bordered" cellspacing=& ...

  9. 【题解】CF1154

    A Description 有三个正整数 \(a,~b,~c\),现在给定 \(x_1~=~a + b,~x_2~=~a + c, x_3~=~b + c, ~x_4~=~a + b + c\),请求 ...

  10. 关于表单中Readonly和Disabled

    Readonly和Disabled是用在表单中的两个属性,它们都能够做到使用户不能够更改表单域中的内容.但是它们之间有着微小的差别,总结如下: Readonly只针对input(text / pass ...