上一章内容:
1.spring框架:作用 开源的框架--提供IOC和AOPIOC控制反转 把创建对象的权力交于spring创建,并管理对象的生命周期,通过DI完成对象属性的注入。 2. spring配置中<bean>也可以使用注解@Component @Controller @Service @Repository 3. 自动注入: @Autowired 4. 注入的方式: set方法注入 构造方法注入。
2.SSM整合
2.1 ssm的整合
springmvc spring mybatis三个框架的整合。起始是spring和mybatis的整合。 只是把mybatis的配置内容整合到spring配置文件中。因为tomcat启动时会加载spring配置。
数据源 2. 配置映射文件
2.2 maven的web工程并加入依赖
pom.xml:
<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/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.zyl</groupId><artifactId>ssm02</artifactId><packaging>war</packaging><version>1.0-SNAPSHOT</version><name>ssm02 Maven Webapp</name><url>http://maven.apache.org</url><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><spring-version>5.2.10.RELEASE</spring-version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>3.8.1</version><scope>test</scope></dependency><!--文件上传依赖--><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.5</version></dependency><!-- Commons FileUpload --><dependency><groupId>commons-fileupload</groupId><artifactId>commons-fileupload</artifactId><version>1.4</version></dependency><!-- Commons IO --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.8.0</version></dependency><!--阿里云存储--><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.17.4</version></dependency><!--springmvc--><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>${spring-version}</version></dependency><!--mybatis--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.17</version></dependency><!--mybatis整合spring的依赖.3.0以后支持8以上--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.7</version></dependency><!--spring-jdbc依赖--><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>${spring-version}</version></dependency><!--mysql依赖--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.19</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.34</version></dependency><!--servlet--><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version></dependency><!--jackson--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.18.1</version></dependency><!--druid连接池: 存放连接数据库对象的池子。--><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.23</version></dependency><!--jstl--><dependency><groupId>jstl</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency></dependencies><build><finalName>ssm02</finalName></build>
</project>
2.3 写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"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"><!--===========================先配置springmvc的内容=================================--><!--包扫描--><context:component-scan base-package="com.zyl"/><!--开启注解驱动--><mvc:annotation-driven/><!--放行静态资源--><mvc:default-servlet-handler/><!--视图解析器--><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/views/"/><property name="suffix" value=".jsp"/></bean><!--文件上传解析器--><bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!--最大上传的大小。单位byte字节。5M=5*1024*1024--><property name="maxUploadSize" value="5242880"/><!--设置文件上传的编码--><property name="defaultEncoding" value="utf-8"/></bean><!--========再配置spring 的内容===============================================--><!--配置数据源:可以存放多个连接数据库的对象Connecttion--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/><property name="url" value="jdbc:mysql://localhost:3306/bb?serverTimezone=Asia/Shanghai"/><property name="username" value="root"/><property name="password" value="root"/><!--连接池最大的连接个数--><property name="maxActive" value="10"/><!--连接池初始的容量--><property name="initialSize" value="5"/><!--最大的等待时长:--><property name="maxWait" value="6000"/></bean><!--SqlSessionFactoryBean:该类就是用于spring整合mybatis配置文件--><bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!--指定 上面的数据源--><property name="dataSource" ref="dataSource"/><!--指定 映射文件的路径--><property name="mapperLocations" value="classpath:/mapper/*.xml"/></bean><!--为dao接口实现代理实现类StudentDao studentDao =sqlSession.getMapper(studentDao.class)并且生成完成后交与spring容器管理--><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!--为指定包下的所有的dao类接口生成代理实现类 --><property name="basePackage" value="com.zyl.mapper"/><property name="sqlSessionFactoryBeanName" value="sessionFactory"/></bean></beans>
2.4 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"><welcome-file-list><welcome-file>login1.jsp</welcome-file></welcome-file-list><!--编码过滤器--><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><!--是否设置请求的编码--><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><!--是否设置响应的编码--><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping><!--加载spring配置文件--><servlet><servlet-name>springmvc</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>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping></web-app>
2.5 生成mapper接口和映射文件和实体类
代码自动生成器:
2.6 编码过滤
自己写一个类,写编码过滤器、或者spring也我提供了一个编码过滤器。
<!--定义编码过滤器--><filter><filter-name>encodingFilter</filter-name><filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class><!--指定编码的值。默认ISO-8859-1--><init-param><param-name>encoding</param-name><param-value>utf-8</param-value></init-param><!--是否设置请求的编码--><init-param><param-name>forceRequestEncoding</param-name><param-value>true</param-value></init-param><!--是否设置响应的编码--><init-param><param-name>forceResponseEncoding</param-name><param-value>true</param-value></init-param></filter><filter-mapping><filter-name>encodingFilter</filter-name><url-pattern>/*</url-pattern></filter-mapping>
controller层,
实体类,
(dao)Mapper.java,
Mapper.xml映射文件,
前端网页代码.jsp
具体整合源代码在我的项目ssm02中,记得去看!!!