基于maven的spring-struts2-mybatis(普通版)环境搭建(包含项目代码案例) select标签 头像上传、有详细案例 连接数据库:mysql

引言
所有软件:idea
插件版本:jdk1.8+tomcat7.88
连接数据库:mysql
所用技术:maven-spring-struts2-mybatis
主要对 select标签 头像上传、有详细案例 
1导入依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<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/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springMyabtis</artifactId>
        <groupId>com</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../springMyabtis/pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>spring-struts2-mybatis</artifactId>
    <packaging>war</packaging>

    <name>spring-struts2-mybatis Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

        <!-- 引入javaEE规范 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>servlet-api</artifactId>
            <version>2.5</version>
            <scope>provided</scope>
        </dependency>

        <!-- 引入spring相关的jar -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-expression</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.3.2.RELEASE</version>
        </dependency>

        <!-- 引入mybatis相关jar -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.2.8</version>
        </dependency>

        <!-- 引入mybatis 与 spring 整合jar -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.2</version>
        </dependency>

        <!-- 引入struts2 -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-core</artifactId>
            <version>2.3.16</version>
        </dependency>

        <!--  引入struts2 与spring的整合jar -->
        <dependency>
            <groupId>org.apache.struts</groupId>
            <artifactId>struts2-spring-plugin</artifactId>
            <version>2.3.16</version>
        </dependency>

        <!-- 引入mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <!-- 引入dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>

        <!-- 引入fastjson -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.47</version>
        </dependency>


        <!-- 引入log4j -->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.15</version>
        </dependency>

        <!--引入jstl标签-->
        <dependency>
            <groupId>jstl</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.2</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>spring-struts2-mybatis</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.1.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2 小配置文件 jdbc.properties
driver= com.mysql.jdbc.Driver
url= jdbc:mysql://localhost:3306/ems_spring
user= root
password = root
3 spring核心配置文件 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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/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">
       <!--读取小配置文件-->                   <!--如果是web项目需要添加classpath:-->
       <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>

        <!--配置数据源-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName">
                <value>${driver}</value>
            </property>
            <property name="url">
                <value>${url}</value>
            </property>
            <property name="username">
                <value>${user}</value>
            </property>
            <property name="password">
                <value>${password}</value>
            </property>
        </bean>

       <!--创建sqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--注入数据源-->
            <property name="dataSource" ref="dataSource"></property>
            <!--给实体类起别名-->
            <property name="typeAliasesPackage">
                <value>com.entity</value>
            </property>
            <!--注册Mapper文件-->
            <property name="mapperLocations">
                <list>
                    <value>classpath:com/dao/Mapper/*Mapper.xml</value>
                </list>
            </property>
        </bean>
    <!--自动提供Dao接口事务实现类对象-->
       <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
           <property name="basePackage">
               <!--dao接口的包结构-->
               <value>com.dao</value>
           </property>
       </bean>
<!--配置service实现类相关信息-->
    <!--员工表 emp service实现类的处理-->
    <bean id="empService" class="com.service.EmpServiceImpl">
        <!--注入dao接口实现的类对象-->
        <property name="empDao" ref="empDao"></property>
    </bean>
    <!--管理员表 admin 实现类的处理-->
    <bean id="adminService" class="com.service.AdminServiceImpl">
        <property name="adminDao" ref="adminDao"></property>
    </bean>

    <!--引入事务的额外功能代码-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
       <!--连接数据源-->
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--对事务进行相关描述-->
    <tx:advice id="transactionInterceptor" transaction-manager="transactionManager">
        <tx:attributes>
            <!--给增删改添加事务-->
            <tx:method name="modify*"/>
            <!--给查询添加事务-->
            <tx:method name="*" propagation="SUPPORTS" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>

    <!--定义切入点-->
    <aop:config>
        <!--定义切点表达式-->
        <aop:pointcut id="pc" expression="execution(* com.service..*.*(..))"></aop:pointcut>
        <aop:advisor advice-ref="transactionInterceptor" pointcut-ref="pc"></aop:advisor>
    </aop:config>


    <!--配置 Action-->
    <!--对员工表action处理-->      <!--scope="prototype" 代表多列 -->
    <bean id="empAction" class="com.action.EmpAction" scope="prototype">
        <property name="empService" ref="empService"></property>
    </bean>
    <!--对管理员action处理-->
    <bean id="adminAction" class="com.action.AdminAction" scope="prototype">
        <property name="adminService" ref="adminService"></property>
    </bean>
</beans>
4 web.xml文件
<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>

  <!--读取spring(applicationContext.xml)配置文件-->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

<!--配置 struts2 的核心过滤器-->
 <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>
  <!--配置spring 的 web 工厂-->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

本次举例 以管理员表为例
解释说明:entity、dao、mapper.xml、service、serviceImpl、action和以前一样 不变,此处省略

####5 实体类 (建表省略)

package com.entity;

import java.io.Serializable;
/**
 * 库表 emp 对应的实体类
 * */
public class Emp implements Serializable {
    private Integer id;
    private String code;
    private String name;
    private double salary;
    private Integer age;
//无参、有参、set/get、toString 省略
}

6 dao接口
package com.dao;

import com.entity.Emp;

import java.util.List;

/**
 * 实体类Emp 对应的 dao 接口
 * */
public interface EmpDao {
    //添加一条数据
    public void EmpSave(Emp emp);
    //展示所有员工信息
    public List<Emp> findAllEmp();
    //根据id 查询
    public Emp findByIdEmp(Emp emp);
    //根据 id 修改一条数据
    public void updateEmp(Emp emp);
    // 删除
    public void deleteByIdEmp(Emp emp);
}

7 实现dao接口的mapper.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.dao.EmpDao">

    <!-- 插入一条数据-->
    <insert id="EmpSave" parameterType="Emp">
          insert into emp (code,name,salary,age) values(#{code},#{name},#{salary},#{age})
    </insert>
    <!--展示所有员工信息-->
        <select id="findAllEmp" resultType="Emp">
            select * from emp
        </select>
    <!--根据id查询一条数据-->
    <select id="findByIdEmp" resultType="Emp">
        select * from emp where id = #{id}
    </select>
    <!--根据id修改一条信息-->
    <update id="updateEmp" parameterType="Emp">
        update emp
        <set>
            <if test="code != null">
              code=#{code},
          </if>
            <if test="name!=null">
                name=#{name},
            </if>
            <if test="age!=null">
                age=#{age}
            </if>
        </set>
        where id=#{id}
    </update>
    <!--删除-->
    <delete id="deleteByIdEmp" parameterType="Emp">
        delete from emp where id = #{id}
    </delete>
</mapper>


8 Service接口
package com.service;

import com.entity.Emp;

import java.util.List;

/**
 * 员工表 业务层 service 接口
 * */
public interface EmpService {
    //添加一条数据
    public void modifyEmpSaveService(Emp emp);
    //展示所有员工信息
    public List<Emp> findAllEmpService();
    //根据id 查询
    public Emp findByIdEmpService(Emp emp);
    //根据 id 修改一条数据
    public void modifyupdateEmpService(Emp emp);
    // 删除
    public void modifydeleteByIdEmpService(Emp emp);
}

9 Service接口的实现类
package com.service;

import com.dao.EmpDao;
import com.entity.Emp;
import java.util.List;

public class EmpServiceImpl implements  EmpService {
    //注入dao接口 作为 属性 注入 让 spring工厂 为我们创建对象
    private EmpDao empDao;
    public EmpDao getEmpDao() {
        return empDao;
    }
    public void setEmpDao(EmpDao empDao) {
        this.empDao = empDao;
    }
    @Override
    public void modifyEmpSaveService(Emp emp) {
        empDao.EmpSave (emp);
    }
    @Override
    public List<Emp> findAllEmpService() {
        List<Emp> empList = empDao.findAllEmp ();
        return empList;
    }
    //根据 id查询
    @Override
    public Emp findByIdEmpService(Emp emp) {
        Emp byIdEmp = empDao.findByIdEmp (emp);
        return byIdEmp;
    }
    //修改
    @Override
    public void modifyupdateEmpService(Emp emp) {
           empDao.updateEmp (emp);
    }
    @Override
    public void modifydeleteByIdEmpService(Emp emp) {
        empDao.deleteByIdEmp (emp);
    }
}

10. action
package com.action;
import com.entity.Emp;
import com.opensymphony.xwork2.ActionSupport;
import com.service.EmpService;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class EmpAction extends ActionSupport {
    //接收添加时需要的数据
    private Emp emp;
    //接收 sevice层的方法
    private EmpService empService;
    public EmpService getEmpService() {
        return empService;
    }
    public void setEmpService(EmpService empService) {
        this.empService = empService;
    }
    public Emp getEmp() {
        return emp;
    }
    public void setEmp(Emp emp) {
        this.emp = emp;
    }
    //处理上传头像
    //定义成员变量接收参数
    private File upload;
    //动态获取文件名
    private String uploadFileName;
    public File getUpload() {
        return upload;
    }
    public void setUpload(File upload) {
        this.upload = upload;
    }
    public String getUploadFileName() {
        return uploadFileName;
    }
    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }

    // 注册 添加
    public String addEmpAction()  {
      //处理上传的图片
        String realPath = ServletActionContext.getServletContext ().getRealPath ("/empImg/" + "/" + uploadFileName);
        System.out.println("相对路径="+realPath);
        try {
            FileUtils.copyFile (upload,new File (realPath));
        } catch (IOException e) {
            e.printStackTrace ();
        }
        emp.setCode (uploadFileName);
        System.out.println ("dddddddddd"+emp);
        empService.modifyEmpSaveService (emp);
        return "addEmpActionOk";
    }
    //展示所有信息
    public String findAllEmpAction(){
        List<Emp> empList = empService.findAllEmpService ();
        //获取 request 作用域 存值
        HttpServletRequest request = ServletActionContext.getRequest ();
        request.setAttribute ("empList",empList);
        return "findAllEmpActionOk";
    }

    //根据 id 删除
    public String deleteByIdEmpAction(){
        empService.modifydeleteByIdEmpService (emp);
        return "deleteByIdEmpActionOk";
    }
    //根据 id 查询 回显
    public String findByIdEmpAction(){
        Emp em = empService.findByIdEmpService (emp);
       //获取作用域
        HttpServletRequest request = ServletActionContext.getRequest ();
        request.setAttribute ("emp",em);
        return "findByIdEmpActionOK";
    }
    //根据id修改
    public String updateByIdEmpAction(){
        if (uploadFileName!=null) {

            //处理上传的图片
            String realPath = ServletActionContext.getServletContext ().getRealPath ("/empImg/" + "/" + uploadFileName);
            System.out.println ("相对路径=" + realPath);
            try {
                FileUtils.copyFile (upload, new File (realPath));
            } catch (IOException e) {
                e.printStackTrace ();
            }

            emp.setCode (uploadFileName);
          }
        empService.modifyupdateEmpService (emp);
        return "updateByIdEmpActionOK";
    }
}

10. 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="admin1" extends="struts-default" namespace="/admin1">
        <!--注册  class="adminAction" 类名首字母小写-->
        <action name="addAdminAction" class="adminAction" method="addAdminAction">
            <result name="addAdminActionOK" type="redirectAction">
                <param name="namespace">/emp</param>
                <param name="actionName">findAllEmpAction</param>
            </result>
        </action>
        <action name="loginAdminAction" class="adminAction" method="loginAdminAction">
            <!--登录成功-->
           <result name="loginAdminOK" type="redirectAction" >
               <param name="namespace">/emp</param>
               <param name="actionName">findAllEmpAction</param>
           </result>
            <!--登录失败-->
            <result name="loginError">/jsp/login.jsp</result>
        </action>
    </package>

    <!--对员工表的处理-->
    <package name="emp" extends="struts-default" namespace="/emp">
        <!-- 展示所有信息-->
        <action name="findAllEmpAction" class="empAction" method="findAllEmpAction">
            <result name="findAllEmpActionOk">/jsp/emplist.jsp</result>
        </action>
        <!--添加一条数据-->
        <action name="addEmpAction" class="empAction" method="addEmpAction">
            <result name="addEmpActionOk" type="redirect">/emp/findAllEmpAction</result>
        </action>
        <!--根据id删除-->
        <action name="deleteByIdEmpAction" class="empAction" method="deleteByIdEmpAction">
            <result name="deleteByIdEmpActionOk" type="redirect">/emp/findAllEmpAction</result>
        </action>

        <!--根据id查询-->
        <action name="findByIdEmpAction" class="empAction" method="findByIdEmpAction">
            <result name="findByIdEmpActionOK">/jsp/updateEmp.jsp</result>
        </action>

        <action name="updateByIdEmpAction" class="empAction" method="updateByIdEmpAction">
            <result name="updateByIdEmpActionOK" type="redirect">/emp/findAllEmpAction</result>
        </action>
    </package>
</struts>
11 页面 省略
总结
基于maven 连接 mysql数据 用 spring-struts2-mybatis 环境搭建, 成开发 案例  主要是service 和 action需要在applicationContext.xml中需要配置(如果是注解版无序配置 service,action)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值