struts2+spring+mybatis整合小案例

最近学习ssm框架,模仿别人做了一个小案例
当然途中也遇到了蛮多的问题.借此机会.记录一下自己的过程

struts2+spring+mybatis的整合过程

1.说明:

  • 个人采用的是deepin操作系统(深度linux)+Intellij Idea(相对与myeclipse我还是更喜欢idea 可能因为更智能)+tomcat7.
  • 当然在windows下没什么不同,具体操作过程很相似.

2.案例环境搭建
注:项目结构图
struts2+spring+mybatis整合小案例

2.1准备工作

     - 新建idea项目
     - 在web/web-inf下新建classes目录和lib
     - 导入jar包到lib目录下
     - 为module添加dependences
     - 具体操作步骤参见我的博客http://blog.csdn.net/jsu_9207/article/details/51271799

2.2 配置xml文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <!--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>

    <!--struts 配置-->
    <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>
</web-app>

2.3 实体类编写

public class User {
    private int id;
    private String name;
    private int age;
    //省略geter和setter方法以及 构造方法

2.4 userMapper.xml编写

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.engle.vo.UserMapper">

    <insert id="insertOne" parameterType="user">
        INSERT INTO user(name,age) VALUES (#{name},#{age})
    </insert>

    <select id="selectOne" parameterType="int" resultType="user">
        SELECT * FROM user WHERE id=#{id}
    </select>
</mapper>

2.5 dao层编写(userdao和userdaoimpl)

public interface UserDao {
    User SelectOne(int id);
}



public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {


    public User SelectOne(int id) {
        String statement = "com.engle.vo.UserMapper.selectOne";
       return getSqlSession().selectOne(statement,1);
    }
}

2.6 utils层

public class MybatisUtils {
    public static UserDao getUserDao(){
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        return (UserDao) context.getBean("userDao");
    }
}

2.7 service层(service及其实现)

public interface UserService {
    User selectOne();
}

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public User selectOne() {
        User user = userDao.SelectOne(1);
        return user;
    }
}

2.8 action层

public class UserAction {
    private User user;
    private UserService service;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserService getService() {
        return service;
    }

    public void setService(UserService service) {
        this.service = service;
    }


    public String getOneUser() throws Exception {
        user = service.selectOne();
        System.out.println(user);
        return "success";
    }
}

2.9 applicationContext.xml配置

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--配置通知-->
    <tx:advice id="txAdvice" transaction-manager="txManager">
        <tx:attributes>
            <tx:method name="select*" read-only="true"/>
            <tx:method name="insert*" propagation="REQUIRED"/>
            <tx:method name="update*" propagation="REQUIRED"/>
            <tx:method name="delete*" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>

    <!--切入点-->
    <aop:config>
        <aop:pointcut id="pointcut" expression="execution(* com.engle.dao.impl.UserDaoImpl.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pointcut"/>
    </aop:config>

    <!--数据源配置-->
     <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
         <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
         <property name="url" value="jdbc:mysql://localhost:3306/spring"/>
         <property name="username" value="root"/>
         <property name="password" value="root"/>

     </bean>

    <!--配置sessionFacfory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--加载dataSource-->
        <property name="dataSource" ref="dataSource"/>
        <!--加载usermapper.xml-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
    </bean>



    <!--userDao容器-->
    <bean id="userDao" class="com.engle.dao.impl.UserDaoImpl">

        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>

    </bean>
    <!--userService容器-->
    <bean id="userService" class="com.engle.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    <!--userAction容器-->
    <bean id="userAction" class="com.engle.action.UserAction">
        <property name="service" ref="userService"/>

    </bean>
    <!--配置事务管理器-->
    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
</beans>

2.10 mybatis-config.xml配置

<?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>

        <typeAliases>
                <typeAlias type="com.engle.vo.User" alias="user"/>
        </typeAliases>
        <mappers>
                <mapper resource="com/engle/vo/UserMapper.xml"/>
        </mappers>

</configuration>

2.11 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="default" namespace="/" extends="struts-default">
        <action name="support" class="userAction" method="getOneUser">
            <result>/user.jsp</result>
        </action>
    </package>
</struts>

2.12 user.jsp编写

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>user page</title>
</head>
<body align="center">
<s:property  value="user.toString()"/>

</body>
</html>

3.运行结果( 请求 http://localhost:8080/project-name/support)

User{id=1, name=’zhansan’, age=’14’}

4.源码地址: https://github.com/engle025/ssm_demo.git

上一篇:规定form表单提交的页面打开方式


下一篇:浅谈跨网站脚本攻击(XSS)的手段与防范(简析新浪微博XSS攻击事件)