基于注解的SSH整合

  1. 开发环节步骤:

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

  2. 导包:

    Struts2需要导入的jar:

    asm-3.3.jarasm-commons-3.3.jarasm-tree-3.3.jarcommons-fileupload-1.3.1.jarcommons-io-2.2.jarcommons-lang3-3.2.jarfreemarker-2.3.22.jarjavassist-3.11.0-GA.jarlog4j-core-2.2.jarlog4j-api-2.2.jarognl-3.0.6.jarstruts2-core-2.3.24.jarxwork-core-2.3.24.jar​struts2-spring-plugin-2.3.24.jarstruts-contention-plugin-2.3.14.jar​

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

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

    spring需要导入的jar:

    spring-aop-4.24.RELEASE.jarspring-aspectJ-4.24.RELEASE.jarspring-beans-4.24.RELEASE.jarspring-context-4.24.RELEASE.jarspring-core-4.24.RELEASE.jarspring-expression-4.24.RELEASE.jarspring-jdbc-4.24.RELEASE.jarspring-orm-4.24.RELEASE.jarspring-test-4.24.RELEASE.jarspring-tx-4.24.RELEASE.jarspring-web-4.24.RELEASE.jar​commons-logging-1.2.jarcom.springsource.org.com.mchange.v2.c3p0-0.9.1.2.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

    hibernate需要导入的jar:

    antrl2-2.7.6.jarcommons-collections-3.1.jardom4j-1.6.1.jargeronimo-jpa-2.0-api-1.0.1.Final.jarhibernate-core.5.0.7.Final.jarhibernate-jpa-2.1.Final.jarjandex-2.0.0.Final.jarjavassist-3.18.1-GA.jarjboss-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开发

    @Repositorypublic 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开发

    @Servicepublic 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")@Controllerpublic 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.jarstruts2-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.jarcommons-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{@Autowiredprivate 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{@Autowiredprivate UserDao userDao;@Transactionalpublic void saveUser(User user){this.userDao.save(user);}@Transactionalpublic void updateUser(User user){this.userDao.update(user);}@Transactionalpublic 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;}@Autowiredprivate 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)咨询。

JavaEE框架整合之基于注解的SSH整合相关推荐

  1. SSH整合,非常详细的SSH整合

    SSH整合,非常详细的SSH整合 这是一个eclipse基于maven整合的struts2-spring-hibernate的简单项目, 1,首先创建一个maven项目如下:名字可以自己命名 , 创建 ...

  2. ssh框架可以使用Ajax,基于Ajax的SSH框架的应用研究

    摘要: 随着基于Web的应用逐渐增多,如何选择适当的开发框架提高开发效率,提高系统的重用性和可移植性,并且提升Web应用的用户体验,这些问题成为热点.Ajax技术的异步机制,使得页面可以局部刷新,提高 ...

  3. 基于注解的SpringMVC整合JPA

    点我下载工程代码 实体类 Department package com.sj.bean; import java.util.Set; import javax.persistence.CascadeT ...

  4. 使用注解实现ssh整合

    1.搭建项目: 项目名称:ss1 2.添加jar包     1).struts 2.3.7          --基础         asm-3.3.jar         asm-commons- ...

  5. spring整合mybatis基于注解

    数据库 /* Navicat MySQL Data Transfer Source Server         : mysql Source Server Version : 50549 Sourc ...

  6. JAVAEE框架之Spring新注解

    五.Spring5 新注解 ​ 新的注解有用,为后面的SpringBoot可以打一个基础,因为到SpringBoot的课程, 多了很多注解.这时候,先了解一部分. 5.1 @Configuration ...

  7. JAVAEE框架技术之10-myBatis注解式开发

    Mybatis注解开发 注解开发只需要写mapper接口即可,无需再书写对应的xxMapper.xml映射文件 对于单表操作还是比较方便的,但对多表操作就维护起来就比较麻烦了 数据库准备 添加依赖 & ...

  8. java切面类整合_自定义注解+面向切面整合的日志记录模块(一)

    java中的常见注解 jdk的自带注解 @Override:告诉编译器我重写了接口方法 @Deprecated:告诉编译器这个方法过时了,不建议使用,Ide会在方法上划横线 @SuppressWarn ...

  9. SSH框架整合——基于注解

    SSH框架整合--基于注解 @(Spring)[Spring, hibernate, struts2, 框架整合] SSH框架整合基于注解 SSH框架整合 第一步导入Jar包 第二步导入配置文件 第三 ...

最新文章

  1. PingCode 全新子产品Access (目录服务)正式发布!
  2. Leetcode-探索 | 旋转数组
  3. BLE 安全之虫洞攻击
  4. 凭运气接来的项目,怎样凭本事搞砸?
  5. android 游戏引擎 排行_4月旗舰手机性能排行榜:小米10 Pro第四,OPPO Ace2第五
  6. 【AutoML白皮书】:感知、认知、决策算法布局提升企业决策水平.pdf(附下载链接)...
  7. SSM项目连接远程Linux服务器的mysql 启动tomcat卡在了 Initializing Spring root WebApplicationContext...
  8. 在将计算机技术应用于会计工作的初期,所开发的会计核算软件主要用于,2013年会计从业考试《电算化》会计核算软件...
  9. Observium Feature分析
  10. tcpdf 使用总结
  11. 关于Bandicam使用心得
  12. CC2530+74HC164矩阵键盘的设计
  13. 吉林大学计算机系高级语言程序设计(C语言)期末题目及解答(上)
  14. ps切图的零散小知识
  15. mysql 查看slave状态_解读show slave status 命令判断MySQL复制同步状态
  16. 计算机代码,名词解释和作用,还有我们要高高飞起来喔!
  17. 在python中读取npz文件
  18. Market-1501和DukeMTMC行人属性解析
  19. IT求职宝典——微软十佳金牌讲师孔文达作序推荐!
  20. Java使用BouncyCastle进行基于ECDSA算法的椭圆曲线secp256r1证书自签名

热门文章

  1. AI一分钟 | 妈呀!连地铁都开始无人驾驶了,飞机还远吗;北京无人驾驶新规出台,终于知道李彦宏该不该被罚了(12月19日)
  2. 能解决80%故障的排查思路
  3. MySQL太细碎了,我硬生生捋出了一条核心大主线!
  4. API 网关从入门到放弃
  5. 两个小模型就能吊打大模型!北大校友、谷歌华人一作「模型集合」,CNN、Transformer都适用!...
  6. 总结 | 机器学习的通俗讲解!
  7. 小心!你下载的机器学习工具包可能是病毒:CuPy被掉包,官方一天后才发现
  8. TinaFace:人脸检测新纪录
  9. 让 PyTorch 更轻便,这款深度学习框架你值得拥有!在 GitHub 上斩获 6.6K 星
  10. ubuntu 对apahce的php 服务器使用