Java学习路线-63:maven整合ssm-spring+struts+mybatis

本文涉及的产品
云数据库 RDS MySQL,集群系列 2核4GB
推荐场景:
搭建个人博客
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
云数据库 RDS MySQL,高可用系列 2核4GB
简介: Java学习路线-63:maven整合ssm-spring+struts+mybatis

项目结构

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── pengshiyu
    │   │           ├── action
    │   │           │   └── UserAction.java
    │   │           ├── bean
    │   │           │   └── User.java
    │   │           ├── dao
    │   │           │   ├── UserDao.java
    │   │           │   └── impl
    │   │           │       └── UserDaoImpl.java
    │   │           └── service
    │   │               ├── UserService.java
    │   │               └── impl
    │   │                   └── UserServiceImpl.java
    │   ├── resources
    │   │   ├── beans.xml
    │   │   ├── config
    │   │   │   ├── mybatis
    │   │   │   │   └── user.mapper.xml
    │   │   │   ├── spring
    │   │   │   │   └── user.xml
    │   │   │   └── struts
    │   │   │       └── user.xml
    │   │   ├── mybatis-config.xml
    │   │   └── struts.xml
    │   └── webapp
    │       ├── WEB-INF
    │       │   └── web.xml
    │       ├── index.html
    │       └── list.jsp
    └── test
        └── java

一、配置

1、Maven

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">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>spring-mybatis-struts-demo</artifactId>
<version>1.0-SNAPSHOT</version>


<build>
<finalName>maven-springmvc</finalName>

<resources>
<!--表示把java目录下的有关xml文件,properties文件编译/打包的时候放在resource目录下-->
<resource>
<directory>${basedir}/src/main/java</directory>
<includes>
<include>*/.properties</include>
<include>*/.xml</include>
</includes>
</resource>
<resource>
<directory>${basedir}/src/main/resources</directory>
</resource>
</resources>

<plugins>
<!-- tomcat7插件 maven 命令 tomcat7:run 启动项目-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/</path>
<uriEncoding>UTF-8</uriEncoding>
<!--添加忽略war包检查标签,则可以让tomcat7:run指令正常启动tomcat-->
<ignorePackaging>true</ignorePackaging>
<contextFile>src/main/webapp/WEB-INF/web.xml</contextFile>
<contextReloadable>true</contextReloadable>
</configuration>
</plugin>

</plugins>
</build>

<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.18</version>
</dependency>

<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>2.5.22</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>

<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.4</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.struts/struts2-spring-plugin -->
<dependency>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-spring-plugin</artifactId>
<version>2.5.22</version>
</dependency>

<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>

<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>

</dependencies>
</project>

2、Tomcat

src/main/webapp/WEB-INF/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_4_0.xsd";
version = "4.0">

<!-- 配置spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beans.xml</param-value>
</context-param>

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

<!-- 配置 struts2 -->
<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>*.action</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>

3、Spring

src/main/resources/beans.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";
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd";>

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

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

<import resource="config/spring/user.xml" />
</beans>

4、MyBatis

src/main/resources/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>
<!-- 打印sql日志 -->
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

<mappers>
<mapper resource="config/mybatis/user.mapper.xml"/>
</mappers>

</configuration>

5、Struts2

src/main/resources/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>
<include file="config/struts/user.xml"/>
</struts>

6、Spring/User

src/main/resources/config/spring/user.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";
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd";>

<bean id="userDao" class="com.pengshiyu.dao.impl.UserDaoImpl">
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>

<bean id="userService" class="com.pengshiyu.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>

<bean id="userAction" class="com.pengshiyu.action.UserAction">
<property name="userService" ref="userService"/>
</bean>
</beans>

7、Mybatis/User

src/main/resources/config/mybatis/user.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.pengshiyu.bean.user.mapper">
<!--
创建users表
create table users(
id int PRIMARY key auto_increment,
name varchar(20),
password varchar(20)
)

插入测试数据

insert into users(name, `password`) values("小明", "123456");
insert into users(name, `password`) values("小王", "123457");
insert into users(name, `password`) values("小赵", "123458");
insert into users(name, `password`) values("小四", "123459");
-->
<select id="getAll" resultType="com.pengshiyu.bean.User">
select * from users
</select>
</mapper>

8、Struts/User

src/main/resources/config/struts/user.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="default" namespace="/" extends="struts-default">

<action name="list" class="userAction" method="list">
<result>/list.jsp</result>
</action>
</package>

</struts>

Java 类

1、User

src/main/java/com/pengshiyu/bean/User.java

ppackage com.pengshiyu.bean;

public class User {
private int id;
private String name;
private String password;

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;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}
}

2、UserDao

src/main/java/com/pengshiyu/dao/UserDao.java

package com.pengshiyu.dao;

import com.pengshiyu.bean.User;

import java.util.List;

public interface UserDao {
List<User> getAll();
}

3、UserDaoImpl

src/main/java/com/pengshiyu/dao/impl/UserDaoImpl.java

package com.pengshiyu.dao.impl;

import com.pengshiyu.bean.User;
import com.pengshiyu.dao.UserDao;
import org.mybatis.spring.support.SqlSessionDaoSupport;

import java.util.List;

public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
public List<User> getAll() {
return this.getSqlSession().selectList(
"com.pengshiyu.bean.user.mapper.getAll");
}
}

4、UserService

src/main/java/com/pengshiyu/service/UserService.java

package com.pengshiyu.service;

import com.pengshiyu.bean.User;

import java.util.List;

public interface UserService {
List<User> getAll();
}

5、UserServiceImpl

src/main/java/com/pengshiyu/service/impl/UserServiceImpl.java

package com.pengshiyu.service.impl;

import com.pengshiyu.bean.User;
import com.pengshiyu.dao.UserDao;
import com.pengshiyu.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
private UserDao userDao;

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

public List<User> getAll() {
return userDao.getAll();
}
}

6、UserAction

src/main/java/com/pengshiyu/action/UserAction.java

package com.pengshiyu.action;

import com.opensymphony.xwork2.ActionSupport;
import com.pengshiyu.bean.User;
import com.pengshiyu.service.UserService;

import java.util.List;

public class UserAction extends ActionSupport {
private List<User> users;
private UserService userService;

public String list(){
System.out.println("list");

users = userService.getAll();
System.out.println(users);
return SUCCESS;
}

public List<User> getUsers() {
return users;
}

public void setUsers(List<User> users) {
this.users = users;
}

public UserService getUserService() {
return userService;
}

public void setUserService(UserService userService) {
this.userService = userService;
}
}

页面

1、index

src/main/webapp/index.html

hello spring

2、list

src/main/webapp/list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"; %>

<table>
<tr>
<th>编号</th>
<th>姓名</th>
<th>密码</th>
</tr>

<c:forEach items="${users}" var="item">
<tr>
<td>${item.id}</td>
<td>${item.name}</td>
<td>${item.password}</td>
</tr>
</c:forEach>

</table>

访问测试

GET http://localhost:8080/list.action

编号  姓名  密码
1 小明 123456
2 小王 123457
3 小赵 123458
4 小四 123459

源码地址:

https://github.com/mouday/spring-struts-mybatis-demo

            </div>
相关实践学习
如何在云端创建MySQL数据库
开始实验后,系统会自动创建一台自建MySQL的 源数据库 ECS 实例和一台 目标数据库 RDS。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助 &nbsp; &nbsp; 相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
4月前
|
Java Maven 容器
java依赖冲突解决问题之Maven在编译打包过程中对依赖的jar包如何解决
java依赖冲突解决问题之Maven在编译打包过程中对依赖的jar包如何解决
|
1月前
|
Java Maven Android开发
【Azure Developer】VS Code打包Java maven Project 遇见 BUILD FAILURE
Unknown lifecycle phase "lean". You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id>[:<plugin-version>]:<goal>
|
1月前
|
Java 持续交付 项目管理
使用Maven进行项目管理:提高Java Web开发的效率
Maven 是一款强大的项目管理和构建自动化工具,广泛应用于Java社区。它通过依赖管理、构建生命周期管理、插件机制和多模块项目支持等功能,简化了项目的构建过程,提高了开发效率。本文将介绍Maven的核心功能及其在Java Web开发中的应用。
63 0
|
2月前
|
Java Shell Maven
Flink-11 Flink Java 3分钟上手 打包Flink 提交任务至服务器执行 JobSubmit Maven打包Ja配置 maven-shade-plugin
Flink-11 Flink Java 3分钟上手 打包Flink 提交任务至服务器执行 JobSubmit Maven打包Ja配置 maven-shade-plugin
125 4
|
3月前
|
Java 数据库连接 Maven
【Java笔记+踩坑】Maven高级
分模块开发、依赖传递与冲突问题、 可选依赖和排除依赖、聚合和继承、属性、多环境配置与应用、私服安装和使用
【Java笔记+踩坑】Maven高级
|
4月前
|
前端开发 Java 编译器
【前端学java】如何从前端视角快速学习Maven
【8月更文挑战第12天】如何从前端视角快速学习Maven
64 2
【前端学java】如何从前端视角快速学习Maven
|
4月前
|
前端开发 Java Maven
【前端学java】全网最详细的maven安装与IDEA集成教程!
【8月更文挑战第12天】全网最详细的maven安装与IDEA集成教程!
109 2
【前端学java】全网最详细的maven安装与IDEA集成教程!
|
4月前
|
Java 持续交付 项目管理
Maven是一款基于Apache许可的项目管理和构建自动化工具,在Java开发中极为流行。
Maven是一款基于Apache许可的项目管理和构建自动化工具,在Java开发中极为流行。它采用项目对象模型(POM)来描述项目,简化构建流程。Maven提供依赖管理、标准构建生命周期、插件扩展等功能,支持多模块项目及版本控制。在Java Web开发中,Maven能够自动生成项目结构、管理依赖、自动化构建流程并运行多种插件任务,如代码质量检查和单元测试。遵循Maven的最佳实践,结合持续集成工具,可以显著提升开发效率和项目质量。
59 1
|
4月前
|
XML Java Maven
Java 中的 Maven 和 Ant 的区别
【8月更文挑战第22天】
200 0
|
5月前
|
存储 Java Maven
Maven依赖全爆红,一刷新就JAVA_HOME environment variable is not defined correctlyThis environment variable is
Maven依赖全爆红,一刷新就JAVA_HOME environment variable is not defined correctlyThis environment variable is

推荐镜像

更多