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),而今天我们就是来简化 ...
随机推荐
- BZOJ5308 ZJOI2018胖
贝尔福特曼(?)的方式相当于每次将所有与源点直接相连的点的影响区域向两边各扩展一格.显然每个点在过程中最多更新其他点一次且这些点构成一段连续区间.这个东西二分st表查一下就可以了.注意某一轮中两点都更 ...
- 一些$LCT$的瓜皮题目
一些瓜皮 放几个比较优(she)秀(pi)的\(LCT\)题. 老惯例,每一题代码因为一些未知原因消失了(如果要的话私我好了,虽然会咕咕咕). 嘴巴\(AC\)真香! [SP16580] QTREE7 ...
- 【NOI 2018】归程(Kruskal重构树)
题面在这里就不放了. 同步赛在做这个题的时候,心里有点纠结,很容易想到离线的做法,将边和询问一起按水位线排序,模拟水位下降,维护当前的各个联通块中距离$1$最近的距离,每次遇到询问时输出所在联通块的信 ...
- 解题:BZOJ 4644 经典砂比题(雾
题面 初见线段树分治 (对我来说可不是什么经典题=.=) 把时间轴建出来一棵线段树,然后在对应的区间上打标记,最后把整棵树DFS一遍,到叶节点输出答案即可 (把最终答案开成全局的了调了半天 #incl ...
- 简单版AC自动机
简单版\(AC\)自动机 学之前听别人说起一直以为很难,今天学了简单版的\(AC\)自动机,感觉海星,只要理解了\(KMP\)一切都好说. 前置知识:\(KMP\)(有链接) 前置知识:\(Trie\ ...
- xampp+vscode开发php的配置流程
一.所需文件 1.xampp集成服务器(个人使用7.1.7)https://www.apachefriends.org/download.html 2.vscode https://code.visu ...
- Python【读写Json文件】
indent=10:缩进10个空格
- vue项目post请求405报错解决办法。
步骤一: 确定ajax语法没有错误. 步骤二: 与后台对接确认请求是否打到nginx上? 步骤三: 检查nginx是否配置了事件转发,比如我们的接口是在,当前地址的8100端口上,并且接口地址上有v1 ...
- SMO详解
转自:简书https://www.jianshu.com/p/55458caf0814 SVM通常用对偶问题来求解,这样的好处有两个:1.变量只有N个(N为训练集中的样本个数),原始问题中的变量数量与 ...
- JS正则表达式验证手机号和邮箱
一.验证手机号 function isPoneAvailable(poneInput) { var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; if (!myreg.test( ...