简单的编写一个Spring+Strusts2+MyBatis CURD操作

之前没使用过Strust,一直都是使用的Spring+SpringMVC+MyBatis。今天百度了下Struts简单的使用。并整合一下使用。 Demo使用Maven进行依赖管理。

1.pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.sitcat</groupId>
  <artifactId>SSMTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SSMTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
 
  <dependencies>
        <!--spring相关依赖-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.0.2.RELEASE</version>
        </dependency>
        <!--struts2相关依赖-->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.5.14.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.5.14.1</version>
        </dependency>
               <!--MyBatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.2</version>
        </dependency>
 
        <!--MyBaits和Spring整合的适配包-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <!--mysql依赖-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.41</version>
        </dependency>
        <!--实现DataSource的接口-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--servlet依赖-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
 
    </dependencies>
  
  <build>
    <finalName>SSMTest</finalName>
  </build>
</project>
复制代码

2.web.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">
       <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    <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>queryAll.action</welcome-file>
    </welcome-file-list>
</web-app>
复制代码

3.基类 Teacher.java

package cn.sitcat.po;
 
import org.springframework.stereotype.Component;
 
/**
 * @author Hiseico
 * @create 2018-05-08 23:14
 * @desc
 **/
@Component
public class Teacher {
    private int id;
    private String name;
 
    public int getId() {
        return id;
    }
 
    public void setId(int id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
}
复制代码

5.TeacherMapper.java

package cn.sitcat.dao;
 
import cn.sitcat.po.Teacher;
import org.springframework.stereotype.Component;
 
import java.util.List;
 
/**
 * @author Hiseico
 * @create 2018-05-08 23:20
 * @desc
 **/
@Component
public interface TeacherMapper {
    //添加
    void addTeacher(Teacher teacher);
    //查询全部
    List<Teacher> queryAll();
    //根据编号删除
    void delete(int id);
    //根据编号查询
    Teacher queryById(int id);
    //修改
    void update(Teacher teacher);
}
复制代码

6.TeacherService.java

package cn.sitcat.service;
 
import cn.sitcat.dao.TeacherMapper;
import cn.sitcat.po.Teacher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
 
/**
 * @author Hiseico
 * @create 2018-05-08 23:36
 * @desc
 **/
@Service
public class TeacherService {
    @Autowired
    private TeacherMapper teacherMapper;
 
    public void add(Teacher teacher) {
        this.teacherMapper.addTeacher(teacher);
    }
 
    public void delete(int id) {
        this.teacherMapper.delete(id);
    }
 
    public List<Teacher> getAll() {
        return this.teacherMapper.queryAll();
    }
 
    public void edit(Teacher teacher) {
        this.teacherMapper.update(teacher);
    }
}
复制代码

7.TeacherController.java

package cn.sitcat.controller;
 
import cn.sitcat.po.Teacher;
import cn.sitcat.service.TeacherService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
 
public class TeacherController extends ActionSupport implements ModelDriven<Teacher>{//继承Strusts需要的父类和支持接受Request域内容的接口
    @Autowired
    private TeacherService teacherService;
    
    public Teacher getModel() {
        return teacher;
    }
 
    public void setTeacherService(TeacherService teacherService) {
        this.teacherService = teacherService;
    }
    @Autowired
    private Teacher teacher;
 
    public void setTeacher(Teacher teacher) {
        this.teacher = teacher;
    }
 
    public String add() {
        System.out.println("开始添加");
        teacherService.add(teacher);
        return "success";
    }
 
    public String queryAll() {
    	System.out.println("开始查询");
        List<Teacher> teachers = teacherService.getAll();
        HttpServletRequest httpServletRequest = ServletActionContext.getRequest();
        httpServletRequest.setAttribute("teachers", teachers);
        return "success";
    }
 
    public String delete() {
        System.out.println("开始删除");
        teacherService.delete(teacher.getId());
        return "success";
    }
 
    public String update() {
        System.out.println("开始更新");
        teacherService.edit(teacher);
        return "success";
    }
}
复制代码

8.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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--扫描Spring注解-->
    <context:component-scan base-package="cn.sitcat"/>
 
    <!--===============数据源=============================-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/strusts"/>
        <property name="driverClass" value="com.mysql.jdbc.Driver"/>
        <property name="user" value="root"/>
        <property name="password" value=""/>
    </bean>
 
    <!--===============配置和MyBaits的整合=============================-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定MyBatis全局配置文件位置-->
        <property name="configLocation" value="classpath:mybatis-config.xml"/>
 
        <!--数据源-->
        <property name="dataSource" ref="dataSource"/>
        <!--指定Mybatis mapper文件的位置-->
        <!-- 显式指定Mapper文件位置 -->
        <property name="mapperLocations" value="classpath:mapper/*.xml"/>
    </bean>
   
    <!-- Mapper注入 这是底层dao接口的注入,很重要,路径要对-->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="cn.sitcat.dao" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />  
    </bean>
</beans>
复制代码

9.struts.xml

<?xml version="1.0" encoding="UTF-8"?>
 
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
 
<struts>
    <package name="ssh" extends="struts-default">
        <action name="add" class="cn.sitcat.controller.TeacherController" method="add">
            <result name="success" type="redirectAction">queryAll</result>
        </action>
        <action name="queryAll" class="cn.sitcat.controller.TeacherController" method="queryAll">
            <result name="success" type="dispatcher">index.jsp</result>
        </action>
        <action name="delete" class="cn.sitcat.controller.TeacherController" method="delete">
            <result name="success" type="redirectAction">queryAll</result>
        </action>
        <action name="queryById" class="cn.sitcat.controller.TeacherController" method="queryById">
            <result name="success" type="dispatcher">update.jsp</result>
        </action>
        <action name="update" class="cn.sitcat.controller.TeacherController" method="update">
            <result name="success" type="redirectAction">queryAll</result>
        </action>
    </package>
</struts>
复制代码

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>
    <settings>
        <!--驼峰命名规则-->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>
 
    <typeAliases>
        <!--类型别名-->
        <package name="cn.sitcat.po"/>
    </typeAliases>
</configuration>
复制代码

11.index.jsp

<%@ page import="cn.sitcat.po.Teacher" %>
<%@ page import="java.util.List" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>老师信息</title>
</head>
<body>
<table border="1">
    <tr>
        <th>编号</th>
        <th>姓名</th>
        <th>操作</th>
    </tr>
    <%
        for(Teacher teacher:(List<Teacher>) request.getAttribute("teachers")){
    %>
    <tr>
        <td><%= teacher.getId()%></td>
        <td><%= teacher.getName()%></td>
        <td>
            <a href="delete.action?id=<%= teacher.getId()%>">删除</a> |
            <a href="queryById.action?id=<%= teacher.getId()%>">修改</a>
        </td>
    </tr>
    <%
        }
    %>
</table>
<a href="add.jsp">添加</a>
</body>
</html>
复制代码

12.add.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>添加</title>
</head>
<body>
<h1>添加</h1>
<form action="add.action" method="post">
    <p>
        <label>姓名:</label>
        <input type="text" name="name" id="name"/>
    </p>
    <p>
        <input type="submit" value="添加">
    </p>
</form>
</body>
</html>
复制代码

13.update.jsp

<%@ page import="cn.sitcat.po.Teacher" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改</title>
</head>
<body>
<h1>修改</h1>
<form action="update.action" method="post">
    <%
        Teacher teacher = (Teacher) request.getAttribute("teacher");
    %>
    <p>
        <label>姓名:</label>
        <input type="text" name="name" value="<%= teacher.getName()%>" id="name"/>
    </p>
    <p>
        <input type="hidden" name="id" value="<%= teacher.getId()%>" id="id"/>
        <input type="submit" value="修改">
    </p>
</form>
</body>
</html>
复制代码

根据strusts.xml中配置的命名空间,使用action进行访问,http://localhost:8080/queryAll.action

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值