zoukankan      html  css  js  c++  java
  • JavaEE框架整合之基于注解的SSH整合

    基于注解的SSH整合

     

    1. 开发环节步骤:

      实体类 -----> DAO开发 -----> Service开发 -----> Action动作类开发 -----> 配置文件(applicationContext.xml)

       

    2. 导包:

      Struts2需要导入的jar:

      asm-3.3.jar
      asm-commons-3.3.jar
      asm-tree-3.3.jar
      commons-fileupload-1.3.1.jar
      commons-io-2.2.jar
      commons-lang3-3.2.jar
      freemarker-2.3.22.jar
      javassist-3.11.0-GA.jar
      log4j-core-2.2.jar
      log4j-api-2.2.jar
      ognl-3.0.6.jar
      struts2-core-2.3.24.jar
      xwork-core-2.3.24.jar

      struts2-spring-plugin-2.3.24.jar
      struts-contention-plugin-2.3.14.jar

      导入struts2的注解开发包:struts-contention-plugin-2.3.14.jar

      |------ 注意: 如果不使用注解开发,千万不要导入这个包

      spring需要导入的jar:

      spring-aop-4.24.RELEASE.jar
      spring-aspectJ-4.24.RELEASE.jar
      spring-beans-4.24.RELEASE.jar
      spring-context-4.24.RELEASE.jar
      spring-core-4.24.RELEASE.jar
      spring-expression-4.24.RELEASE.jar
      spring-jdbc-4.24.RELEASE.jar
      spring-orm-4.24.RELEASE.jar
      spring-test-4.24.RELEASE.jar
      spring-tx-4.24.RELEASE.jar
      spring-web-4.24.RELEASE.jar

      commons-logging-1.2.jar
      com.springsource.org.com.mchange.v2.c3p0-0.9.1.2.jar
      com.springsource.org.aopalliance-1.0.0.jar
      com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

      hibernate需要导入的jar:

      antrl2-2.7.6.jar
      commons-collections-3.1.jar
      dom4j-1.6.1.jar
      geronimo-jpa-2.0-api-1.0.1.Final.jar
      hibernate-core.5.0.7.Final.jar
      hibernate-jpa-2.1.Final.jar
      jandex-2.0.0.Final.jar
      javassist-3.18.1-GA.jar
      jboss-logging-3.3.0.Final.jar

      2.1 配置文件详解见 其他文章

    3. 实体类开发:

      package cn.javabs.entity;
      @Entity
      @Table(name="t_user")
      public class User{

      @Id
      @GeneratedValue(strategy=GenerationType.AUTO)
      private Integer id;
        @Column(name="username",length=50)
        private String username;
        private String password;
        // 此处省略getter和setter方法
         
      }

      在上述代码中:

      @Entity 注解为实体类制定类的路径

      @Id 注解是制定id为主键

      @Generated 注解是为主键制定生成策略

      以上注解实际上代表着hibernate的实体映射文件User.hbm.xml的功能。

      1. DAO开发

      @Repository
      public class UserDaoImpl implementsn UserDao{
        @Autowired
        private HibernateTemplate hibernateTemplate;//提供Hibernate模板
         
        public User findById(Integer id){
            return this.hibernateTemplate.get(User.class,id);
        }
        public List<User> findAll(){
            this.hibenrateTemplate.find("from User");
        }
        public void save(User user){
            this.hibernateTemplate.save(user);
        }
        public void update(User user){
            this.hibernateTemplate.update(user);
        }
        public void delete(User user){
            this.hibernateTemplate.delete(user);
        }
      }
    4. Service开发

      @Service
      public class UserServiceImpl implements UserService{
         
        @Autowired
        private UserDao userDao;
         
        @Transactional
        public void saveUser(User user){
            this.userDao.save(user);
        }
         
          @Transactional
        public void updateUser(User user){
            this.userDao.update(user);
        }
         
          @Transactional
        public void deleteUser(User user){
            this.userDao.delete(user);
        }
         
          @Transactional(readOnly=true)
        public User findUserById(Integer id){
            return this.userDao.findById(id);
        }
         
          @Transactional(readOnly=true)
        public User findAll(){
            return this.userDao.findAll();
        }
         
      }

      在上述代码中,使用@Service 注解用于标注Service层信息

      @Autowired 用于自动注入UserDao接口

      @Transactional注解用于配置事务,此时可以删除掉spring配置文件中的相关的配置信息

       

    5. Action开发

      @Namespace("/")
      @ParentPackage("struts-default")
      @Controller
      public class UserAction extentds ActionSupport implements ModelDriven<User>{
        //封装数据
        private User user = new User();
         
        public User getModel(){
            return user;
        }
         
        @Autowired
        private UserService userService;
         
        @Action(value="user_Action_add",results={@Result(name="add",location="/success.jsp")})
        public String add(){
            userService.saveUser(user);
            return "add";
        }  
      }

      在以上代码中,

      @Namespace 和 @ ParentPackage 注解用于代替 Struts2 配置文件中对action的配置

      @Controller 注解用于Spring 容器中注册UserManagerAction实例

       

    6. applicationContext.xml配置文件开发

        <beans>
        <!--1. 配置扫描-->
        <context:component-scan base-package="cn.javabs"></context:component-scan>

        <!--2.配置SessionFactory-->
        <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <!--2.1 加载hibernate核心配置文件-->
            <property name="configLocation"> value="classpath:hibernate.cfg.xml"></property>
          </bean>

          <!--3.配置hibernate模板-->
          <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
            <!--3.1 通过工厂获取session,操作PO类-->
            <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>

          <!--4.事务管理-->
          <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>

          <!--事务注解驱动-->
          <tx:annotation-driven transaction-manager="txManager"/>

      </beans>

      6.hibernate.cfg.xml开发

      <hibernate-configuration>

      <session-factory>
      <!--1.基本4项-->
      <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
      <property name="connection.url">jdbc:mysql://localhost:3306/user</property>
      <property name="connection.username">root</property>
      <property name="connection.password">sorry</property>
      <!--2.数据库方言-->
      <property name="dialect">org/hibernate.dialect.MySQL5Dialect</property>
      <!--3.配置处理SQL信息-->
      <property name="show_sql">true</property>
      <property name="format_sql">true</property>
      <property name="hbm2ddl_auto">update</property>
      <!--4.取消Bean验证-->
            <property name="show_sql">true</property>
            <!--5.整合 c3p0-->
            <property name="connection.provider_class">
            org.hibernate.connection.C3P0ConnectionProvider
            </property>
             
            <!--6.添加映射信息-->
            <mapping class = "cn.javabs.entity.User"/>
             
             
          </session-factory>
         
      </hibernate-configuration>

       

    基于注解的SSH整合

    1. 开发环节步骤:

      实体类 —–> DAO开发 —–> Service开发 —–> Action动作类开发 —–> 配置文件(applicationContext.xml)

    2. 导包:

      Struts2需要导入的jar:

      asm-3.3.jar
      asm-commons-3.3.jar
      asm-tree-3.3.jar
      commons-fileupload-1.3.1.jar
      commons-io-2.2.jar
      commons-lang3-3.2.jar
      freemarker-2.3.22.jar
      javassist-3.11.0-GA.jar
      log4j-core-2.2.jar
      log4j-api-2.2.jar
      ognl-3.0.6.jar
      struts2-core-2.3.24.jar
      xwork-core-2.3.24.jar
      
      struts2-spring-plugin-2.3.24.jar
      struts-contention-plugin-2.3.14.jar
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17

      导入struts2的注解开发包:struts-contention-plugin-2.3.14.jar

      ​ |—— 注意: 如果不使用注解开发,千万不要导入这个包

      spring需要导入的jar:

      spring-aop-4.24.RELEASE.jar
      spring-aspectJ-4.24.RELEASE.jar
      spring-beans-4.24.RELEASE.jar
      spring-context-4.24.RELEASE.jar
      spring-core-4.24.RELEASE.jar
      spring-expression-4.24.RELEASE.jar
      spring-jdbc-4.24.RELEASE.jar
      spring-orm-4.24.RELEASE.jar
      spring-test-4.24.RELEASE.jar
      spring-tx-4.24.RELEASE.jar
      spring-web-4.24.RELEASE.jar
      
      commons-logging-1.2.jar
      com.springsource.org.com.mchange.v2.c3p0-0.9.1.2.jar
      com.springsource.org.aopalliance-1.0.0.jar
      com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16

      hibernate需要导入的jar:

      antrl2-2.7.6.jar
      commons-collections-3.1.jar
      dom4j-1.6.1.jar
      geronimo-jpa-2.0-api-1.0.1.Final.jar
      hibernate-core.5.0.7.Final.jar
      hibernate-jpa-2.1.Final.jar
      jandex-2.0.0.Final.jar
      javassist-3.18.1-GA.jar
      jboss-logging-3.3.0.Final.jar
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9

      2.1 配置文件详解见 其他文章

      1. 实体类开发:
      package cn.javabs.entity;
      @Entity
      @Table(name="t_user")
      public class  User{
      
          @Id
          @GeneratedValue(strategy=GenerationType.AUTO)
          private Integer id;
          @Column(name="username",length=50)
          private String username;
          private String password;
          // 此处省略getter和setter方法
      
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14

      在上述代码中:

      ​ @Entity 注解为实体类制定类的路径

      ​ @Id 注解是制定id为主键

      ​ @Generated 注解是为主键制定生成策略

      以上注解实际上代表着hibernate的实体映射文件User.hbm.xml的功能。

      1. DAO开发
      @Repository
      public class UserDaoImpl implementsn UserDao{
          @Autowired
          private HibernateTemplate hibernateTemplate;//提供Hibernate模板
      
          public User findById(Integer id){
              return this.hibernateTemplate.get(User.class,id);
          }
          public List<User> findAll(){
              this.hibenrateTemplate.find("from User");
          }
          public void save(User user){
              this.hibernateTemplate.save(user);
          }
          public void update(User user){
              this.hibernateTemplate.update(user);
          }
          public void delete(User user){
              this.hibernateTemplate.delete(user);
          }
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      1. Service开发
      @Service
      public class UserServiceImpl implements UserService{
      
          @Autowired
          private UserDao userDao;
      
          @Transactional
          public void saveUser(User user){
              this.userDao.save(user);
          }
      
           @Transactional
          public void updateUser(User user){
              this.userDao.update(user);
          }
      
           @Transactional
          public void deleteUser(User user){
              this.userDao.delete(user);
          }
      
           @Transactional(readOnly=true)
          public User findUserById(Integer id){
              return this.userDao.findById(id);
          }
      
            @Transactional(readOnly=true)
          public User findAll(){
              return this.userDao.findAll();
          }
      
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29
      • 30
      • 31
      • 32

      在上述代码中,使用@Service 注解用于标注Service层信息

      @Autowired 用于自动注入UserDao接口

      @Transactional注解用于配置事务,此时可以删除掉spring配置文件中的相关的配置信息

      1. Action开发
      @Namespace("/")
      @ParentPackage("struts-default")
      @Controller
      public class UserAction extentds ActionSupport implements ModelDriven<User>{
          //封装数据
          private User user = new User();
      
          public User getModel(){
              return user;
          }
      
          @Autowired
          private UserService userService;
      
          @Action(value="user_Action_add",results={@Result(name="add",location="/success.jsp")})
          public String add(){
              userService.saveUser(user);
              return "add";
          }   
      }
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20

      在以上代码中,

      @Namespace 和 @ ParentPackage 注解用于代替 Struts2 配置文件中对action的配置

      @Controller 注解用于Spring 容器中注册UserManagerAction实例

      1. applicationContext.xml配置文件开发
        <beans>
          <!--1. 配置扫描-->
          <context:component-scan base-package="cn.javabs"></context:component-scan>
      
          <!--2.配置SessionFactory-->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
              <!--2.1 加载hibernate核心配置文件-->
              <property name="configLocation"> value="classpath:hibernate.cfg.xml"></property>
           </bean>
      
           <!--3.配置hibernate模板-->
           <bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
              <!--3.1 通过工厂获取session,操作PO类-->
              <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
      
           <!--4.事务管理-->
           <bean id="txManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
              <property name="sessionFactory" ref="sessionFactory"></property>
           </bean>
      
           <!--事务注解驱动-->
           <tx:annotation-driven transaction-manager="txManager"/>
      
       </beans> 
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25

      6.hibernate.cfg.xml开发

      <hibernate-configuration>
      
          <session-factory>
              <!--1.基本4项-->
              <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
              <property name="connection.url">jdbc:mysql://localhost:3306/user</property>
              <property name="connection.username">root</property>
              <property name="connection.password">sorry</property>
              <!--2.数据库方言-->
              <property name="dialect">org/hibernate.dialect.MySQL5Dialect</property>
              <!--3.配置处理SQL信息-->
              <property name="show_sql">true</property>
              <property name="format_sql">true</property>
              <property name="hbm2ddl_auto">update</property>
              <!--4.取消Bean验证-->
              <property name="show_sql">true</property>
              <!--5.整合 c3p0-->
              <property name="connection.provider_class">
                  org.hibernate.connection.C3P0ConnectionProvider
              </property>
      
              <!--6.添加映射信息-->
              <mapping class = "cn.javabs.entity.User"/>
      
      
           </session-factory>
      
       </hibernate-configuration>
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • 29

    作者: 杨校

    出处: https://blog.csdn.net/kese7952

    分享是快乐的,也见证了个人成长历程,文章大多都是工作经验总结以及平时学习积累,基于自身认知不足之处在所难免,也请大家指正,共同进步。

    本文版权归作者所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出, 如有问题, 可邮件(397583050@qq.com)咨询。

  • 相关阅读:
    [力扣活动] 914. 卡牌分组
    [ 力扣活动0319 ] 409. 最长回文串
    88. 合并两个有序数组
    自己无聊封装一个 LTView
    ios ViewController 页面跳转
    UI打地鼠
    登陆页面,找回密码,注册页面(三个view的切换)
    登陆页面
    UIView 和Label
    对oracle里面clob字段里面xml的增删改查学习
  • 原文地址:https://www.cnblogs.com/xiaoxiao5016/p/9393935.html
Copyright © 2011-2022 走看看