SSH全注解开发:
(1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace、@ParentPackage、@Action...)
1 package com.tongji.actions; 2 3 import org.apache.struts2.convention.annotation.Action; 4 import org.apache.struts2.convention.annotation.Namespace; 5 import org.apache.struts2.convention.annotation.ParentPackage; 6 import org.apache.struts2.convention.annotation.Result; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.context.annotation.Scope; 9 import org.springframework.stereotype.Component; 10 11 import com.tongji.beans.Student; 12 import com.tongji.service.IStudentService; 13 14 @Component 15 @Scope("prototype") 16 @Namespace("/test") 17 @ParentPackage("struts-default") 18 public class RegisterAction { 19 private String name; 20 private int age; 21 22 //声明Service对象 23 @Autowired 24 private IStudentService service; 25 26 public String getName() { 27 return name; 28 } 29 30 public void setName(String name) { 31 this.name = name; 32 } 33 34 public int getAge() { 35 return age; 36 } 37 38 public void setAge(int age) { 39 this.age = age; 40 } 41 42 public IStudentService getService() { 43 return service; 44 } 45 46 public void setService(IStudentService service) { 47 this.service = service; 48 } 49 50 @Action(value="register", [email protected](location="/welcome.jsp")) 51 public String execute() { 52 Student student = new Student(name, age); 53 service.addStudent(student); 54 return "success"; 55 } 56 }
(2) 在 Spring 配置文件中注册组件扫描的基本包 <context:component-scan base-package="com.tongji"/>,在Dao层、Service层和Action层中注解组件和依赖注入,相应地删除在 Spring 配置文件中删除 dao、service、action 的 Bean 定义
1 package com.tongji.dao; 2 3 import java.util.List; 4 5 import org.hibernate.Session; 6 import org.hibernate.SessionFactory; 7 import org.springframework.beans.factory.annotation.Autowired; 8 import org.springframework.stereotype.Repository; 9 10 import com.tongji.beans.Student; 11 12 @Repository("studentDao") //表示当前Dao的Bean的id为studentDao 13 public class StudentDaoHbnImpl implements IStudentDao{ 14 15 @Autowired 16 private SessionFactory sessionFactory; 17 18 public void setSessionFactory(SessionFactory sessionFactory) { 19 this.sessionFactory = sessionFactory; 20 } 21 22 @Override 23 public void insertStudent(Student student) { 24 sessionFactory.getCurrentSession().save(student); 25 } 26 27 @Override 28 public void deleteStudent(int id) { 29 Student student = sessionFactory.getCurrentSession().get(Student.class, id); 30 //Student student = this.selectStudentById(id); 31 sessionFactory.getCurrentSession().delete(student); 32 } 33 34 @Override 35 public void updateStudent(Student student) { 36 sessionFactory.getCurrentSession().update(student); 37 } 38 39 @Override 40 public String selectStudentNameById(int id) { 41 // Student student = this.selectStudentById(id); 42 // if (student != null) { 43 // return student.getName(); 44 // } 45 // return null; 46 47 String hql = "select name from Student where id= :id"; 48 String name = (String) sessionFactory.getCurrentSession().createQuery(hql). 49 setInteger("id", id). 50 uniqueResult(); 51 return name; 52 } 53 54 @Override 55 public List<String> selectStudentNames() { 56 String hql = "select name from Student"; 57 return sessionFactory.getCurrentSession().createQuery(hql).list(); 58 } 59 60 @Override 61 public Student selectStudentById(int id) { 62 return sessionFactory.getCurrentSession().get(Student.class, id); 63 } 64 65 @Override 66 public List<Student> selectStudents() { 67 String hql = "from Student"; 68 return sessionFactory.getCurrentSession().createQuery(hql).list(); 69 } 70 71 }
(3) 在 Spring 配置文件中开启 Spring 事务注解驱动 <tx:annotation-driven transaction-manager="transactionManager"/>,使用 Spring 的事务注解管理事务
1 package com.tongji.service; 2 3 import java.util.List; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.stereotype.Service; 7 import org.springframework.transaction.annotation.Transactional; 8 9 import com.tongji.beans.Student; 10 import com.tongji.dao.IStudentDao; 11 12 @Service("studentService") //表示当前Service的Bean的id为studentService 13 public class StudentServiceImpl implements IStudentService { 14 15 @Autowired 16 private IStudentDao dao; 17 18 public void setDao(IStudentDao dao) { 19 this.dao = dao; 20 } 21 22 @Override 23 @Transactional 24 public void addStudent(Student student) { 25 dao.insertStudent(student); 26 } 27 28 @Override 29 @Transactional 30 public void removeStudent(int id) { 31 dao.deleteStudent(id); 32 } 33 34 @Override 35 @Transactional 36 public void modifyStudent(Student student) { 37 dao.updateStudent(student); 38 } 39 40 @Override 41 @Transactional(readOnly=true) 42 public String findStudentNameById(int id) { 43 return dao.selectStudentNameById(id); 44 } 45 46 @Override 47 @Transactional(readOnly=true) 48 public List<String> findStudentNames() { 49 return dao.selectStudentNames(); 50 } 51 52 @Override 53 @Transactional(readOnly=true) 54 public Student findStudentById(int id) { 55 return dao.selectStudentById(id); 56 } 57 58 @Override 59 @Transactional(readOnly=true) 60 public List<Student> findStudents() { 61 return dao.selectStudents(); 62 } 63 64 }
(4) 删除Hibernate映射文件,在实体类中以注解的方式表明映射关系,并在 Spring 配置文件的 SessionFactory 定义中,删除映射文件的相关配置mappingResources,添加实体包扫描器packagesToScan。
1 package com.tongji.beans; 2 3 import javax.persistence.Entity; 4 import javax.persistence.GeneratedValue; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 import org.hibernate.annotations.GenericGenerator; 9 10 @Entity 11 @Table 12 public class Student { 13 @Id 14 @GenericGenerator(name="xxx", strategy="native") 15 @GeneratedValue(generator="xxx") 16 private Integer id; 17 private String name; 18 private int age; 19 20 public Integer getId() { 21 return id; 22 } 23 24 public void setId(Integer id) { 25 this.id = id; 26 } 27 28 public String getName() { 29 return name; 30 } 31 32 public void setName(String name) { 33 this.name = name; 34 } 35 36 public int getAge() { 37 return age; 38 } 39 40 public void setAge(int age) { 41 this.age = age; 42 } 43 44 public Student() { 45 super(); 46 } 47 48 public Student(String name, int age) { 49 super(); 50 this.name = name; 51 this.age = age; 52 } 53 54 @Override 55 public String toString() { 56 return "Student [id=" + id + ", name=" + name + ", age=" + age + "]"; 57 } 58 59 }
Spring配置文件:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xsi:schemaLocation=" 8 http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/tx 13 http://www.springframework.org/schema/tx/spring-tx.xsd 14 http://www.springframework.org/schema/aop 15 http://www.springframework.org/schema/aop/spring-aop.xsd"> 16 17 <!-- 注册数据源:C3P0数据源 --> 18 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 19 <property name="driverClass" value="${jdbc.driverClass}" /> 20 <property name="jdbcUrl" value="${jdbc.url}" /> 21 <property name="user" value="${jdbc.user}" /> 22 <property name="password" value="${jdbc.password}" /> 23 </bean> 24 25 <!-- 注册JDBC属性文件 --> 26 <context:property-placeholder location="classpath:jdbc.properties"/> 27 28 <!-- 参考hibernate的主配置文件来配置 --> 29 <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 30 <property name="dataSource" ref="dataSource"/> 31 <property name="hibernateProperties"> 32 <props> 33 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 34 <prop key="hibernate.current_session_context_class"> org.springframework.orm.hibernate5.SpringSessionContext</prop> 35 <prop key="hibernate.hbm2ddl.auto">update</prop> 36 <prop key="hibernate.show_sql">true</prop> 37 <prop key="hibernate.format_sql">true</prop> 38 </props> 39 </property> 40 <property name="packagesToScan" value="com.tongji.beans"/> 41 </bean> 42 43 <context:component-scan base-package="com.tongji"/> 44 45 <!-- 注册事务管理器 --> 46 <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 47 <property name="sessionFactory" ref="sessionFactory"/> 48 </bean> 49 50 <!-- 事务注解驱动 --> 51 <tx:annotation-driven transaction-manager="transactionManager"/> 52 </beans>
至此,全注解开发完成。
时间: 2024-12-02 22:46:14