使⽤IDEA搭建SSM框架的详细教程(spring+springMVC+MyBatis)⽬录
1 框架组成
2 所需⼯具
3 搭建步骤
4 接⼝对应的l
5 功能添加步骤
6 建议
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.l导⼊依赖,设置Maven资源过滤<!--导⼊依赖-->
<dependencies>
<!--Junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--数据库驱动-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
<!-- 数据库连接池 -->
<dependency>
<groupId&hange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.2</version>
</dependency>
<!--Servlet - JSP -->
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--Mybatis-->
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId&batis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.2</version>
</dependency>
<!--Spring-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.9.RELEASE</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
</dependencies>
<!--静态资源导出问题-->
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
ssm框架简单吗</resources>
</build>
3.4 编写l(核⼼配置⽂件)<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-////DTD Config 3.0//EN" "/dtd/mybatis-3-config.dtd">
<configuration>
<!--设置运⾏⽇志-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/> </settings>
<!--取别名-->
<typeAliases>
<package name="com.pojo"/>
</typeAliases>
<!--绑定mapper,根据⾃⼰的项⽬设置-->
<mappers>
<mapper resource="com/l"/>
</mappers>
3.5 编写database.properties(数据库配置⽂件)
jdbc.sql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=true&useUnicode=true&characterEncoding=utf8 jdbc.username=数据库⽤户名
jdbc.password=数据库密码
根据⾃⼰的MySQL以及项⽬实际使⽤的数据库来修改设置
注:MySQL8.0以上驱动得使⽤sql.cj.jdbc.Driver
3.6 编写l(Spring整合MyBatis配置⽂件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd">
<!-- 配置整合mybatis -->
<!-- 1.关联数据库⽂件 -->
<context:property-placeholder location="classpath:database.properties"/>
<!-- 2.数据库连接池 -->
<!--数据库连接池
dbcp 半⾃动化操作不能⾃动连接
c3p0 ⾃动化操作(⾃动的加载配置⽂件并且设置到对象⾥⾯)
-->
<bean id="dataSource" class="hange.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="batis.spring.SqlSessionFactoryBean">
<!-- 注⼊数据库连接池 -->
<property name="dataSource" ref="dataSource"/>
<!-- 配置MyBaties全局配置⽂件:l -->
<property name="configLocation" value="l"/>
</bean>
<!-- 4.配置扫描Dao接⼝包,动态实现Dao接⼝注⼊到spring容器中 -->
<!--解释:wwwblogs/jpfss/p/7799806.html-->
<bean class="batis.spring.mapper.MapperScannerConfigurer">
<!-- 注⼊sqlSessionFactory -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!-- 给出需要扫描Dao接⼝包 -->
<property name="basePackage" value="com.dao"/>
</bean>
</beans>
3.7 编写l(Spring整合service层)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/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 修改l⽂件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="/xml/ns/javaee"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/xml/ns/javaee /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>l</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 编写l
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xmlns:context="/schema/context"
xmlns:mvc="/schema/mvc"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd
/schema/context
/schema/context/spring-context.xsd
/schema/mvc
/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="ller" />
</beans>
3.10 编写l(Spring配置整合⽂件)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="/schema/beans"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/schema/beans
/schema/beans/spring-beans.xsd">
<!--将Spring其他配置⽂件整合到⼀个总的配置⽂件,⽤的时候使⽤这个配置⽂件-->
<import resource="l"/>
<import resource="l"/>
<import resource="l"/>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。