关闭

Spring+Struts2+Mybatis在Intellij下的整合

标签: struts2+mybatis整合Spring整合MybatisMybatis的配置intellij下整个Struts2My
266人阅读 评论(0) 收藏 举报
分类:

环境:jdk 1.8,      tomcat 8.0.38;

开发工具:Intellj;

具体步骤:

1. 创建一个maven下的web项目

2. 配置相应的jar文件,具体的配置方式为:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts2-core</artifactId>
        <version>2.5.8</version>
    </dependency>
    <!-- jstl 标签类 -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <!-- 日志记录(在控制台输出MyBatis的SQL语句) -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- FastJSON -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.21</version>
    </dependency>
    <!-- 数据库驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.38</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-core</artifactId>
        <version>1.3.5</version>
    </dependency>
    <!-- 数据库连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.26</version>
    </dependency>
    <!-- MyBatis 核心Jar包 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.4.1</version>
    </dependency>
    <!-- Spring MyBatis 整合 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.3.0</version>
    </dependency>
    <!-- Spring MVC -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <!-- spring核心包 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <!-- Spring 上下文 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <!-- Spring AOP -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <!-- 面向切面编程 -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.8.9</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.8.9</version>
    </dependency>
    <!-- Spring JDBC-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
    <!-- Spring Web-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.3.5.RELEASE</version>
    </dependency>
</dependencies>
<build>
    <finalName>SpringStrutsMyBatis</finalName>
    <!-- 指定Mapper XML文件在mybatis-config.xml中的配置有效-->
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>


3.  创建一个控制层的实体类:LoginAction.java, 并新建一个struts的配置文件struts.xml

具体的代码为:

package com.ssm.action;

import com.ssm.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class LoginAction {

    @Autowired
    private DeptService deptService;
    private String uname;
    private String upass;
    public String login() {
        if("aa".equals(uname)&&"aa".equals(upass)){
           
            return "success";
        }else
            return "error";
    }
    public String getUname() {
        return uname;
    }
    public void setUname(String uname) {
        this.uname = uname;
    }
    public String getUpass() {
        return upass;
    }
    public void setUpass(String upass) {
        this.upass = upass;
    }
}


struts.xml:配置文件:

<struts>
   <constant name="struts.i18n.encoding" value="UTF-8"></constant>
    <!-- 指定需要struts2处理的请求后缀 -->
   <constant name="struts.action.extension" value="do,action" />
    <constant name="struts.enable.DynamicMethodInvocation" value="true" />
    <package name="user" namespace="/user" extends="struts-default" strict-method-invocation="false">
        <action name="login" class="com.ssm.action.LoginAction" method="login">
            <result type="dispatcher" name="input">/login.jsp</result>
            <result type="dispatcher" name="success">/success.jsp</result>
            <result type="dispatcher" name="error">/error.jsp</result>
        </action>
    </package>
</struts>

4. 对web.xml文件做如下配置:

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>
        org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
</welcome-file-list>

5. 配置MyBatis, 新建一个xml文件,准备一个mybatis-generator-core-1.3.2.jar, 其位置关系如下:


具体作如下配置:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动,用绝对路径 -->
    <classPathEntry
            location="C:/Users/Administrator/.m2/repository/mysql/mysql-connector-java/5.1.38/mysql-connector-java-5.1.38.jar"/>
    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 : false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--数据库链接URL,用户名、密码 -->
        <jdbcConnection driverClass="com.mysql.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=UTC&amp;useSSL=true"
                        userId="root"
                        password="root">
        </jdbcConnection>
        <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer;
             为 true时,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal -->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>
        <!-- 生成Model的包名和位置 -->
        <javaModelGenerator targetPackage="com.ssm.domain"
                            targetProject="../../java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
            <!-- 从数据库返回的值被清理前后的空格 -->
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>
        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="com.ssm.mapping"
                         targetProject="../../java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>
        <!-- 生成DAO(Mapper接口)的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.ssm.dao" targetProject="../../java">
            <!-- enableSubPackages:是否让schema作为包的后缀 -->
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>
        <!-- 要对哪些数据表进行生成操作,至少得有一个 -->
        <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名 -->
<!-- 如果需要生成多个实体类,则需配置多次 -->
<table tableName="tb_Dept" domainObjectName="Dept"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</
table>
</context>
</
generatorConfiguration>

然后,打开命令行窗口(win+R):进入该配置文件所在的目录,然后,输入以下命令:
java -jar mybatis-generator-core-1.3.2.jar -configfile ../myBatisAuto/generatorConfig.xml -overwrite(需要注意的是:不能更改位置结构),此时相应的包下,已经生成了相应的实体类及dao层的文件;

6. 新建一个xml文件mybatisMapper.xml用来加载相应的mapping包下的资源,具体作如下配置:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--此时只有一个资源加载进去,如果需要加载多个资源,则需要一一的配置进去 -->
   <mappers>
        <mapper resource="com/ssm/mapping/DeptMapper.xml"/>
    </mappers>
</configuration>

7. 创建一个Spring的配置文件applicationContext.xml文件,并做以下配置:

<!-- ======================================== 配置自动注入 ========================================= -->
<context:component-scan base-package="com.ssm"></context:component-scan>

<!-- ======================================== 配置数据源 ========================================= -->

<bean id="ds" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql:///test?useSSL=false"></property>
    <property name="username" value="root"></property>
    <property name="password" value="root"></property>
</bean>
<!-- ======================================== 配置MyBatis的SqlSessionFactory ========================================= -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="ds"></property>
    <property name="configLocation" value="classpath:mybatisMapper.xml"></property>
</bean>
<!-- ======================================== 配置MyBatis自动扫描SQL映射文件  ========================================= -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--使用mapper的扫描器自动扫描mapper接口在spring中进行注册 -->
    <property name="basePackage" value="com.ssm.dao"></property>  <!-- basePackage指的是mapper所对应的接口的包名 -->
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- ========================================配置事务管理========================================= -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="ds" />
</bean>
<tx:annotation-driven transaction-manager="txManager" /> <!-- 让事务支持注解 (使用声明式事务)-->


8. 在webapp下创建几个jsp的测试页面,其结构如下:




页面 login.jsp:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<form action="user/login.do" method="post">
    <input type="text" name="uname">
    <input type="text" name="upass"><br>
    <input type="submit">
</form>
</body>
</html>


success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录成功</title>
</head>
<body>
登录成功
</body>
</html>


error.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录失败</title>
</head>
<body>
    登陆失败
</body>
</html>


9. 接下来用注解的方式,使相应层的类受Spring的管理,具体的注解方式,可以查阅其他资料获取,此处不再一一赘述;









1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:267次
    • 积分:13
    • 等级:
    • 排名:千里之外
    • 原创:1篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档