我是靠谱客的博主 超级黄蜂,最近开发中收集的这篇文章主要介绍spring(七)—struts2+spring+mybatis 注解式开发,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

struts2+spring+mybatis 注解式开发

    • 1. 引入依赖
    • 2. 建表
    • 3. 实体
    • 4. 创建Dao
    • 5. Mapper
    • 6. 创建Servise
    • 7. 创建Service实现类ServiceImpl
    • 8. 配置spring.xml
    • 9. 测试
    • 10. 创建Action
    • 11. 配置struts.xml
    • 12. 配置web.xml
    • 13. 部署, 运行
    • 14. 测试

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. 测试

最后

以上就是超级黄蜂为你收集整理的spring(七)—struts2+spring+mybatis 注解式开发的全部内容,希望文章能够帮你解决spring(七)—struts2+spring+mybatis 注解式开发所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部