Struts2+Spring+Mybatis框架集成的搭建。(SSM形式)

1. jar包
struts2:相关jar
spring:相关加入
struts2-spring-plugin.jar:spring和struts集成jar
mybatis:相关jar
mysql:数据库驱动包(这里用mysql作为样本)
mybatis-spring-1.2.1.jar:mybatis和spring集成jar
dbcp:database连接池
jstl:c标签(前台)
junit:测试相关
如果还需其他jar,自己加

2. 首先配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

    <!-- 配置Spring监视器 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- 配置strtus请求过滤器 -->
    <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>*.action</url-pattern>
    </filter-mapping>

</web-app>

3. 配置连接数据库参数: jdbc.properties

username=root
password=123
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/ssm

4. 配置Spring主配置文件(就是Spring监视器中的location)

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

        <!-- 注解扫描路径 -->
        <context:component-scan base-package="com.spring_struts"></context:component-scan>

        <!-- 解析datasource -->
        <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations" value="classpath:jdbc.properties"></property>
        </bean>

        <bean class="org.apache.commons.dbcp2.BasicDataSource" id="dataSource" destroy-method="close">
            <property name="url" value="${url}"></property>
            <property name="username" value="${username}"></property>
            <property name="driverClassName" value="${driverClassName}"></property>
            <property name="password" value="${password}"></property>
        </bean>

        <!-- 以下两个是Mybatis配置文件 -->
        <!-- 配置自定义注解映射扫描范围和名称  -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" id="mapperScannerConfigurer">
            <!-- 用到自定义注解类的包 -->
            <property name="basePackage" value="com.spring_struts.mapper"></property>
            <!-- 自定义注解 -->
            <property name="annotationClass" value="com.spring_struts.annotation.MybatisAnotation"></property>
        </bean>

        <!-- 配置SqlSessionFactoryBean -->
        <bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactoryBean">
            <!-- 配置datasource -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 如果一个包下有多个, -->
            <property name="mapperLocations" value="classpath:com/spring_struts/mapper/mapperxml/*.xml"></property>
            <!--   主配置文件(可以配置别名)  -->
            <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        </bean>

    </beans>

5. 创建struts.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="user_default" extends="struts-default" namespace="/user" >
        <!-- 这里的class是spring中@Controller中bean别名 -->
        <action name="findall" class="userAction">
            <result>/WEB-INF/jsp/userlist.jsp</result>
        </action>
        <!-- 这里的class是@controller 类的 Javabean 的 ID -->
        <action name="add" class="userAction" method="addUser">
            <result type="chain">findall</result>
        </action>
        <action name="delete" class="userAction" method="delete">
            <result type="chain" >findall</result>
        </action>
    </package>
</struts>

6.注解类 com.spring_struts.annotation.MybatisAnotation:

package com.spring_struts.annotation;

public @interface MybatisAnotation {
    //定义属性,类名称
    String value() default "";
}

7.Mybatis接口:Mapper.java

package com.spring_struts.mapper;
import java.util.List;
import com.spring_struts.annotation.MybatisAnotation;
import com.spring_struts.entity.User;

//自定义的注解
@MybatisAnotation
public interface Mapper {

    public List<User> findAll();

    public void addUser(String uname);

    public void delete(User user);

    public void dele(int id);
}

8.Mybatis的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"      
 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd">
 <!-- 接口名 -->
<mapper namespace="com.spring_struts.mapper.Mapper">

    <select id="findAll" resultType="com.spring_struts.entity.User">
        select * from t_user    
    </select>

    <insert id="addUser" parameterType="java.lang.String">
        insert into t_user values(null,#{uname})
    </insert>

    <update id="delete" parameterType="com.spring_struts.entity.User">
        update t_user set uname=#{uname} where id=#{id}
    </update> 

    <delete id="dele" parameterType="int">
        delete from t_user where id=#{id}
    </delete> 
</mapper>

9.mybatis主配置文件:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" 
    "http://ibatis.apache.org/dtd/ibatis-3-config.dtd">

<configguration>
    <!-- 配置别名 -->
    <typeAliases>
        <typeAlias type="com.ssm.entity.Emp" alias="emp"/>
    </typeAliases>
    <!-- 
    配置Mybatis映射文件 
    <mappers>
        <mapper resource="com/ssm/mapper/mapperxml/*.xml"/>
    </mappers> 
    -->
</configguration>

注意:在Struts的Action中,要是使用注解 @Controller(“userAction”)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值