• SSH注解整合


    首先是实体类中的注解:

    @Entity
    @Table(name = "Stock")
    public class Stock {
        @Id //标识实体中ID和底层数据表的主键统一
        @GeneratedValue(strategy = GenerationType.IDENTITY)   //主键生成策略:mysql 底层自动autoincreament
        private int sid;     //股票编号
        private String sname;//股票名称   YYM教育
        @Column(name="scount")
        private int scount;  //股数
    
        public int getSid() {
            return sid;
        }
    
        public void setSid(int sid) {
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public int getScount() {
            return scount;
        }
    
        public void setScount(int scount) {
            this.scount = scount;
        }
    }

    然后就是dao层中注解:

    @Repository("stockDAO")
    public class StockDAOImpl implements IStockDAO {
        //dao植入SessionFactory变量  重量级
        //使用自动装配让SessionFactory
        @Autowired
        private SessionFactory sessionFactory;
        public int addStock(Stock stock) {
            System.out.println(sessionFactory+"========================");
            Session session = sessionFactory.getCurrentSession();
            Serializable count=session.save(stock);
            Integer cc=(Integer)count;
            return cc.intValue();
        }
    
    }

    之后就是service中的注解:

    @Service("stockService")
    public class StockServiceImpl implements IStockService {
        //植入dao的接口变量
        @Resource(name = "stockDAO")
        private IStockDAO dao;
        @Transactional
        public int saveStock(Stock stock) {
            return dao.addStock(stock);
        }
    }

    然后就是action连接service

    @Controller
    @Scope(value = "prototype")
    @ParentPackage("struts-default")
    @Namespace("/")
    public class StockAction extends ActionSupport implements ModelDriven<Stock> {
       //Entity变量
        private Stock stock;
        @Resource(name = "stockService")
        private IStockService service;
    
        public StockAction(){
            stock=new Stock();
        }
        @Action(value = "addStock",results = {@Result( name = "success",location = "/success.jsp"),@Result(name="input",location = "/add.jsp")})
        public String execute() throws Exception {
            int count = service.saveStock(stock);
            if (count>0) {
                return SUCCESS;
            }else {
                return INPUT;
            }
        }
        public Stock getModel() {
            return stock;
        }
    }
     <!--开启包扫描器-->
        <context:component-scan base-package="cn.wwy"></context:component-scan>
        <!--1.Datasource-->
        <!--1.配置数据源c3p0-->
        <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
            <property name="driverClass" value="${jdbc.driverClassName}"/>
            <property name="user" value="${jdbc.username}"/>
            <property name="password" value="${jdbc.password}"/>
            <property name="jdbcUrl" value="${jdbc.url}"/>
        </bean>
    
        <!--jdbc.properties-->
        <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    
        <!--2.SessionFactory         类:Local-->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
              <property name="hibernateProperties">
                  <props>
                       <!--hibernate.xxxxxx必须以hibernate-->
                      <prop key="hibernate.show_sql">true</prop>
                      <prop key="hibernate.format_sql">true</prop>
                      <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
                      <!--with current thread bind session-->
                      <prop key="hibernate.current_session_context_class">org.springframework.orm.hibernate5.SpringSessionContext</prop>
                  </props>
              </property>
              <property name="packagesToScan" value="cn.wwy.beans"></property>
        </bean>
    
        <!--5.事务管理器-->
        <bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory"></property>
        </bean>
        <!--6.事务-->
        <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
    </beans>

    pom.xml文件

     <!--jta的jar包-->
        <dependency>
          <groupId>javax.transaction</groupId>
          <artifactId>jta</artifactId>
          <version>1.1</version>
        </dependency>
    
        <!--mysql数据库驱动-->
        <dependency>
          <groupId>org.wisdom-framework</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.34_1</version>
        </dependency>
    
        <dependency>
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>5.0.6.Final</version>
        </dependency>
    
        <!--以上是HIbernate的jar包配置-->
    
        <!--下面是Spring相关的jar包配置-->
        <!--c3p0-->
        <dependency>
          <groupId>com.mchange</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.5.2</version>
        </dependency>
    
        <!--AOP的相关jar-->
        <dependency>
          <groupId> org.aspectj</groupId >
          <artifactId> aspectjweaver</artifactId >
          <version> 1.8.7</version >
        </dependency>
    
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-context</artifactId>
          <version>4.2.0.RELEASE</version>
        </dependency>
    
        <!--Spring-ORM-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-orm</artifactId>
          <version> 4.2.2.RELEASE</version>
        </dependency>
    
        <!--以下是Struts2的jar包-->
        <!--1.获取ServletAPI-->
        <dependency>
          <groupId>javaee</groupId>
          <artifactId>javaee-api</artifactId>
          <version>5</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-core</artifactId>
          <version>2.3.4.1</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.struts.xwork</groupId>
          <artifactId>xwork-core</artifactId>
          <version>2.3.4.1</version>
        </dependency>
    
        <!--Spring-web-->
        <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-web</artifactId>
          <version>4.1.8.RELEASE</version>
        </dependency>
    
        <!--Struts整合Spring的jar包-->
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-spring-plugin</artifactId>
          <version>2.3.4.1</version>
        </dependency>
    
        <dependency>
          <groupId>org.apache.struts</groupId>
          <artifactId>struts2-convention-plugin</artifactId>
          <version>2.3.4.1</version>
        </dependency>
    
      </dependencies>
    
      <build>
        <resources>
          <resource>
            <directory>src/main/java</directory>
            <includes>
              <include>**/*.xml</include>
            </includes>
          </resource>
        </resources>
      </build>
    </project>
    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
      </context-param>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
    
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
      </filter-mapping>
      <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
    </web-app>
  • 相关阅读:
    开淘店记录
    广告轮播效果
    loading事件加载效果
    正则表达式摘要
    cookie存取数据分析
    js 空格与回车处理
    数据对象型转换为数组型
    变换闪烁效果
    eclipse配置新环境
    五小时轻松入门Python
  • 原文地址:https://www.cnblogs.com/xiaoyu1997/p/6693912.html
Copyright © 2020-2023  润新知