一. SSM整合
1.1 新建一个动态web项目
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201101171659185.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNjA4MzI2NzU4,size_16,color_FFFFFF,t_70#pic_center)
1.2 导入需要的jar包
- jstl:
- mybatis:
- asm-3.3.1.jar
- cglib-2.2.2.jar
- commons-logging-1.1.1.jar
- javassist-3.17.1-GA.jar
- log4j-1.2.17.jar
- mybatis-3.2.1.jar
- slf4j-api-1.7.2.jar
- slf4j-log4j12-1.7.2.jar
- sevlet+jsp:
- jsp-api.jar
- servlet-api.jar
- spring:
- commons.logging-1.1.1.jar
- spring-aop-4.1.2.RELEASE.jar
- spring-beans-4.1.2.RELEASE.jar
- spring-context-4.1.2.RELEASE.jar
- spring-core-4.1.2.RELEASE.jar
- spring-expression-4.1.2.RELEASE.jar
- spring-test-4.1.2.RELEASE.jar
- spring+mybatis
- com.springsource.org.aopalliance-1.0.0.jar
- com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
- commons.dbcp-1.2.2.jar
- commons.pool-1.5.3.jar
- mybatis-spring-1.2.0.jar
- spring-jdbc-4.1.2.RELEASE.jar
- spring-tx-4.1.2.RELEASE.jar
- springmvc
- commons-fileupload-1.3.1.jar
- commons-io-2.2.jar
- jackson-annotations-2.5.0.jar
- jackson-core-2.5.0.jar
- jackson-databind-2.5.0.jar
- spring-web-4.1.2.RELEASE.jarss
- spring-webmvc-4.1.2.RELEASE.jar
- 数据库连接:
- mysql-connector-java-5.1.26-bin.jar
- 使用的服务器为tomcat,需要的童鞋可以百度自行下载配置
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201101171709950.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNjA4MzI2NzU4,size_16,color_FFFFFF,t_70#pic_center)
1.3 创建配置文件
- 新建资源文件夹source folder:
resources
![在这里插入图片描述](https://img-blog.csdnimg.cn/20201101171948537.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2ExNjA4MzI2NzU4,size_16,color_FFFFFF,t_70#pic_center)
- 新建数据库配置文件:
db.properties
#驱动全限定类名
jdbc.driverClassName=com.mysql.jdbc.Driver
#数据库连接地址
jdbc.url=jdbc:mysql://127.0.0.1:3306/你的数据库名?useUnicode=true&characterEncoding=UTF-8
#数据库用户名
jdbc.username=你的数据库用户名
#数据库密码
jdbc.password=你的数据库密码
- 新建log4j配置文件:
log4j.properties
控制台输出+自定义布局
log4j.rootLogger=DEBUG,my
#指定输出器
log4j.appender.my=org.apache.log4j.ConsoleAppender
#指定布局器(自定义布局)
#指定布局为自定义布局
log4j.appender.my.layout=org.apache.log4j.PatternLayout
#指定在自定义布局的格式,%d -- 表示当前系统时间,%t -- 执行该业务的线程名称,%p -- 日记器的级别,-5 -- 5表示输出字符的个数,符号表示右对齐
#%c -- 表示指定业务所在的类的完全限定名(包名.类名),%m -- 输出额外信息,%n -- 表示换行
log4j.appender.my.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n
#设置package(可以是自定义的包也可以是api的包)输出级别
log4j.logger.org.springframework=info
log4j.logger.cn.xxxx=debug
- Spirng配置文件:
applicationContext.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:context="http://www.springframework.org/schema/context"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<context:property-placeholder location="classpath:db.properties"/>
<bean id="basicDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="basicDataSource"></property>
<property name="mapperLocations" value="classpath:cn/xxxx/mapper/*Mapper.xml"></property>
<property name="typeAliasesPackage" value="cn.xxxx.domain"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.xxxx.mapper"></property>
</bean>
<context:component-scan base-package="cn.xxxx.service"></context:component-scan>
</beans>
- SpringMVC配置文件:
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:mvc="http://www.springframework.org/schema/mvc"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="cn.xxxx.controller"></context:component-scan>
<mvc:default-servlet-handler/>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.