SSH中的注解式整合

SSH中的注解式整合

原创地址为 https://blog.csdn.net/caoyujiao520/article/details/68067138

前言


因为接触配置文件比较多,所以对于我来说更喜欢配置文件,把需要配置的内容全部整到一个文件中,对于注解就显得很陌生。《Java注解基本知识》


Annotation(注解)就是Java提供了一种元程序中的元素关联任何信息和着任何元数据(metadata)的途径和方法。Annotion(注解)是一个接口,程序可以通过反射来获取指定程序元素的Annotion对象,然后通过Annotion对象来获取注解里面的元数据。


Entity注解



  
  
  1. //定义实体类
  2. @Entity
  3. //定义数据库中表名
  4. @Table(name= "cst_customer")
  5. public class Customer implements Serializable {
  6. //定义id主键字段名称
  7. @Id
  8. @GeneratedValue(strategy=GenerationType.IDENTITY)
  9. @Column(name= "cust_id")
  10. private Long custId;
  11. @Column(name= "cust_name")
  12. private String custName;
  13. @Column(name= "cust_source")
  14. private String custSource;
  15. /*get set方法*/
  16. }

扫描二维码关注公众号,回复: 5354548 查看本文章


Spring配置文件注解:将Hibernate的功能:连接数据库配置数据源接管过来。



  
  
  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:aop= "http://www.springframework.org/schema/aop"
  5. xmlns:tx= "http://www.springframework.org/schema/tx"
  6. xmlns:context= "http://www.springframework.org/schema/context"
  7. xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  8. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
  9. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
  10. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean definitions here -->
  11. <!-- 配置spring运行要扫描的包 -->
  12. <context:component-scan base-package="com.itheima"> </context:component-scan>
  13. <!-- 开启spring对注解事务的支持 -->
  14. <tx:annotation-driven transaction-manager="transactionManager"/>
  15. <!-- 配置HibernateTemplate -->
  16. <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
  17. <!-- 注入SessionFactory -->
  18. <property name="sessionFactory" ref="sessionFactory"> </property>
  19. </bean>
  20. <!-- 配置事务管理器 -->
  21. <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
  22. <!-- 注入SessionFactory -->
  23. <property name="sessionFactory" ref="sessionFactory"> </property>
  24. </bean>
  25. <!-- 配置SessionFactory -->
  26. <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
  27. <!-- 连接数据库信息 -->
  28. <property name="dataSource" ref="dataSource"> </property>
  29. <!-- hibernate基本配置 -->
  30. <property name="hibernateProperties">
  31. <props>
  32. <!-- 数据库方言 -->
  33. <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect </prop>
  34. <!-- 是否显示sql语句 -->
  35. <prop key="hibernate.show_sql">true </prop>
  36. <!-- 是否格式化sql语句 -->
  37. <prop key="hibernate.format_sql">true </prop>
  38. <!-- 采用何种方式生成数据库表结构 -->
  39. <prop key="hibernate.hbm2ddl.auto">update </prop>
  40. <!-- 把session绑定到当前线程上 -->
  41. <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext </prop>
  42. </props>
  43. </property>
  44. <!-- 映射文件位置 -->
  45. <property name="packagesToScan">
  46. <array>
  47. <value>com.itheima.domain </value>
  48. </array>
  49. </property>
  50. </bean>
  51. <!-- 配置数据源 -->
  52. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  53. <property name="driverClass" value="com.mysql.jdbc.Driver"> </property>
  54. <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/day47_ee48_ssh"> </property>
  55. <property name="user" value="root"> </property>
  56. <property name="password" value="123456"> </property>
  57. </bean>
  58. </beans>


Struts配置文件注解:原来配置在此的内容,现在分布在Action Service Dao层



  
  
  1. <!-- 开启开发者模式 -->
  2. <constant name="struts.devMode" value="true"> </constant>


Action注解



  
  
  1. /*
  2. * 客户动作类
  3. */
  4. @Controller( "customerAction") //控制层value
  5. @Scope( "prototype") //多例
  6. @ParentPackage( "struts-default") //默认父包
  7. @Namespace( "/customer") //命名空间
  8. public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
  9. private Customer customer = new Customer();
  10. @Autowired //对类成员变量、方法及构造函数进行标注,完成自动装配的工作,消除 set,get方法
  11. private ICustomerService customerService;
  12. /*
  13. * 模型驱动
  14. */
  15. @Override
  16. public Customer getModel() {
  17. return customer;
  18. }
  19. /*
  20. * 获取添加客户页面
  21. * value:action名称 results:配置返回结果集属性
  22. * @Result:配置返回具体结果 name:return中的String对应的 type:路径跳转方式 location:跳转路径
  23. */
  24. @Action(value= "addUICustomer",results={
  25. @Result(name= "addUICustomer",type= "dispatcher",location= "/jsp/customer/add.jsp")
  26. })
  27. public String addUICustomer(){
  28. return "addUICustomer";
  29. }
  30. }

Service层



  
  
  1. /**
  2. * 客户的业务层实现类
  3. */
  4. @Service( "customerService") //Service层
  5. //配置事务,如果没有事务则开启一个事务
  6. @Transactional(readOnly= false,propagation=Propagation.REQUIRED)
  7. public class CustomerServiceImpl implements ICustomerService {
  8. /*与Action一致,调用dao层*/
  9. }

Dao层



  
  
  1. /**
  2. * 客户的持久层实现类
  3. */
  4. @Repository( "customerDao")
  5. public class CustomerDaoImpl implements ICustomerDao {
  6. /*操作数据库*/
  7. }



猜你喜欢

转载自blog.csdn.net/Alone5256/article/details/87455981