文章目录
一、开发环境准备
- JDK1.8
- MySQL8
- idea2021
- Tomcat8.5.87
- Apache-Maven3.9
二、搭建SSM
2.1新建Maven项目
【File】-> 【new Project】-> 【选择Archetype】
2.2项目整体结构
项目的整体结构: 包括resources
目录下的xml和webapp
目录下文件。如果不包含某些文件,就自己创建。
MVC结构、spring-config.xml(applicationContext.xml)、jdbc.properties(数据源)、mybatis-config.xml(mybatis配置类)、spring-mvc.xml(springMVC前端控制器配置类)、web.xml(web项目配置类)
2.3spring-config.xml配置
<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"> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="myDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${database.url}"/> <property name="username" value="${database.username}"/> <property name="password" value="${database.password}"/> bean> <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="myDataSource"/> <property name="configLocation" value="classpath:mybatis-config.xml"/> bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="factory"/> <property name="basePackage" value="com.shenxm.mapper"/> bean>beans>
2.4jdbc.properties配置
database.driver=com.mysql.cj.jdbc.Driverdatabase.url=jdbc:mysql://localhost:3306/demodatabase.username=rootdatabase.password=123456
2.5mybatis-config.xml配置
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration> <properties resource="jdbc.properties"/> <settings> <setting name="logImpl" value="STDOUT_LOGGING"/> <setting name="autoMappingBehavior" value="PARTIAL"/> <setting name="mapUnderscoreToCamelCase" value="true"/> <setting name="lazyLoadingEnabled" value="true"/> settings> <mappers> <package name="com.shenxm.mapper"/> mappers>configuration>
2.6spring-mvc.xml配置
<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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <mvc:annotation-driven /> <context:component-scan base-package="com.shenxm"/>beans>
2.7web.xml配置
<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"> <filter> <filter-name>CharacterEncodingFilterfilter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class> <init-param> <param-name>encodingparam-name> <param-value>UTF-8param-value> init-param> filter> <filter-mapping> <filter-name>CharacterEncodingFilterfilter-name> <url-pattern> private Integer id; private String name; private Integer age;}
2.11测试
tomcat8.5配置启动
访问页面:(成功)
来源地址:https://blog.csdn.net/love_study1314/article/details/129974446