SSM框架整合详细案例

article/2025/9/18 12:31:03

目录描述

    • 一、创建web项目(使用idea maven)
    • 二、使用maven添加依赖
      • 1、在pom.xml中添加项目所需的所有依赖
      • 2、添加tomcat运行驱动
    • 三、建立整体项目架构
    • 四、搭建mybatis
      • 1、编写mybatis-config.xml
      • 2、编写数据库连接配置文件
      • 3、编写spring-mybatis.xml
      • 4、编写pojo和dao层
      • 5、编写映射文件
    • 五、搭建spring
      • 1、spring-context.xml
    • 六、DAO层测试
    • 七、搭建SpringMVC
      • 1、编写spring-mvc.xml
      • 2、编写Service层
      • 3、测试Service层
      • 4、编写web.xml
      • 5、编写Controller层
    • 八、编写页面
    • 九、运行
      • 1、配置tomcat
      • 2、运行

一、创建web项目(使用idea maven)

都会

二、使用maven添加依赖

1、在pom.xml中添加项目所需的所有依赖

     <!-- mysql驱动--><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency><!-- mybatis驱动--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version><scope>provided</scope></dependency><!--spring依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>5.3.13</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.13</version></dependency><!--持久化支持依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.13</version></dependency><!--spring 整合mybatis依赖--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!--德鲁伊数据库连接池--><!-- https://mvnrepository.com/artifact/com.alibaba/druid --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.10</version></dependency><!--单元测试--><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.13</version></dependency><!--junit--><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><!-- Servlet 依赖--><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><!--分页插件 PageHelper--><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.1.10</version></dependency><dependency><groupId>com.github.3tty0n</groupId><artifactId>jwt-scala_2.12</artifactId><version>1.3.0</version></dependency><dependency><groupId>com.github.3tty0n</groupId><artifactId>jwt-scala_2.11</artifactId><version>1.3.0</version></dependency><!--SPRING-MVC依赖--><!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.13</version></dependency><!-- https://mvnrepository.com/artifact/org.springframework/spring-web --><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>5.3.13</version></dependency><!--JSON 将json类型转换成对象--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.13.0</version></dependency><dependency><groupId>org.junit.jupiter</groupId><artifactId>junit-jupiter</artifactId><version>RELEASE</version><scope>compile</scope></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>compile</scope></dependency></dependencies>

2、添加tomcat运行驱动

<!--添加运行tomcat的插件--><plugin><!-- https://mvnrepository.com/artifact/org.apache.tomcat.maven/tomcat7-maven-plugin --><groupId>org.apache.tomcat.maven</groupId><artifactId>tomcat7-maven-plugin</artifactId><version>2.1</version></plugin>

三、建立整体项目架构

在这里插入图片描述

四、搭建mybatis

1、编写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></configuration>

其实可以省略。

2、编写数据库连接配置文件

druid.url=jdbc:mysql://localhost:3306/biology?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
druid.driver=com.mysql.jdbc.Driver
druid.username=root
druid.password=123456
druid.pool.init=2
druid.pool.minIdle=3
druid.pool.maxActive=20
druid.pool.timeout=30000

3、编写spring-mybatis.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!--加载druid.properties属性文件--><context:property-placeholder location="classpath:druid.properties"/><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="url" value="${druid.url}"/><property name="driverClassName" value="${druid.driver}"/><property name="username" value="${druid.username}"/><property name="password" value="${druid.password}"/><property name="initialSize" value="${druid.pool.init}"/><property name="maxActive" value="${druid.pool.maxActive}"/><property name="minIdle" value="${druid.pool.minIdle}"/><property name="maxWait" value="${druid.pool.timeout}"/></bean><!--依赖Spring容器完成Mybatis的SqlSession对象的创建--><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--配置数据源--><property name="dataSource" ref="dataSource"/><!--配置Mapper文件路径--><property name="mapperLocations" value="classpath:mappers/*.xml"/><!--自动取别名 即类名 !可选!--><property name="typeAliasesPackage" value="com.atmae.pojo"/><!--配置mybatis-config主配置文件 !可选!--><property name="configLocation" value="classpath:mybatis-config.xml"/></bean><!--配置dao包中所有Dao接口,通过sqlSessionFactory获取SelSession,然后创建所有Dao接口对象 存储spring容器--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="basePackage" value="com.atmae.dao"/></bean><!--spring提供的事务管理类配置给spring容器--><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="dataSource"/></bean><!--通过spring jdbc提供的tx标签 声明事务管理策略--><tx:advice id="txAdvice" transaction-manager="transactionManager"><tx:attributes><tx:method name="insert*" isolation="REPEATABLE_READ" propagation="REQUIRED"/><tx:method name="query*" isolation="REPEATABLE_READ" propagation="SUPPORTS"/></tx:attributes></tx:advice><!--将事务管理策略以AOP配置 应用于Service中--><aop:config><aop:pointcut id="crud" expression="execution(* com.atmae.service.*.*(..))"/><aop:advisor advice-ref="txAdvice" pointcut-ref="crud"/></aop:config>
</beans>

4、编写pojo和dao层

POJO:

package com.atmae.pojo;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {private int userId;private String userName;private String userPassword;private String userEmail;
}

DAO:

package com.atmae.dao;import com.atmae.pojo.User;public interface UserDao {public User queryUserByName(String name);
}

5、编写映射文件

<?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.atmae.dao.UserDao"><resultMap id="userMap" type="User"><id column="id" property="userId"/><result column="username" property="userName"/><result column="password" property="userPassword"/><result column="email" property="userEmail"/></resultMap><select id="queryUserByName" resultMap="userMap">select id,username,password,emailfrom t_userwhere username=#{userName}</select>
</mapper>

五、搭建spring

1、spring-context.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--声明使用注解配置--><context:annotation-config/><!--声明Spring工厂注解扫描的包--><context:component-scan base-package="com.atmae"/>
</beans>

六、DAO层测试

package com.atmae.test;import com.atmae.dao.UserDao;
import com.atmae.pojo.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml","classpath:spring-mvc.xml","classpath:spring-mybatis.xml"})
/*** RunWith:自动创建Spring应用上下文* ContextConfiguration:当一个类添加了注解@Component,那么他就自动变成了一个bean,就不需要再Spring配置文件中显示的配置了。*/
public class UserDaoTest {/*** Resource:自动装配*/@Resourceprivate UserDao userDao;@Testpublic void queryUserByName() {User user = userDao.queryUserByName("Admin");System.out.println(user);}
}

七、搭建SpringMVC

1、编写spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:mvc="http://www.springframework.org/schema/mvc"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsd"><!--声明MVC使用注解配置--><mvc:annotation-driven/></beans>

2、编写Service层

UserService接口:

package com.atmae.service;import com.atmae.pojo.User;public interface UserService {public User checkLogin(String userName, String UserPassword);
}

UserServiceImpl实现类:

package com.atmae.service.impl;import com.atmae.dao.UserDao;
import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;import javax.annotation.Resource;@Service
public class UserServiceImpl implements UserService {@Resourceprivate UserDao userDao;/*** Transactional:添加事务管理*/@Transactionalpublic User checkLogin(String userName, String UserPassword) {User user = userDao.queryUserByName(userName);if (user!=null&&user.getUserPassword().equals(UserPassword)) {return user;} else {return null;}}
}

3、测试Service层

package com.atmae.test;import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import javax.annotation.Resource;import static org.junit.Assert.*;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-context.xml", "classpath:spring-mybatis.xml", "classpath:spring-mvc.xml"})
public class UserServiceTest {@ResourceUserService userService;@Testpublic void checkLogin() {User user = userService.checkLogin("Admin", "666666");assertNotNull(user);}
}

4、编写web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--Licensed to the Apache Software Foundation (ASF) under one or morecontributor license agreements.  See the NOTICE file distributed withthis work for additional information regarding copyright ownership.The ASF licenses this file to You under the Apache License, Version 2.0(the "License"); you may not use this file except in compliance withthe License.  You may obtain a copy of the License athttp://www.apache.org/licenses/LICENSE-2.0Unless required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.
--><!--- This is the Cocoon web-app configurations file-- $Id$-->
<web-app version="2.4"xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"><servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><!--加载 三个文件 spring-mvc /context / mybatis--><param-value>classpath:spring-*.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>

5、编写Controller层

userController:

package com.atmae.controller;import com.atmae.pojo.User;
import com.atmae.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;@Controller
@RequestMapping("/user")
public class UserController {@Resourceprivate UserService userService;@RequestMapping("/login")public String login(String userName, String userPassword, HttpServletRequest request){User user=userService.checkLogin(userName,userPassword);if (user==null){request.getSession().setAttribute("tips","用户名或者密码错误!");return "/login.jsp";}else{request.getSession().setAttribute("user",user);return "/index.jsp";}}
}

八、编写页面

<%--Created by IntelliJ IDEA.User: MaeDate: 2021/12/12Time: 11:32To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><title>Index</title>
</head>
<body>
<h1>${user.userName}</h1>
</body>
</html>
<%--Created by IntelliJ IDEA.User: MaeDate: 2021/12/12Time: 11:35To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head><base href="${pageContext.request.contextPath}/"><title>Login</title>
</head>
<body>
<h1>${tips}</h1>
<form action="user/login" method="post"><p>账号:<input type="text" name="userName"/></p><p>密码:<input type="password" name="userPassword"></p><p>登录:<input type="submit" value="Login"></p>
</form>
</body>
</html>

九、运行

1、配置tomcat

在这里插入图片描述

2、运行

在这里插入图片描述
在这里插入图片描述


http://chatgpt.dhexx.cn/article/ECVN3KMW.shtml

相关文章

SSM框架详细实例讲解

SSM框架简介 SSM框架&#xff0c;是Spring Spring MVC MyBatis的缩写&#xff0c;这个是继SSH之后&#xff0c;目前比较主流的Java EE企业级框架&#xff0c;适用于搭建各种大型的企业级应用系统。 我们先大概的回顾一下吧。1.Spring简介 Spring是一个开源框架&…

SSM框架搭建实例

SSM框架搭建简单实例 - 搭建步骤 需求分析&#xff1a; 实现用户表信息的增删改查 1. 开发环境 环境 &#xff1a; JDK 1.8 软件 &#xff1a; myeclipse 数据库 &#xff1a; MySql 2. 创建数据库 数据库名称&#xff1a;ssm 字符集&#xff1a;…

java ssm框架项目_3个SSM框架应用实例教程

SSM框架简介 SSM(SpringSpringMVCMyBatis)框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成&#xff0c;常作为数据源较简单的web项目的框架。 文章主要介绍3个SSM框架应用的实例教程&#xff0c;非常适合想学习应用该整合框架的小伙伴。 该项目假设为开发一个简单的用…

SSM框架整合实例

1.数据准备 创建一个数据库并取名为ssm&#xff0c;在数据库ssm下创建user表&#xff1b;对应的sql脚本如下&#xff0c;这里我们直接使用navicat工具更方便。 create database ssm;use ssm; create table user(id int primary key auto_increment,username varchar(20),pass…

SSM框架整合+简单案例实现

SSM框架整合简单案例实现 文章目录 前言一、SpringSpringMVCMybatis框架整合1.建立一个新的web项目2.所需jar包3.建立数据库表与实体类之间的映射4.web.xml5.数据库信息db.properties6.Spring与Mybatis的整合7.SpringMVC基本配置 二、整合测试1.结构展示2.studentMapper.xml3.s…

ssm框架的简单案例

学完ssm框架后做了一个简单的案例&#xff0c;实现简单的增删改查。 项目结构图&#xff1a; 看项目结构&#xff1a; 关键代码&#xff1a; 前端是jsp技术&#xff0c;当然也可以用vue分离的自己选择就好。 订单页面&#xff1a; <% page language"java&quo…

SSM框架开发-基础案例

SSM框架整合基础案例详解 1.数据库环境 创建一个存放书籍数据的数据库表 CREATE DATABASE ssmbuild;USE ssmbuild;DROP TABLE IF EXISTS books;CREATE TABLE books (bookID INT(10) NOT NULL AUTO_INCREMENT COMMENT 书id,bookName VARCHAR(100) NOT NULL COMMENT 书名,bookCou…

SSM框架简单实例

1、SSM框架 SSM&#xff08;SpringSpringMVCMyBatis&#xff09;框架集由Spring、SpringMVC、MyBatis三个开源框架整合而成&#xff0c;常作为数据源较简单的web项目的框架。 2、简单实例 &#xff08;1&#xff09;项目结构 &#xff08;2&#xff09;PageBean.java packa…

用SSM实现简单实例

SSM的简例实现 这是项目完整代码的链接地址 [https://download.csdn.net/download/m0_46308149/12961224]源码地址 SSM介绍 SSM&#xff08;SpringSpringMVCMyBatis&#xff09;框架集由Spring、MyBatis两个开源框架整合而成&#xff08;SpringMVC是Spring中的部分内容&am…

SpringMVC笔记——SSM框架搭建简单实例

简介 SpringSpringMVCMyBatis框架&#xff08;SSM&#xff09;是比较热门的中小型企业级项目开发的框架&#xff0c;对于新手来说也是比较容易学习入门的。虽说容易&#xff0c;但在框架搭建过程中仍然遇到了许多问题&#xff0c;因此用实例记录下来吧。 实例 第一步——导包 S…

SSM框架整合+案例

SSM框架整合 1、环境要求2、数据库环境3、基本环境搭建3.1 创建项目3.2 Maven项目添加web支持3.3 配置pom.xml文件3.4 建立框架的基本结构和配置文件3.4.1 创建包3.4.2 添加配置文件3.4.3 database.properties文件3.4.4 mybatis-config.xml 文件3.4.5 applicationContext.xml 文…

C# SqlHelper类的使用

SqlHelper类 1.首先SqlHelper类是一个基于.NET Framework的数据库操作组件&#xff0c;包含了数据库的操作方法。可以简化在C#中使用ADO.NET连接数据库时每次都要编写连接、打开、执行SQL语句的代码&#xff0c;它将每次连接都要写的代码封装成方法&#xff0c;把要执行的SQL语…

SqlHelper类(C#)

大神可以绕道了... 目的&#xff1a;搜集SqlHelper类 自己写的一个SQLHelper类&#xff0c;如下&#xff1a; 编辑App.config节点&#xff0c;添加<connectionStrings>节点&#xff0c;并根据实际填上相应的参数 <?xml version"1.0" encoding"utf…

SQLHelper

前言 小编在最近的学习过程中用到了SQLHelper&#xff0c;说起来&#xff0c;小编也是有点懒&#xff0c;虽然是用到了&#xff0c;但是也没有用明白&#xff0c;开始的时候也没有好好的研究&#xff0c;直到后来报错了&#xff0c;才下定决心好好好学习了解一下这个东西。下面…

sqlhelper 的使用 (C#)超级详细的入门教程

sql helper 的使用 &#xff08;C#&#xff09;小白教程 提到CRUD&#xff0c;很多刚入门的小白总是来一条写一条连接&#xff0c;先建立连接connection 打开连接 open 来查询query 最后别忘了关闭连接 close 。 要是一次写多个操作&#xff0c;那一遍一遍写下来肯定麻木了。…

Python中的BIF

什么是BIF呢&#xff1f; BIF是built-in functions的缩写&#xff0c;顾名思义&#xff0c;就是内建函数。Python中提供了大量的BIF&#xff0c;这就意味着代码量可以大大减少。 如果要查看Python中的内建函数&#xff0c;就可以使用命令&#xff1a;dir(__builtins__) 注意…

一、bif

缩进是python的灵魂&#xff0c;缩进可以使python的代码整洁&#xff0c;有层次。 python是脚本语言&#xff0c;就是为了简单方便以辅助科学运算&#xff0c;因此python有许多bif&#xff0c;build in function 全部都是小写的就是bif。 转义字符是一个字符&#xff0c;在内存…

FineBI

还是数据可视化工具Tableau、FineBI? 不禁联想起在微软系统出现之前&#xff0c;程序员的电脑系统还是用的linux&#xff0c;只能通过各种复杂的指令来实现字符的简单可视化&#xff1b;而当win系统普及于世后&#xff0c;计算机从此突破了技术人群的限制&#xff0c;交互方式…

Python学习笔记2(小甲鱼)—— 内置函数BIF

&#xfeff;&#xfeff; 这里有一个让人“亮瞎眼”的“游戏”开始python的学习。首先我们编写一段代码&#xff0c;来实现这个游戏。编写操作可参考前面的《Python学习笔记1——搭建环境与“Hello World”》这篇文章&#xff0c;网址&#xff1a;http://blog.csdn.net/tongbi…

BSDiff算法

https://blog.csdn.net/add_ada/article/details/51232889 BSDiff是一个差量更新算法&#xff0c;它在服务器端运行BSDiff算法产生patch包&#xff0c;在客户端运行BSPatch算法&#xff0c;将旧文件和patch包合成新文件。 差量更新算法的核心思想 尽可能多的利用old文件中已有…