亲宝软件园·资讯

展开

使用IDEA搭建SSM框架的详细教程(spring + springMVC +MyBatis)

想不到啥名字 人气:2

1 框架组成

Spring

SpringMVC

MyBatis

2 所需工具

Mysql 8.0.15

数据库管理系统,创建数据库

Tomcat 8.5.51

用于部署web项目

Maven 3.6.1

项目构建、项目依赖管理

lombok 1.18.10(可用可不用工具)

用于类注解创建setter、getter、无参构造、全参构造、toString等函数

注:只导入依赖,不安装插件是不起作用的

3 搭建步骤

3.1 新建一个空Maven项目,填写项目相关信息,完成

在这里插入图片描述

3.2 添加web框架支持

在这里插入图片描述

选择现有框架支持

在这里插入图片描述

3.3 pom.xml导入依赖,设置Maven资源过滤

<!--导入依赖-->
<dependencies>
 <!--Junit-->
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.12</version>
 <https://img.qb5200.com/download-x/dependency>

 <!--数据库驱动-->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.15</version>
 <https://img.qb5200.com/download-x/dependency>

 <!-- 数据库连接池 -->
 <dependency>
 <groupId>com.mchange</groupId>
 <artifactId>c3p0</artifactId>
 <version>0.9.5.2</version>
 <https://img.qb5200.com/download-x/dependency>

 <!--Servlet - JSP -->
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5</version>
 <https://img.qb5200.com/download-x/dependency>
 <dependency>
 <groupId>javax.servlet.jsp</groupId>
 <artifactId>jsp-api</artifactId>
 <version>2.2</version>
 <https://img.qb5200.com/download-x/dependency>
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 <https://img.qb5200.com/download-x/dependency>

 <!--Mybatis-->
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis</artifactId>
 <version>3.5.2</version>
 <https://img.qb5200.com/download-x/dependency>
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-spring</artifactId>
 <version>2.0.2</version>
 <https://img.qb5200.com/download-x/dependency>

 <!--Spring-->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>5.1.9.RELEASE</version>
 <https://img.qb5200.com/download-x/dependency>

 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>5.1.9.RELEASE</version>
 <https://img.qb5200.com/download-x/dependency>

 <!--lombok-->
 <dependency>
 <groupId>org.projectlombok</groupId>
 <artifactId>lombok</artifactId>
 <version>1.18.10</version>
 <https://img.qb5200.com/download-x/dependency>
<https://img.qb5200.com/download-x/dependencies>

<!--静态资源导出问题-->
<build>
 <resources>
 <resource>
  <directory>src/main/java<https://img.qb5200.com/download-x/directory>
  <includes>
  <include>**/*.properties</include>
  <include>**/*.xml</include>
  </includes>
  <filtering>false</filtering>
 </resource>
 <resource>
  <directory>src/main/resources<https://img.qb5200.com/download-x/directory>
  <includes>
  <include>**/*.properties</include>
  <include>**/*.xml</include>
  </includes>
  <filtering>false</filtering>
 </resource>
 </resources>
</build>

3.4 编写MyBatis-config.xml(核心配置文件)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 "http://mybatis.orghttps://img.qb5200.com/download-x/dtd/mybatis-3-config.dtd">
<configuration>
 
 <!--设置运行日志-->
 <settings>
 <setting name="logImpl" value="STDOUT_LOGGING"/>
 </settings>
 
 <!--取别名-->
 <typeAliases>
 <package name="com.pojo"/>
 </typeAliases>
 
 <!--绑定mapper,根据自己的项目设置-->
 <mappers>
 <mapper resource="comhttps://img.qb5200.com/download-x/dao/Mapper.xml"/>
 </mappers>

</configuration>

3.5 编写database.properties(数据库配置文件)

jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=utf8
jdbc.username=数据库用户名
jdbc.password=数据库密码

​根据自己的MySQL以及项目实际使用的数据库来修改设置

​注:MySQL8.0以上驱动得使用com.mysql.cj.jdbc.Driver

3.6 编写Spring-dao.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"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
 http://www.springframework.org/schema/beans/spring-beans.xsd
 http://www.springframework.org/schema/context
 https://www.springframework.org/schema/context/spring-context.xsd">

 <!-- 配置整合mybatis -->
 <!-- 1.关联数据库文件 -->
 <context:property-placeholder location="classpath:database.properties"/>

 <!-- 2.数据库连接池 -->
 <!--数据库连接池
 dbcp 半自动化操作 不能自动连接
 c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 <!-- 配置连接池属性 -->
 <property name="driverClass" value="${jdbc.driver}"/>
 <property name="jdbcUrl" value="${jdbc.url}"/>
 <property name="user" value="${jdbc.username}"/>
 <property name="password" value="${jdbc.password}"/>

 <!-- c3p0连接池的私有属性 -->
 <property name="maxPoolSize" value="30"/>
 <property name="minPoolSize" value="10"/>
 <!-- 关闭连接后不自动commit -->
 <property name="autoCommitOnClose" value="false"/>
 <!-- 获取连接超时时间 -->
 <property name="checkoutTimeout" value="10000"/>
 <!-- 当获取连接失败重试次数 -->
 <property name="acquireRetryAttempts" value="2"/>
 </bean>

 <!-- 3.配置SqlSessionFactory对象 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <!-- 注入数据库连接池 -->
 <property name="dataSource" ref="dataSource"/>
 <!-- 配置MyBaties全局配置文件:MyBatis-config.xml -->
 <property name="configLocation" value="classpath:MyBatis-config.xml"/>
 </bean>

 <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
 <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <!-- 注入sqlSessionFactory -->
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
 <!-- 给出需要扫描Dao接口包 -->
 <property name="basePackage" value="com.dao"/>
 </bean>

</beans>

3.7 编写Spring-service.xml(Spring整合service层)

<?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"
 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">

 <!-- 扫描service相关的bean -->
 <context:component-scan base-package="com.service" />

 <!--ServiceImpl注入到IOC容器中,此处需要修改成自己的-->
 <bean id="ServiceImpl" class="com.service.ServiceImpl">
 <property name="Mapper" ref="Mapper"/>
 </bean>

 <!-- 配置事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <!-- 注入数据库连接池 -->
 <property name="dataSource" ref="dataSource" />
 </bean>

</beans>

3.8 修改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">

 <!--DispatcherServlet-->
 <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:applicationContext.xml</param-value>
 </init-param>
 <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
 <servlet-name>DispatcherServlet</servlet-name>
 <url-pattern>/</url-pattern>
 </servlet-mapping>

 <!--encodingFilter-->
 <filter>
 <filter-name>encodingFilter</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>encodingFilter</filter-name>
 <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!--Session过期时间-->
 <session-config>
 <session-timeout>15</session-timeout>
 </session-config>
</web-app>

3.9 编写Spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
 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/mvc
 https://www.springframework.org/schema/mvc/spring-mvc.xsd">

 <!-- 配置SpringMVC -->
 <!-- 1.开启SpringMVC注解驱动 -->
 <mvc:annotation-driven />
 <!-- 2.静态资源默认servlet配置-->
 <mvc:default-servlet-handler/>

 <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
 <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
 <!--此处注意路径问题,/WEB-INF/jsp/-->
 <property name="prefix" value="/WEB-INF/jsp/" />
 <property name="suffix" value=".jsp" />
 </bean>

 <!-- 4.扫描web相关的bean -->
 <context:component-scan base-package="com.controller" />
</beans>

3.10 编写applicationContext.xml(Spring配置整合文件)

<?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">

 <!--将Spring其他配置文件整合到一个总的配置文件,用的时候使用这个配置文件-->
 <import resource="classpath:Spring-service.xml"/>
 <import resource="classpath:Spring-dao.xml"/>
 <import resource="classpath:Spring-mvc.xml"/>

</beans>

3.11 配置Tomcat

在这里插入图片描述

3.12 检查项目结构(左上角 文件 -> 项目结构)

在这里插入图片描述

3.13 最后的项目文件结构

在这里插入图片描述

​到了这里,框架已经搭建完成

4 接口对应的Mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 "http://mybatis.orghttps://img.qb5200.com/download-x/dtd/mybatis-3-mapper.dtd">

<!--绑定对应的接口-->
<mapper namespace="com.dao.Mapper">
 <!--此处写对应的SQL操作-->
</mapper>

5 功能添加步骤

  • 先编写实体类(pojo)
  • dao层:编写接口,接口对应mapper.xml(建议同名)
  • service层:编写接口,编写接口实现类(创建dao层对象,返回调用dao层的操作)
  • controller层:负责具体的业务模块流程的控制,在此层要调用service层的接口来控制业务流程
  • 编写相应的jsp文件

6 建议

框架搭建完成后应写个简单的功能测试框架环境有无问题

7 SSM框架项目文件

http://xiazai.jb51.net/202005/yuanma/ssm_kuangjia_jb51.rar

总结

加载全部内容

相关教程
猜你喜欢
用户评论