zoukankan      html  css  js  c++  java
  • Spring学习笔记(六)----SSH框架整合

    SSH 框架整合

    Struts2 和 Spring 整合

    Jar 包:Struts2-spring-plugin 和 struts 相关 jar 包

    public class UserAction extend ActionSupport {
    
        @Override
        public String execute() throws Exception {
            return NONE;
        }
    }
    
    <!-- spring 配置文件 -->
    <!-- c3p0 连接池配置(默认写好) -->
    
    <!-- 配置 Action 的对象 -->
    <bean id="userAction" class="xx.xxx.UserAction" scope="prototype"/>
    
    <!-- struts 配置文件 -->
    <struts>
        <package name="xxx" extends="struts-default" namespace="/">
            <!-- 与 spring 整合后 class 只需要写其 id -->
            <action name="userAction" class"userAction" />
        </package>
    </struts>
    

    Hibernate 和 Spring 整合

    不需要数据库信息的配置

    <!-- c3p0 连接池配置(默认写好) -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" calss="...HibernateTransactionManager">
        <!-- 注入 sessionFactory (也可以注入 dataSource) -->
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!-- 开启事务注解 (同上)-->
    <!-- sessionFactory 创建交给 Spring 管理(Spring 中有封装好的类 -->
    <bean id="sessionFactory" class="...LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 指定 Hibernate 配置文件(无配置文件不需要写这个)-->
        <property name="configLocations" value="classpath:hibernate.cfg.xml"/>
        <!-- 也可以直接将 hibernate 配置写在此处,减少配置文件 -->
        <!-- 以下为无 hibernate 配置文件 示例 -->
        <!-- 配置 hibernate 基本信息 -->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql"> true </prop>
                <prop key="hibernate.format_sql"> true </prop>
                <prop key="hibernate.dialet"> org.hibernate.dialect.MySQLDialect </prop>
            </props>
        </property>
        <!-- 配置映射文件 -->
        <property name="mappingResources">
            <List>
                <value> xin/jeson/entity/User.hbm.xml </value>
                <!-- .... -->
            </List>
        </property>
    </bean>
    

    HibernateTemplate 的使用

    <!-- 包含以上整合内容 -->
    <!-- 创建 HibernateTemplate 对象 -->
    <bean id="hibernateTemplate" class"...">
        <property name="sessionFactory" ref="sessionFactory/>
    </bean>
    
    @Component
    public class User {
        // TODO
    }
    public interface UserDao {
        public void add();
    }
    @Repository
    @Transactional  // 开启事务后正常运行
    public class UserDaoImpl implements UserDao {
        private HibernateTemplate hibernateTemplate;
        @Autowired
        public UserDaoImpl(HibernateTemplate hibernateTemplate) {
            this.hibernateTemplate = hibernateTemplate;
        }
        public void add() {
            // 此时无法成功更新数据库,需要使用事务提交
            hibernateTemplate.save(entity);  // 添加操作
        }
    }
    @Service
    public class UserService {
        private UserDao userDao;
        @Autowired
        public UserService(UserDaoImpl userDaoImpl) {
            this.userDao = userDaoImpl;
        }
    }
    @Controller
    public class UserAction {
        private UserService userService;
        @Autowired
        public UserAction(UserService userService) {
            this.userService = userService;
        }
    }
    
    // Hibernate crud 操作
    // 添加操作
    Serializable save(Object entity)
    // 修改操作
    void update(Object entity)
    // 删除操作
    void delete(Object entity)
    // 查询操作
    <T> T get(Class<T> entityClass, Serializable id)
    <T> T load(Class<T> entityClass, Serializable id)
    List find(Sring queryString, Object... values)
    
    // 示例
    // 1. get 方法:根据 id 查询
    User user = hibernateTemplate.get(User.class, id)
    // 2. find 查询所有 (HQL 语句查询)
    List<User> list = (List<User>)hibernateTemplate.find("from User")
    // 3. find 查询 username = jeson 的语句
    List<User> list = (List<User>)hibernateTemplate.find("from User where username=?", "jeson")
    
    

    Spring 分模块开发

    1. 在 Spring 里面配置多个内容,造成配置混乱,不利于维护
      • 可将部分配置使用单个文件配置
      • 在 Spring 核心文件中引用即可
    // 引用格式
    <!-- 引入其他配置文件 -->
    <import resource="classpath:xx.xml"/>
    
  • 相关阅读:
    视频测试序列的下载地址【转】
    RDO、SAD、SATD、λ相关概念【转】
    RGB、YUV和YCbCr介绍【转】
    H.264和HEVC分析软件和工具【转】
    Mysql——Innodb和Myisam概念与数据恢复
    ubuntu个人初始配置记录
    H.264学习笔记6——指数哥伦布编码
    H.264学习笔记5——熵编码之CAVLC
    C/C++语言学习——内存分配管理
    H.264学习笔记4——变换量化
  • 原文地址:https://www.cnblogs.com/qq188380780/p/11443854.html
Copyright © 2011-2022 走看看