Spring笔记13--SSH--全注解开发
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--全注解开发的更多相关文章
- SSH全注解开发
web.xml: <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=&quo ...
- 20.SSM整合-全注解开发
全注解开发 1.将SpringMVC改为注解 修改spring-mvc.xml 2.将Spring改为注解 将Service改为注解,完成Dao的注入 将事务以注解方式织入到Service 1.修改s ...
- spring boot + vue + element-ui全栈开发入门——开篇
最近经常看到很多java程序员朋友还在使用Spring 3.x,Spring MVC(struts),JSP.jQuery等这样传统技术.其实,我并不认为这些传统技术不好,而我想表达的是,技术的新旧程 ...
- spring boot + vue + element-ui全栈开发入门——基于Electron桌面应用开发
前言 Electron是由Github开发,用HTML,CSS和JavaScript来构建跨平台桌面应用程序的一个开源库. Electron通过将Chromium和Node.js合并到同一个运行时环 ...
- spring boot + vue + element-ui全栈开发入门
今天想弄弄element-ui 然后就在网上找了个例子 感觉还是可以用的 第一步是完成了 果断 拿过来 放到我这里这 下面直接是连接 点进去 就可以用啊 本想着不用vue 直接导入连接 ...
- spring+hibernate+Struts2 整合(全注解及注意事项)
最近帮同学做毕设,一个物流管理系统,一个点餐系统,用注解开发起来还是很快的,就是刚开始搭环境费了点事,今天把物流管理系统的一部分跟环境都贴出来,有什么不足的,请大神不吝赐教. 1.结构如下 2.jar ...
- spring boot + vue + element-ui全栈开发入门——spring boot后端开发
前言 本文讲解作为后端的spring boot项目开发流程,如果您还不会配置spring boot环境,就请点击<玩转spring boot——快速开始>,如果您对spring boot还 ...
- 学习笔记之Python全栈开发/人工智能公开课_腾讯课堂
Python全栈开发/人工智能公开课_腾讯课堂 https://ke.qq.com/course/190378 https://github.com/haoran119/ke.qq.com.pytho ...
- Spring学习04(使用注解开发)
7.使用注解开发 说明:在spring4之后,想要使用注解形式,必须得要引入aop的包. 在配置文件当中,还得要引入一个context约束 <?xml version="1.0&quo ...
- spring boot整合mybatis基于注解开发以及动态sql的使用
让我们回忆一下上篇博客中mybatis是怎样发挥它的作用的,主要是三类文件,第一mapper接口,第二xml文件,第三全局配置文件(application.properties),而今天我们就是来简化 ...
随机推荐
- 一文看尽HashMap
前言 日常开发中,经常会使用到JDK自带的集合类:List.Set.Map三者的实现,ArrayList.LinkedList.HashSet.TreeSet.HashMap.TreeMap等.其中L ...
- Valid Parentheses - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Valid Parentheses - LeetCode 注意点 考虑输入为空的情况 解法 解法一:如果是'('.'{'.'['这三者就入栈,否则就判断栈 ...
- BZOJ2436 [Noi2011]Noi嘉年华 【dp】
题目链接 BZOJ2436 题解 看这\(O(n^3)\)的数据范围,可以想到区间\(dp\) 发现同一个会场的活动可以重叠,所以暴力求出\(num[l][r]\)表示离散化后\([l,r]\)的完整 ...
- 单点登录(十八)----cas4.2.x客户端增加权限控制shiro
我们在上面章节已经完成了cas4.2.x登录启用mongodb的验证方式. 单点登录(十三)-----实战-----cas4.2.X登录启用mongodb验证方式完整流程 也完成了获取管理员身份属性 ...
- java之初学线程
线程 学习线程相关的笔记,前面写过关于很多线程的使用,有兴趣的可以去了解下 线程 概念理解 并发 : 指两个或多个事件在同一个时间段内发生(交替执行). 并行 : 指两个或多个事件在同一时刻发生(同时 ...
- Android 图片加载框架 Glide 的用法
https://github.com/bumptech/glide Android图片加载框架最全解析(一),Glide的基本用法http://blog.csdn.net/guolin_blog/ar ...
- js正则取出一个字符串小括号中的内容
var aa="ldfjsldfj(dsfasjfj3124123)"; var result = aa.match(/\(([^)]*)\)/); // 此时result=[&q ...
- Hibernate中常见问题 No row with the given identifier exists问题
收集:Hibernate中常见问题 No row with the given identifier exists问题的原因及解决 2007年11月21日 15:02:00 eyejava 阅读数:2 ...
- 六、java异常处理
目录 一.异常的概念 二.异常的分类 三.异常的捕获和处理 四.使用自定义异常 一.异常的概念 java异常是指java提供的用于处理程序运行过程中错误的一种机制 所谓错误是指在程序运行的过程中发生的 ...
- JS获取当前时间并格式化"yyyy-MM-dd HH:mm:ss"
先来看下JS中的日期操作: var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年 ...