ssm框架整合

ssm

也就是:spring + springmvc + mybatis

 

具体思路如下:

1659055261913

springmvc,负责实现MVC设计模式

mybatis,负责数据持久层

spring,负责管理这两个框架相关对象的创建和依赖注入。

因为springmvc是spring的一个子模块,所以ssm整合,其实就是spring和mybatis的整合。

 

整合

1.创建一个maven工程,打钩,现在webapp

2.删掉单元测试依赖

 <dependency>
     <groupId>junit</groupId>
     <artifactId>junit</artifactId>
     <version>4.11</version>
     <scope>test</scope>
   </dependency>

 

2.去pom添加依赖(加在删掉哪里)

<?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>com..ssm</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>

 <name>com..ssm 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>

   <!--添加依赖 springMVC-->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>5.3.12</version>
   </dependency>
   <!--添加依赖 springJDBC-->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>5.3.12</version>
   </dependency>

   <!--添加依赖 springAOP-->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aop</artifactId>
     <version>5.3.12</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aspects</artifactId>
     <version>5.3.12</version>
   </dependency>

   <!--添加依赖 mybatis-->
   <dependency>
     <groupId>org.mybatis</groupId>
     <artifactId>mybatis</artifactId>
     <version>3.4.5</version>
   </dependency>
   <!--添加依赖 mybatis整合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.21</version>
   </dependency>

   <!--添加依赖 c3p0连接池--> <dependency>
   <groupId>c3p0</groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.1.2</version>
 </dependency>


   <!--添加依赖 JSTL jsp依赖-->
   <dependency>
     <groupId>jstl</groupId>
     <artifactId>jstl</artifactId>
     <version>1.2</version>
   </dependency>
   <!--添加依赖 servletAPI依赖-->
   <dependency>
     <groupId>javax.servlet</groupId>
     <artifactId>javax.servlet-api</artifactId>
     <version>3.1.0</version>
   </dependency>
<!--lombok-->
   <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>1.18.6</version>
     <scope> provided</scope>
   </dependency>
 </dependencies>

 <build>
<!--   这里-->
   <resources>
     <resource>
       <directory>src/main/java</directory>
       <includes>
         <include>**/*.xml</include>
       </includes>
     </resource>
     <resource>
       <directory>src/main/resources</directory>
       <includes>
         <include>*.xml</include>
         <include>*.properties</include>
       </includes>
     </resource>
   </resources>



   <finalName>com..ssm</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>

 

3.web.xml里面去配置springmvc,spring,字符编码过滤器,加载静态资源。

(web.xml相当于,这个程序的主入口,相当于main方法)。

<?xml version="1.0" encoding="UTF-8"?>
<!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>
 <display-name>Archetype Created Web Application</display-name>
 <!-- 启动Spring -->
 <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:spring.xml</param-value>
 </context-param>
 <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>

 <!-- Spring MVC -->
 <servlet>
   <servlet-name>dispatcherServlet</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>classpath:springmvc.xml</param-value>
   </init-param>
 </servlet>

 <servlet-mapping>
   <servlet-name>dispatcherServlet</servlet-name>
   <url-pattern>/</url-pattern>
 </servlet-mapping>

 <!-- 字符编码过滤器 -->
 <filter>
   <filter-name>characterEncodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <init-param>
     <param-name>encoding</param-name>
     <param-value>UTF-8</param-value>
   </init-param>
 </filter>
 <filter-mapping>
   <filter-name>characterEncodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!-- 加载静态资源 -->
 <servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.js</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.css</url-pattern>
 </servlet-mapping>
 <servlet-mapping>
   <servlet-name>default</servlet-name>
   <url-pattern>*.jpg</url-pattern>
 </servlet-mapping>
</web-app>

上面注意,需要在main包下面,创建一个resouces包(资源包)(需要,Resources Root),然后在这个包里面建xml文件(配置文件写在这个包下)

1659059621294

4.在spring.xml中去配置spring和mybatis的整合。

<?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:p="http://www.springframework.org/schema/p"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
">
   <!-- 整合mybatis-->
   <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
       <property name="user" value="root"></property> 数据库用户名
       <property name="password" value="123456"></property>   数据库密码
       <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/springboot-vue?useUnicode=true&amp;characterEncoding=utf-8&amp;allowMultiQueries=true&amp;useSSL=false&amp;serverTimezone=GMT%2b8"></property> 访问地址,数据库字段那些
       <property name="driverClass" value="com.mysql.jdbc.Driver"></property> 数据库驱动
       <property name="initialPoolSize" value="5"></property> 连接池数量
       <property name="maxPoolSize" value="10"></property> 连接池最大数量
   </bean>
   <!-- 配置mybatis sqlsessionFactory-->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
       <property name="dataSource" ref="dataSource"></property>
       <property name="mapperLocations" value="classpath:com/southwind/repository/mapper/*.xml"></property> 注意这里,到要扫描mapper下面的xml文件,也就是映射文件,这里错了好久
       <property name="configLocation" value="classpath:config.xml"></property>
   </bean>
   <!--扫描自定义的mapper接口-->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
       <property name="basePackage" value="com.southwind.repository.mapper"></property> 一直到自己的mapper包即可,错了好久

   </bean>
</beans>

5.config.xml配置一些mybatis的辅助信息,比如打印SQL等。(不是很必要)

<?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>
   
   <typeAliases>
<!--       指定一个包名,mybatis会在包名下搜索需要的JavaBean-->
       <package name="com.southwind.entity"/>
   </typeAliases>
</configuration>

 

6.配置springmvc

<?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:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

   <!-- 启动注解驱动 -->
   <mvc:annotation-driven></mvc:annotation-driven>

   <!-- 扫描业务代码 -->
   <context:component-scan base-package="com.southwind.repository"></context:component-scan> 扫描到这个目录就可以了,防止漏掉业务代码

   <!-- 配置视图解析器 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="prefix" value="/"></property>
       <property name="suffix" value=".jsp"></property>
   </bean>

</beans>

image-20220730205028618

因为不是springboot,我在这里用的jsp将数据返回给前端。就上面的index.jsp文件

<%--
 Created by IntelliJ IDEA.
 User: Administrator
 Date: 2022/7/29
 Time: 11:53
 To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
   <title>Title</title>
</head>
<body>
<c:forEach items="${list}" var="admin">
    ${admin.id}--${admin.name}--${admin.password}--${admin.nickName}--${admin.age}--${admin.sex}--${admin.address}--${admin.role}<br/>
</c:forEach>

</body>
</html>

 

 

配置到这里基本上就结束了,下面和springboot就差不多了

一般都是entity---》mapper--》service--》controller

7.实体类

package com.southwind.repository.entity;
import lombok.Data;
@Data
public class Admin {
   private Integer id;
   private String name;
   private String password;
   private String nickName;
   private Integer age;
   private String sex;
   private String address;
   private Integer role;
}

 

8.mapper层,一般有java和xml文件映射。

java接口
package com.southwind.repository.mapper;
import com.southwind.repository.entity.Admin;
import java.util.List;
public interface AdminMapper {
public List<Admin> findAll();
}
xml映射的SQL
<?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.southwind.repository.mapper.AdminMapper"> namespace命名空间,映射到mapper,确保映射。
<select id="findAll" resultType="Admin"> id 里面是mapper里面的方法名,resultType(结果类型),写类型
select * from admin
</select>
</mapper>
如果是update语句,就需要插入,标签就有parameterType,参数类型。

 

9.service层,一般有两个,接口直接在service下,具体实现在impl包里面。

接口
package com.southwind.repository.service;
import com.southwind.repository.entity.Admin;
import java.util.List;
public interface AdminServise {
List<Admin> findAll();
}
impl里面一般就是具体实现
package com.southwind.repository.service.impl;
import com.southwind.repository.entity.Admin;
import com.southwind.repository.mapper.AdminMapper;
import com.southwind.repository.service.AdminServise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class AdminServiceImpl implements AdminServise {
@Autowired
private AdminMapper adminMapper;

@Override 这个是重写,基本上都要写
public List<Admin> findAll() {
return adminMapper.findAll();
}
}

10.controller层

package com.southwind.repository.controller;
import com.southwind.repository.service.AdminServise;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {
@Autowired
private AdminServise adminServise;

@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView(); //springmvc会经常用到,模型和视图,因为不是springboot可以直接返回,就可以显示出来
modelAndView.setViewName("index");//将下面的的index.jsp,set到视图
modelAndView.addObject("list",adminServise.findAll());//将获取到的对象,添加到modelandview里面去。
System.out.println(adminServise.findAll());
return modelAndView;//将这个返回出来。
}
}

posted @ 2022-07-31 22:28  锦书南辞  阅读(3)  评论(0编辑  收藏  举报