struts2+spring+mybatis 注解式开发

1. 引入依赖

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.12</version>
      <scope>test</scope>
    </dependency>

    <!-- spring相关(context aspects jdbc tx web test)  -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aspects</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>4.3.26.RELEASE</version>
    </dependency>

    <!--struts2相关     struts2-core  struts2-spring-plugin  servlet-api  jstl   fastjsion    -->
    <!-- struts2对spring的支持 -->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-spring-plugin</artifactId>
      <version>2.3.36</version>
    </dependency>
    <!-- struts2核心依赖-->
    <dependency>
      <groupId>org.apache.struts</groupId>
      <artifactId>struts2-core</artifactId>
      <version>2.3.36</version>
    </dependency>
    <!-- servlet-->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>provided</scope>
    </dependency>
    <!-- jstl -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>
    <!-- fastjson -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.58</version>
    </dependency>

    <!--mybatis相关-->
    <!-- mybatis-spring -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.3.2</version>
    </dependency>
    <!-- mybatis -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.1.1</version>
    </dependency>
    <!-- mysql -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.18</version>
    </dependency>
    <!-- 阿里连接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.10</version>
    </dependency>
    <!-- 日志 -->
    <dependency>
      <groupId>org.apache.logging.log4j</groupId>
      <artifactId>log4j-core</artifactId>
      <version>2.0</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.30</version>
    </dependency>

2. 建表

3. 实体

4. 创建Dao

5. Mapper

6. 创建Servise

7. 创建Service实现类ServiceImpl

  • @Service
  • @Transactional

8. 配置spring.xml

    <!--创建数据源-->
    <context:property-placeholder location="classpath:mysql.properties"></context:property-placeholder>
    <bean class="com.alibaba.druid.pool.DruidDataSource" id="druidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${name}"></property>
        <property name="password" value="${password}"></property>
    </bean>

    <!--创建SqlSessionFactory-->
    <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
        <!-- 注入数据源 -->
        <property name="dataSource" ref="druidDataSource"></property>
        <!-- 注入mapper注册 -->
        <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"></property>
        <!-- 注入实体类定义别名 -->
        <property name="typeAliasesPackage" value="entity"></property>
    </bean>

    <!--创建dao扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- sqlSessionFactory-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 扫描的包 -->
        <property name="basePackage" value="dao"></property>
    </bean>

    <!--创建事务管理器-->
    <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
        <!--数据源-->
        <property name="dataSource" ref="druidDataSource"></property>
    </bean>

    <!-- 事务注解的驱动(事务注解生效)-->
    <tx:annotation-driventransactionmanager="transactionManager"/>

    <!-- 创建实例化注解扫描 -->
    <context:component-scan base-package="com.bb"/>

9. 测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:spring.xml"})
public void test(){
  //测试service
}

10. 创建Action

  • @Controller

11. 配置struts.xml

<struts>

    <package name="包名" extends="struts-default" namespace="/">
        <action name="对外访问路径" class="spring容器中的当前action的唯一标识" method="方法名"/>
    </package>

</struts>

12. 配置web.xml

    <!--
            全局参数(创建spring容器需要spring配置文件, 指定配置文件的路径)
        -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring.xml</param-value>
    </context-param>

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--
        监听器
            监听到服务器启动, 就创建spring容器
    -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

13. 部署, 运行

14. 测试

展开阅读全文
©️2019 CSDN 皮肤主题: 深蓝海洋 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读