目录描述

  • 一、创建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、运行


SSM框架整合详细案例相关推荐

  1. SSM框架整合---详细案例

    目录 一.建四个配置文件在resource根目录 二.applicationContext-mybatis.xml核心配置 三.database.properties配置 四.mybatis-conf ...

  2. java ssm小案例_简易的SSM框架整合小案例

    简易的SSM框架整合小案例 一.创建一个web工程的maven项目 1.项目名随便起 2.选择好你的maven路径,然后finish 二.配置pom.xml文件 org.springframework ...

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

    SSM框架整合+简单案例实现 文章目录 前言 一.Spring+SpringMVC+Mybatis框架整合 1.建立一个新的web项目 2.所需jar包 3.建立数据库表与实体类之间的映射 4.web ...

  4. SSM框架整合—详细整合教程(Spring+SpringMVC+MyBatis)

    SSM框架整合-详细整合教程(Spring+SpringMVC+MyBatis) ✨博主介绍 MyBatis和Spring整合 1.整合原因 2.整合条件 3.整合入门 4.整合MyBatis现有配置 ...

  5. SSM框架整合完整案例

    SSM框架整合 一.整合思路 二.案例实战 1. 项目前期准备 2. 整合dao层 ① mybatis全局配置文件(SqlConfig.xml) ② 配置spring.xml ③ 编写POJO类(ja ...

  6. SSM框架整合入门案例

    文章目录 SSM整合案例 1,SSM整合 1.1 流程分析 1.2 整合配置 步骤1:创建Maven的web项目 步骤2:添加依赖 步骤3:创建项目包结构 步骤4:创建SpringConfig配置类 ...

  7. SSM框架开发-基础案例

    SSM框架整合基础案例详解 1.数据库环境 创建一个存放书籍数据的数据库表 CREATE DATABASE `ssmbuild`;USE `ssmbuild`;DROP TABLE IF EXISTS ...

  8. IDEA优雅整合Maven+SSM框架(详细思路+附带源码)

    前言: 网上很多整合SSM博客文章并不能让初探ssm的同学思路完全的清晰,可以试着关掉整合教程,摇两下头骨,哈一大口气,就在万事具备的时候,开整,这个时候你可能思路全无 ~中招了咩~ ,还有一些同学依 ...

  9. ssm框架使用resultful_SSM框架整合完整案例

    SSM框架整合 一.整合思路 二.案例实战 1. 项目前期准备 2. 整合dao层 ① mybatis全局配置文件(SqlConfig.xml) ② 配置spring.xml ③ 编写POJO类(ja ...

最新文章

  1. c 语言 按位与或非运算符,C++中的按位与、按位与或|、按位异或^运算符详解
  2. matlab读取.xyz文件及任意有间隔符数据
  3. 最人性化的在线作图工具
  4. 1059 Prime Factors (25 分)【难度: 一般 / 知识点: 分解质因子 】
  5. js 停止事件冒泡 阻止浏览器的默认行为
  6. 【Spring学习】spring提供的三种定时任务
  7. github怎么自动更新被人更新过的项目_8 个程序员应该掌握的 GitHub 实用技巧
  8. 创建并运用客户化jsp标签
  9. centos 安装openoffice (办公软件 WPS)
  10. 报告称相比南方 数字化平台对北方小微商家助力作用更大
  11. 电商促销页面需要的辅助图形给设计师准备好了,拿走!
  12. android 9.0 开机动画,小米9开机动画安装器
  13. 高拍仪是否支持TWAIN方式的测试方法
  14. Spring AOP动态代理的实现方式
  15. 目标检测算法——小目标检测相关数据集(附下载链接)
  16. 力扣刷题 DAY_82 贪心
  17. 用eclipse和androidstudio和基于高德地图API开发的附近购平台的java服务端和android客户端数据库用mysql包括程序设计图
  18. linux运行gpg软件,Linux实用工具之GPG
  19. mybatis框架实现一对多、多对多关系查询,以及递归查询(单表多级分类:省市区三级地址查询)
  20. Portraiture中文版最新mac3.5版win5.0版插件介绍

热门文章

  1. 虚拟服务器nat模式,nat模式虚拟机宿主机相互ping通
  2. 中专计算机要考什么证
  3. 解决上下标的大写字母显得太大的方法
  4. SVN客户端checkout失败解决方案
  5. 听见丨 锤子明年将有更多智能硬件还有T3
  6. 从单车到飞船的SQL优化之旅
  7. 信鸽Android推送解决问题记录--无法跳转到指定页面
  8. 小程序wx.showToast在真机上闪烁一下就消失
  9. 抖音点赞最多的标题_抖音吸引眼球的标题 抖音点赞高的句子
  10. python——list