SSM整合
Spring整合MyBatis和SpringMVC
SpringMVC是一个表述层框架,处理浏览器向服务器发送的请求,并且将数据响应到浏览器,管理控制层组件,其它的组件就要给Spring来管理-比如service层
MyBatis是一个持久层框架,帮助我们连接数据库,访问数据库、操作数据库;
Spring是整合型框架IOC、和AOP,比如SpringMVC的SqlSession就可以交给Spring来处理
不整合就是让SpringMVC和Spring来创建同一个容器,整合就是各自管理自己的组件
SpringMVC的Controller层组件是依赖于Spring的,Spring管理Service组件,所以在Controller层创建之前应该有Service,才能自动装配
4.1、ContextLoaderListener
监听器和过滤器是在Servlet之前执行的,所以可以配置
监听器是在服务器启动时候第一个执行的方法,比DispatcherServlet早,可以通过监听器创建Spring的IOC容器。当DispatcherServlet完成初始化的时候获取IOC容器,就可以完成Controller的初始化
在服务器启动的时候加载Spring的配置文件,来获取Spring的IOC容器
Spring提供了监听器ContextLoaderListener,实现ServletContextListener接口,可监听
ServletContext的状态,在web服务器的启动,读取Spring的配置文件,创建Spring的IOC容器。web
应用中必须在web.xml中配置

这是别人写好的代码,统一规定的位置
在web.xml中配置,classpath:spring.xml这个对应我们创建的spring.xml里面配置的有扫描组件
为什么SpringMVC中能够为Spring创建bean(不同的IOC能够互相访问?)?
在源码WebApplicationContext中它就创建了Spring为父容器
子容器是可以访问到父容器中的bean的(SpringMVC中的IOC可以访问到Spring中的IOC)
?<listener> ? ? ?<!--这里配置到了web.xml ? ? ? ? ?配置Spring的监听器,在服务器启动时加载Spring的配置文件 ? ? ? ? ?Spring配置文件默认位置和名称:/WEB-INF/applicationContext.xml ? ? ? ? ?可通过上下文参数自定义Spring配置文件的位置和名称 ? ? ?--> ? ? ?<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> ?</listener> ?<!--自定义Spring配置文件的位置和名称,名字和配置SpringMVC的name一样--> ?<context-param> ? ? ?<param-name>contextConfigLocation</param-name> ? ? ?<param-value>classpath:spring.xml</param-value> ?</context-param>
4.2、准备工作
①创建Maven Module
②导入pom依赖
?<packaging>war</packaging>
?<!--这个是spring的版本,用的还是比较多的,统一管理各个依赖的版本-->
?<properties>
? ? ?<spring.version>5.3.1</spring.version>
?</properties>
?<dependencies>
? ? ?<dependency>
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-context</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<dependency>
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-beans</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<!--springmvc-->
? ? ?<dependency>
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-web</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<dependency>
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-webmvc</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<!--用的是MyBatis为什么还导入这个jdbc Jar包?因为事务管理器在Spring-jdbc中-->
? ? ?<dependency>
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-jdbc</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<dependency>
? ? ? ? ?<!--spring管理切面-->
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-aspects</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<dependency>
? ? ? ? ? ?<!--spring整合junit-->
? ? ? ? ?<groupId>org.springframework</groupId>
? ? ? ? ?<artifactId>spring-test</artifactId>
? ? ? ? ?<version>${spring.version}</version>
? ? ?</dependency>
? ? ?<!-- Mybatis核心依赖 -->
? ? ?<dependency>
? ? ? ? ?<groupId>org.mybatis</groupId>
? ? ? ? ?<artifactId>mybatis</artifactId>
? ? ? ? ?<version>3.5.7</version>
? ? ?</dependency>
? ? ?<!--mybatis和spring的整合包(spring整合MyBatis的整合包,比如提供了SqlSessionFactoryBean)-->
? ? ?<dependency>
? ? ? ? ?<groupId>org.mybatis</groupId>
? ? ? ? ?<artifactId>mybatis-spring</artifactId>
? ? ? ? ?<version>2.0.6</version>
? ? ?</dependency>
? ? ?<!-- 连接池 -->
? ? ?<dependency>
? ? ? ? ?<groupId>com.alibaba</groupId>
? ? ? ? ?<artifactId>druid</artifactId>
? ? ? ? ?<version>1.0.9</version>
? ? ?</dependency>
? ? ?<!-- junit测试 -->
? ? ?<dependency>
? ? ? ? ?<groupId>junit</groupId>
? ? ? ? ?<artifactId>junit</artifactId>
? ? ? ? ?<version>4.12</version>
? ? ? ? ?<scope>test</scope>
? ? ?</dependency>
? ? ?<!-- MySQL驱动 -->
? ? ?<dependency>
? ? ? ? ?<groupId>mysql</groupId>
? ? ? ? ?<artifactId>mysql-connector-java</artifactId>
? ? ? ? ?<version>8.0.16</version>
? ? ?</dependency>
? ? ?<!-- log4j日志 -->
? ? ?<dependency>
? ? ? ? ?<groupId>log4j</groupId>
? ? ? ? ?<artifactId>log4j</artifactId>
? ? ? ? ?<version>1.2.17</version>
? ? ?</dependency>
? ? ?<!-- 分页插件https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> ?
? ? ?<dependency>
? ? ?<groupId>com.github.pagehelper</groupId>
? ? ?<artifactId>pagehelper</artifactId>
? ? ?<version>5.2.0</version>
? ? ?</dependency>
? ? ?<!-- slf4j日志(门面)的实现 -->
? ? ?<dependency>
? ? ? ? ?<groupId>ch.qos.logback</groupId>
? ? ? ? ?<artifactId>logback-classic</artifactId>
? ? ? ? ?<version>1.2.3</version>
? ? ?</dependency>
? ? ?<!-- ServletAPI SpringMVC的DispatcherServlet前端控制器间接继承了Servlet中的HttpServlet,-->
? ? ?<dependency>
? ? ? ? ?<groupId>javax.servlet</groupId>
? ? ? ? ?<artifactId>javax.servlet-api</artifactId>
? ? ? ? ?<version>3.1.0</version>
? ? ? ? ?<scope>provided</scope>
? ? ?</dependency>
? ? ? ?<!--SpringMVC中处理json的依赖-->
? ? ?<dependency>
? ? ? ? ?<groupId>com.fasterxml.jackson.core</groupId>
? ? ? ? ?<artifactId>jackson-databind</artifactId>
? ? ? ? ?<version>2.12.1</version>
? ? ?</dependency>
? ? ?<!--文件上传-->
? ? ?<dependency>
? ? ? ? ?<groupId>commons-fileupload</groupId>
? ? ? ? ?<artifactId>commons-fileupload</artifactId>
? ? ? ? ?<version>1.3.1</version>
? ? ?</dependency>
? ? ?<!-- Spring5和Thymeleaf整合包(Spring整合Thymeleaf的整合包) -->
? ? ?<dependency>
? ? ? ? ?<groupId>org.thymeleaf</groupId>
? ? ? ? ?<artifactId>thymeleaf-spring5</artifactId>
? ? ? ? ?<version>3.0.12.RELEASE</version>
? ? ?</dependency>
?</dependencies>
③创建表
?CREATE TABLE `t_emp` ( ? ? ?`emp_id` int(11) NOT NULL AUTO_INCREMENT, ? ? ?`emp_name` varchar(20) DEFAULT NULL, ? ? ?`age` int(11) DEFAULT NULL, ? ? ?`sex` char(1) DEFAULT NULL, ? ? ?`email` varchar(50) DEFAULT NULL, ? ? ?PRIMARY KEY (`emp_id`) ?) ENGINE=InnoDB DEFAULT CHARSET=utf8
4.3、配置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"> ?? ? ? ?<!-- 配置Spring的编码过滤器,两个都设置请求响应都能过滤 --> ? ? ?<filter> ? ? ? ? ?<filter-name>CharacterEncodingFilter</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>forceEncoding</param-name> ? ? ? ? ? ? ?<param-value>true</param-value> ? ? ? ? ?</init-param> ? ? ?</filter> ? ? ?<filter-mapping> ? ? ? ? ?<filter-name>CharacterEncodingFilter</filter-name> ? ? ? ? ?<url-pattern>/*</url-pattern> ? ? ?</filter-mapping> ?? ? ? ?<!-- 配置处理请求方式PUT和DELETE的过滤器 --> ? ? ?<filter> ? ? ? ? ?<filter-name>HiddenHttpMethodFilter</filter-name> ? ? ? ? ?<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class> ? ? ?</filter> ? ? ?<filter-mapping> ? ? ? ? ?<filter-name>HiddenHttpMethodFilter</filter-name> ? ? ? ? ?<url-pattern>/*</url-pattern> ? ? ?</filter-mapping> ?? ? ? ?<!-- 配置SpringMVC的前端控制器 --> ? ? ?<servlet> ? ? ? ? ?<servlet-name>DispatcherServlet</servlet-name> ? ? ? ? ?<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> ? ? ? ? ?<!-- 设置SpringMVC的配置文件的位置和名称 --> ? ? ? ? ?<init-param> ? ? ? ? ? ? ?<param-name>contextConfigLocation</param-name> ? ? ? ? ? ? ?<param-value>classpath:springmvc.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> ?? ? ? ? <!-- 配置Spring的监听器,在服务器启动时自动加载Spring的配置文件 --> ? ? ?<listener> ? ? ? ? ?<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> ? ? ?</listener> ? ? ?<!-- 设置Spring的指定配置文件的位置和名称 --> ? ? ?<context-param> ? ? ? ? ?<param-name>contextConfigLocation</param-name> ? ? ? ? ?<param-value>classpath:spring.xml</param-value> ? ? ?</context-param> ?</web-app>
4.4、创建SpringMVC的配置文件并配置
只有控制层需要交给SpringMVC管理,其它的交给Spring管理
?<!--扫描组件(管理的是控制层)--> ?<context:component-scan base-package="com.atguigu.ssm.controller"> ?</context:component-scan> ?<!--配置视图解析器--> ?<bean id="viewResolver" ? ? ? ?class="org.thymeleaf.spring5.view.ThymeleafViewResolver"> ? ? ?<property name="order" value="1"/> ? ? ?<property name="characterEncoding" value="UTF-8"/> ? ? ?<property name="templateEngine"> ? ? ? ? ?<bean class="org.thymeleaf.spring5.SpringTemplateEngine"> ? ? ? ? ? ? ?<property name="templateResolver"> ? ? ? ? ? ? ? ? ?<bean ? ? ? ? ? ? ? ? ? ? ? ?class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver"> ? ? ? ? ? ? ? ? ? ? ?<!-- 视图前缀 --> ? ? ? ? ? ? ? ? ? ? ?<property name="prefix" value="/WEB-INF/templates/"/> ? ? ? ? ? ? ? ? ? ? ?<!-- 视图后缀 --> ? ? ? ? ? ? ? ? ? ? ?<property name="suffix" value=".html"/> ? ? ? ? ? ? ? ? ? ? ?<property name="templateMode" value="HTML5"/> ? ? ? ? ? ? ? ? ? ? ?<property name="characterEncoding" value="UTF-8" /> ? ? ? ? ? ? ? ? ?</bean> ? ? ? ? ? ? ?</property> ? ? ? ? ?</bean> ? ? ?</property> ?</bean> ?<!-- 配置访问首页的视图控制,如果DispatcherServlet处理不了才会执行下面的default-servlet-handler --> ?<mvc:view-controller path="/" view-name="index"></mvc:view-controller> ?<!-- 配置默认的servlet处理静态资源 --> ?<mvc:default-servlet-handler /> ?<!-- 开启MVC的注解驱动 --> ?<mvc:annotation-driven /> ? ?<!-- 配置访问首页的视图控制 --> ? ? ?<mvc:view-controller path="/" view-name="index"></mvc:view-controller> ? ? ?<!--处理文件上传的的;必须通过文件解析器的解析才能将文件转换为MultipartFile对象,id必须设置为这个,底层通过id来匹配的bean--> ? ? ?<bean id="multipartResolver" ? ? ? ? ? ?class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> ? ? ?</bean> ??
4.5、搭建MyBatis环境
①创建属性文件jdbc.properties
?jdbc.driver=com.mysql.cj.jdbc.Driver ?jdbc.url=jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC ?jdbc.username=root ?jdbc.password=132456
②创建MyBatis的核心配置文件mybatis-config.xml
将其它的都交给Spring配置了,插件可映射驼峰也可以交给Spring来管理
?<?xml version="1.0" encoding="UTF-8" ?> ?<!DOCTYPE configuration ?PUBLIC "-//mybatis.org//DTD Config 3.0//EN" ?"http://mybatis.org/dtd/mybatis-3-config.dtd"> ?<configuration> ? ? ?<settings> ? ? ? ? ?<!--将下划线映射为驼峰--> ? ? ? ? ?<setting name="mapUnderscoreToCamelCase" value="true"/> ? ? ?</settings> ? ? ?<plugins> ? ? ? ? ?<!--配置分页插件--> ? ? ? ? ?<plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin> ? ? ?</plugins> ?</configuration>
MyBatis-config.xml
MyBatis中是面向接口实现语句的,仅创建接口即可
?<?xml version="1.0" encoding="UTF-8" ?>
?<!DOCTYPE configuration
? ? ? ? ?PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
? ? ? ? ?"http://mybatis.org/dtd/mybatis-3-config.dtd">
?<configuration>
? ? ?<!--
? ? ? ? ?MyBatis核心配置文件中的标签必须要按照指定的顺序配置:
? ? ? ? ?properties?,settings?,typeAliases?,typeHandlers?,
? ? ? ? ?objectFactory?,objectWrapperFactory?,reflectorFactory?,
? ? ? ? ?plugins?,environments?,databaseIdProvider?,mappers?
? ? ?-->
? ? ?<properties resource="jdbc.properties"/>
? ? ?<settings>
? ? ? ? ?<setting name="mapUnderscoreToCamelCase" value="true"/>
? ? ?</settings>
? ? ?<typeAliases>
? ? ? ? ?<package name=""/>
? ? ?</typeAliases>
? ? ?<!--
? ? ? ? ?environments:配置连接数据库的环境
? ? ? ? ?属性:default:设置默认使用的环境的id
? ? ?-->
? ? ?<environments default="development">
? ? ? ? ?<!--environment:设置一个具体的连接数据库的环境-->
? ? ? ? ?<environment id="development">
? ? ? ? ? ? ?<transactionManager type="JDBC"/>
? ? ? ? ? ? ?<dataSource type="POOLED">
? ? ? ? ? ? ? ? ?<property name="driver" value="${jdbc.driver}"/>
? ? ? ? ? ? ? ? ?<property name="url" value="${jdbc.url}"/>
? ? ? ? ? ? ? ? ?<property name="username" value="${jdbc.username}"/>
? ? ? ? ? ? ? ? ?<property name="password" value="${jdbc.password}"/>
? ? ? ? ? ? ?</dataSource>
? ? ? ? ?</environment>
? ? ? ? ?<environment id="test">
? ? ? ? ? ? ?<transactionManager type="JDBC"/>
? ? ? ? ? ? ?<dataSource type="POOLED">
? ? ? ? ? ? ? ? ?<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
? ? ? ? ? ? ? ? ?<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"/>
? ? ? ? ? ? ? ? ?<property name="username" value="root"/>
? ? ? ? ? ? ? ? ?<property name="password" value="123456"/>
? ? ? ? ? ? ?</dataSource>
? ? ? ? ?</environment>
? ? ?</environments>
? ? ?<!--引入mybatis的映射文件-->
? ? ?<mappers>
? ? ? ? ?<package name=""/>
? ? ?</mappers>
?</configuration>
? ? ? ? ?<!-- ? 以下是 详解 注释
? ? ? ? ?<?xml version="1.0" encoding="UTF-8" ?>
? ? ? ? ?<!DOCTYPE configuration
? ? ? ? ? ? ? ? ?PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
? ? ? ? ? ? ? ? ?"http://mybatis.org/dtd/mybatis-3-config.dtd">
?<configuration>
?<!–
? ? ?MyBatis核心配置文件中的标签必须要按照指定的顺序配置:
? ? ?properties?,settings?,typeAliases?,typeHandlers?,
? ? ?objectFactory?,objectWrapperFactory?,reflectorFactory?,
? ? ?plugins?,environments?,databaseIdProvider?,mappers?
?–>
??
?<!–引入properties文件,此后就可以在当前文件中使用的方式访问value–>
?<properties resource="jdbc.properties"/>
??
?<!–
? ? ?typeAliases:设置类型别名,即为某个具体的类型设置一个别名
? ? ?在MyBatis的范围中,就可以使用别名表示一个具体的类型
?–>
?<typeAliases>
? ? ?<!– type:设置需要起别名的类型 alias:设置某个类型的别名 –>
? ? ?<!–<typeAlias type="com.atguigu.mybatis.pojo.User" alias="abc"></typeAlias>–>
? ? ?<!–若不设置alias,当前的类型拥有默认的别名,即类名且不区分大小写–>
? ? ?<!–<typeAlias type="com.atguigu.mybatis.pojo.User"></typeAlias>–>
? ? ?<!–通过包设置类型别名(只写typeAlias,不写alias情况下),指定包下所有的类型将全部拥有默认的别名,即类名且不区分大小写(建议写成类名)
? ? ?以后开发中一张表对应一个实体类,对应一个mapper接口,对应一个映射文件,这样也是比较麻烦的
? ? ?实体类虽然有很多,但是以后肯定是统一放在同一包下的,那这个时候就可以将实体类所对应的包设置到package这个标签中 ,这个时候这个包下面的所有类将全部拥有默认的别名-即类名不区分大小写(比如用到User的地方可以改啦)
? ? ?–>
? ? ?<package name="com.atguigu.mybatis.pojo"/>
?</typeAliases>
?<!–
? ? ?environments:配置连接数据库的环境
? ? ?属性:
? ? ?default:设置默认使用的环境的id
?–>
?<environments default="development">
? ? ?<!–
? ? ? ? ?environment:设置一个具体的连接数据库的环境
? ? ? ? ?属性:
? ? ? ? ?id:设置环境的唯一标识,不能重复
? ? ?–>
? ? ?<environment id="development">
? ? ? ? ?<!–
? ? ? ? ? ? ?transactionManager:设置事务管理器
? ? ? ? ? ? ?属性:
? ? ? ? ? ? ?type:设置事务管理的方式
? ? ? ? ? ? ?type="JDBC|MANAGED"
? ? ? ? ? ? ?JDBC:表示使用JDBC中原生的事务管理方式
? ? ? ? ? ? ?MANAGED:被管理,例如Spring
? ? ? ? ?–>
? ? ? ? ?<transactionManager type="JDBC"/>
? ? ? ? ?<!–
? ? ? ? ? ? ?dataSource:设置数据源
? ? ? ? ? ? ?属性:
? ? ? ? ? ? ?type:设置数据源的类型
? ? ? ? ? ? ?type="POOLED|UNPOOLED|JNDI"
? ? ? ? ? ? ?POOLED:表示使用数据库连接池
? ? ? ? ? ? ?UNPOOLED:表示不使用数据库连接池
? ? ? ? ? ? ?JNDI:表示使用上下文中的数据源
? ? ? ? ?–>
? ? ? ? ?<dataSource type="POOLED">
? ? ? ? ? ? ?<property name="driver" value="${jdbc.driver}"/>
? ? ? ? ? ? ?<property name="url" value="${jdbc.url}"/>
? ? ? ? ? ? ?<property name="username" value="${jdbc.username}"/>
? ? ? ? ? ? ?<property name="password" value="${jdbc.password}"/>
? ? ? ? ?</dataSource>
? ? ?</environment>
??
? ? ?<environment id="test">
? ? ? ? ?<transactionManager type="JDBC"/>
? ? ? ? ?<dataSource type="POOLED">
? ? ? ? ? ? ?<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
? ? ? ? ? ? ?<property name="url" value="jdbc:mysql://localhost:3306/ssm?serverTimezone=UTC"/>
? ? ? ? ? ? ?<property name="username" value="root"/>
? ? ? ? ? ? ?<property name="password" value="123456"/>
? ? ? ? ?</dataSource>
? ? ?</environment>
?</environments>
??
?<!–引入mybatis的映射文件–>
?<mappers>
? ? ?<!–<mapper resource="mappers/UserMapper.xml"/>–>
? ? ?<!–
? ? ? ? ?以包的方式引入映射文件,但是必须满足两个条件:
? ? ? ? ?1、mapper接口和映射文件所在的包必须一致
? ? ? ? ?2、mapper接口的名字和映射文件的名字必须一致
? ? ?–>
? ? ?<package name="com.atguigu.mybatis.mapper"/>
?</mappers>
?</configuration>
?-->
③创建Mapper接口和映射文件
持久层只需要有一个接口就行,语句放到映射文件中
?public interface EmployeeMapper {
?List<Employee> getEmployeeList();
?}
?<?xml version="1.0" encoding="UTF-8" ?> ?<!DOCTYPE mapper ?PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" ?"http://mybatis.org/dtd/mybatis-3-mapper.dtd"> ?<mapper namespace="com.atguigu.ssm.mapper.EmployeeMapper"> ? ? ?<select id="getEmployeeList" resultType="Employee"> ? ? ? ? select * from t_emp ? ? ?</select> ?</mapper>
④创建日志文件log4j.xml
?<?xml version="1.0" encoding="UTF-8" ?>
?<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
?<!--报红也没事-->
?<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
? ? ?<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
? ? ? ? ?<param name="Encoding" value="UTF-8" />
? ? ? ? ?<layout class="org.apache.log4j.PatternLayout">
? ? ? ? ? ? ?<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n" />
? ? ? ? ?</layout>
? ? ?</appender>
? ? ?<logger name="java.sql">
? ? ? ? ?<level value="debug" />
? ? ?</logger>
? ? ?<logger name="org.apache.ibatis">
? ? ? ? ?<level value="info" />
? ? ?</logger>
? ? ?<root>
? ? ? ? ?<level value="debug" />
? ? ? ? ?<appender-ref ref="STDOUT" />
? ? ?</root>
?</log4j:configuration>
4.6、创建Spring的配置文件并配置
只要是对象都可以交给Spring来管理
?<!--<typeAlias type="com.atguigu.mybatis.pojo.User"></typeAlias>--> ? <!--以包为单位,将包下所有的类型设置默认的类型别名,即类名且不区分大小写-->
?<?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"> ?
? ? ? <!--扫描组件(除控制层)-->
? ? ?<context:component-scan base-package="com.atguigu.ssm">
? ? ? ? ?<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
? ? ?</context:component-scan>
? ? ?<!-- 引入jdbc.properties,classpath就是resources下,也就是target下面的WEB-INF下的classes -->
? ?<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
? ? ?<!-- 配置Druid数据源,德鲁伊数据源是个对象,可以交给Spring来管理,Spring整合MyBatis时也可以让MyBatis使用Spring中的数据源 -->
? ? ?<!--resources下(同目录)创建jdbc.properties-->
? ? ?<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
? ? ? ? ?<property name="driverClassName" value="${jdbc.driver}"></property>
? ? ? ? ?<property name="url" value="${jdbc.url}"></property>
? ? ? ? ?<property name="username" value="${jdbc.username}"></property>
? ? ? ? ?<property name="password" value="${jdbc.password}"></property>
? ? ?</bean>
? ? ? ?<!--配置事务管理器-一般都是对于连接对象,所有要配置数据源;它依赖于spring-jdbc的jar包-->
? ? ?<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
? ? ? ? ?<property name="dataSource" ref="dataSource"></property>
? ? ?</bean>
? ? ?<!--
? ? ? ? ?开启事务的注解驱动,如果id和事务管理器一样可以省略,建议不要省略
? ? ? ? ?将使用注解@Transactional标识的方法或类中的所有方法作为连接点,通过切面进行事务管理,在Service注解@Translational
? ? ?-->
? ? ?<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
? ? ?<!--SqlSessionFactory提供的对象就是SqlSessionFactory,如果不是工厂bean,我们要先获取IOC再获取工厂的这个bean,
?然后在通过工厂获取我们所提供的对象-->
? ? ?<!--我们用了这个工厂bean就可以省略掉获取工厂的步骤,直接在IOC容器中获取工厂所提供的对象SqlSessionFactory对象-->
? ? ?<!-- 配置用于创建SqlSessionFactory的工厂bean(获取工厂提供的对象,而非真的对象),由MyBatis-spring的jar包提供的 -->
? ? ?<!--之前创建SqlSession需要通过核心配置文件,获取输入流,根据输入流获取SqlSession;万变不离其宗,交给Spring管理依旧同理-->
? ? ?<bean class="org.mybatis.spring.SqlSessionFactoryBean">
? ? ? ? ?<!-- 设置MyBatis配置文件的路径(可以不设置-如果全部都让Spring完全可以不要这个配置,哪一个都行,这里设置了MyBatis核心配置文件,在MyBatis和Spring里配置都可以)-->
? ? ? ? ?<property name="configLocation" value="classpath:mybatis-config.xml"> </property>
? ? ? ? ?<!-- 设置数据源 ,在这里设置了,在MyBatis中就不用配置了-->
? ? ? ? ?<property name="dataSource" ref="dataSource"></property>
? ? ? ? ?<!-- 设置类型别名所对应的包 -->
? ? ? ? ?<property name="typeAliasesPackage" value="com.atguigu.ssm.pojo">
? ? ? ? ?</property>
? ? </bean>
? ? ? ? ?<!--
? ? ? ? ? ? ?设置映射文件的路径,设置了MyBatis就不需要引入映射文件了
? ? ? ? ? ? ?若映射文件所在路径和mapper接口所在路径一致,则不需要设置;
? location是路径不是包,所以不能用.点,要用/
? ? ? ? ?-->
? ? ? ? ?<!--
? ? ? ? ? ? ?<property name="mapperLocations" value="classpath:mapper/*.xml">
? ? ? ? ? ? ?</property>
? ? ? ? ?-->
? ? ? ? ?<!--ConfigurationProperties 里面需要传properties对象,把MyBatis中的将下划线映射为驼峰的name设置为键,value映射为值;MyBatis就可以不写了-->
? ?
? ? ?<!--
?(可以想象如果不配置,在Service层注解装配一个SqlSessionFactory对象,通过其创建SqlSession,再通过getMapping方法获取Mapper接口对象;这也是挺麻烦的)
? ? ?配置mapper接口的扫描配置
? 通过SqlSessionFactory所提供的SqlSession对象来创建这些Mapper接口的代理实现类对象,然后交给IOC容器来管理
? ? ?由mybatis-spring提供,可以将指定包下所有的mapper接口创建动态代理
? ? ? ? ?并将这些动态代理作为IOC容器的bean管理
? ? ?-->
? ? ?<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
? ? ? ? ?<!--把om.atguigu.ssm.mapper下面所有的接口,通过Spring所提供的SqlSessionFactory创建出来的SqlSession,
? 通过这些SqlSession获取这个包下面所有的代理实现类对象,并且将这些对象交给IOC容器管理
? 设置了这个mapper它是mapper接口和映射文件共同的包
? -->
? ? ? ? ?<property name="basePackage" value="com.atguigu.ssm.mapper"></property>
? ? ?</bean>
?</beans>

插件交给Spring也可以:

4.7、测试功能
①创建组件
实体类Employee
?public class Employee {
? ? ?private Integer empId;
? ? ?private String empName;
? ? ?private Integer age;
? ? ?private String sex;
? ? ?private String email;
? ? ?public Employee() {
? ? }
? ? ?public Employee(Integer empId, String empName, Integer age, String sex,
? ? ? ? ? ? ? ? ? ? ?String email) {
? ? ? ? ?this.empId = empId;
? ? ? ? ?this.empName = empName;
? ? ? ? ?this.age = age;
? ? ? ? ?this.sex = sex;
? ? ? ? ?this.email = email;
? ? }
? ? ?public Integer getEmpId() {
? ? ? ? ?return empId;
? ? }
? ? ?public void setEmpId(Integer empId) {
? ? ? ? ?this.empId = empId;
? ? }
? ? ?public String getEmpName() {
? ? ? ? ?return empName;
? ? }
? ? ?public void setEmpName(String empName) {
? ? ? ? ?this.empName = empName;
? ? }
? ? ?public Integer getAge() {
? ? ? ? ?return age;
? ? }
? ? ?public void setAge(Integer age) {
? ? ? ? ?this.age = age;
? ? }
? ? ?public String getSex() {
? ? ? ? ?return sex;
? ? }
? ? ?public void setSex(String sex) {
? ? ? ? ?this.sex = sex;
? ? }
? ? ?public String getEmail() {
? ? ? ? ?return email;
? ? }
? ? ?public void setEmail(String email) {
? ? ? ? ?this.email = email;
? ? }
?}
创建控制层组件EmployeeController
??
?/**
? * Date:2022/7/11
? * Author:ybc
? * Description:
? * 查询所有的员工信息-->/employee-->get
? * 查询员工的分页信息-->/employee/page/1-->get
? * 根据id查询员工信息-->/employee/1-->get
? * 跳转到添加页面-->/to/add-->get
? * 添加员工信息-->/employee-->post
? * 修改员工信息-->/employee-->put
? * 删除员工信息-->/employee/1-->delete
? */
?@Controller
?public class EmployeeController {
? ? ?@Autowired
? ? ?private EmployeeService employeeService;
?//查询在 RESTful风格中是get,共享了这个page就不能同时用下面查询所有员工的方法了,因为我们共享的数据是page,而当前页的员工信息是在page里面的list属性中
? ? ?@RequestMapping(value = "/employee/page/{pageNum}", method = RequestMethod.GET)
? ? ?public String getEmployeePage(@PathVariable("pageNum") Integer pageNum, Model model){
? ? ? ? ?//获取员工的分页信息
? ? ? ? ?PageInfo<Employee> page = employeeService.getEmployeePage(pageNum);
? ? ? ? ?//将分页数据共享到请求域中
? ? ? ? ?model.addAttribute("page", page);
? ? ? ? ?//跳转到employee_list.html
? ? ? ? ?return "employee_list";
? ? }
??
? ? ?@RequestMapping(value = "/employee", method = RequestMethod.GET)
? ? ?public String getAllEmployee(Model model){
? ? ? ? ?//查询所有的员工信息
? ? ? ? ?List<Employee> list = employeeService.getAllEmployee();
? ? ? ? ?//将员工信息在请求域中共享
? ? ? ? ?model.addAttribute("list", list);
? ? ? ? ?//跳转到employee_list.html
? ? ? ? ?return "employee_list";
? ? }
?}
创建接口EmployeeService
?public interface EmployeeService {
? ?/**
? ? ? * 查询所有的员工信息
? ? ? * @return
? ? ? */
? ? ?List<Employee> getAllEmployee();
??
? ? ?/**
? ? ? * 获取员工的分页信息
? ? ? * @param pageNum
? ? ? * @return
? ? ? */
? ? ?PageInfo<Employee> getEmployeePage(Integer pageNum);
?}
创建实现类EmployeeServiceImpl
?@Service
?@Transactional
?public class EmployeeServiceImpl implements EmployeeService {
? ? ?@Autowired
? ? ?private EmployeeMapper employeeMapper;
? ? ?/* @Override
? ? ?public List<Employee> getAllEmployee() {
? ? ? ? ?return employeeMapper.getAllEmployee();
? ? ?}
? ? ?*/
? ? ?@Override
? ? ?public PageInfo<Employee> getEmployeePage(Integer pageNum) {
? ? ? ? ?//开启分页功能
? ? ? ? ?PageHelper.startPage(pageNum, 4);
? ? ? ? ?//查询所有的员工信息
? ? ? ? ?List<Employee> list = employeeMapper.getAllEmployee();
? ? ? ? ?//获取分页相关数据
? ? ? ? ?PageInfo<Employee> page = new PageInfo<>(list, 5);
? ? ? ? ?return page;
? ? }
?}


②创建页面
?<!DOCTYPE html>
?<html lang="en" xmlns:th="http://www.thymeleaf.org">
? ? ?<head>
? ? ? ? ?<meta charset="UTF-8">
? ? ? ? ?<title>Employee Info</title>
? ? ? ? ?<link rel="stylesheet" th:href="@{/static/css/index_work.css}">
? ? ?</head>
? ? ?<body>
? ? ? ? ?<table>
? ? ? ? ? ? ?<tr>
? ? ? ? ? ? ? ? ?<th colspan="6">Employee Info</th>
? ? ? ? ? ? ?</tr>
? ? ? ? ? ? ?<tr>
? ? ? ? ? ? ? ? ?<th>emp_id</th>
? ? ? ? ? ? ? ? ?<th>emp_name</th>
? ? ? ? ? ? ? ? ?<th>age</th>
? ? ? ? ? ? ? ? ?<th>sex</th>
? ? ? ? ? ? ? ? ?<th>email</th>
? ? ? ? ? ? ? ? ?<th>options</th>
? ? ? ? ? ? ?</tr>
? ? ? ? ? ? ?<!--这里应该是page.list,共享域是page,list是它的一个属性-->
? ? ? ? ? ? ?<tr th:each="employee : ${page.list}">
? ? ? ? ? ? ? ? ?<td th:text="${employee.empId}"></td>
? ? ? ? ? ? ? ? ?<td th:text="${employee.empName}"></td>
? ? ? ? ? ? ? ? ?<td th:text="${employee.age}"></td>
? ? ? ? ? ? ? ? ?<td th:text="${employee.sex}"></td>
? ? ? ? ? ? ? ? ?<td th:text="${employee.email}"></td>
? ? ? ? ? ? ? ? ?<td>
? ? ? ? ? ? ? ? ? ? ?<a href="">delete</a>
? ? ? ? ? ? ? ? ? ? ?<a href="">update</a>
? ? ? ? ? ? ? ? ?</td>
? ? ? ? ? ? ?</tr>
? ? ? ? ? ? ?<tr>
? ? ? ? ? ? ? ? ?<td colspan="6">
? ? ? ? ? ? ? ? ? ? ?<span th:if="${page.hasPreviousPage}">
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:href="@{/employee/page/1}">首页</a>
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:href="@{'/employee/page/'+${page.prePage}}">上一页</a>
? ? ? ? ? ? ? ? ? ? ?</span>
? ? ? ? ? ? ? ? ? ? ?<!--num表示当前导航分页的页码,-->
? ? ? ? ? ? ? ? ? ? ?<span th:each="num : ${page.navigatepageNums}">
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:if="${page.pageNum==num}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? th:href="@{'/employee/page/'+${num}}" th:text="'['+${num}+']'" style="color:red;"></a>
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:if="${page.pageNum!=num}"
? ? ? ? ? ? ? ? ? ? ? ? ? ? th:href="@{'/employee/page/'+${num}}" th:text="${num} "></a>
? ? ? ? ? ? ? ? ? ? ?</span>
? ? ? ? ? ? ? ? ? ? ?<span th:if="${page.hasNextPage}">
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:href="@{'/employee/page/'+${page.nextPage}}">下一页</a>
? ? ? ? ? ? ? ? ? ? ? ? ?<a th:href="@{'/employee/page/'+${page.pages}}">末页</a>
? ? ? ? ? ? ? ? ? ? ?</span>
? ? ? ? ? ? ? ? ?</td>
? ? ? ? ? ? ?</tr>
? ? ? ? ?</table>
? ? ?</body>
?</html>
employee_list.html 新版源文件:
?<!DOCTYPE html>
?<html lang="en" xmlns:th="http://www.thymeleaf.org">
?<head>
? ? ?<meta charset="UTF-8">
? ? ?<title>员工列表</title>
? ? ?<link rel="stylesheet" th:href="@{/static/css/index_work.css}">
?</head>
?<body>
?<table>
? ? ?<tr>
? ? ? ? ?<th colspan="6">员工列表</th>
? ? ?</tr>
? ? ?<tr>
? ? ? ? ?<th>流水号</th>
? ? ? ? ?<th>员工姓名</th>
? ? ? ? ?<th>年龄</th>
? ? ? ? ?<th>性别</th>
? ? ? ? ?<th>邮箱</th>
? ? ? ? ?<th>操作</th>
? ? ?</tr>
? ? ? ? <!--这里可以设置一个状态(任意起名如:status,Thymeleaf为我们提供的辅助对象,帮助我们获取循环的一些信息),里面有一些属性,这里为了让每一页列从1开始-->
? ? ?<tr th:each="employee,status : ${page.list}">
? ? ? ? ?<td th:text="${status.count}"></td>
? ? ? ? ?<td th:text="${employee.empName}"></td>
? ? ? ? ?<td th:text="${employee.age}"></td>
? ? ? ? ?<td th:text="${employee.gender}"></td>
? ? ? ? ?<td th:text="${employee.email}"></td>
? ? ? ? ?<td>
? ? ? ? ? ? ?<a href="">删除</a>
? ? ? ? ? ? ?<a href="">修改</a>
? ? ? ? ?</td>
? ? ?</tr>
?</table>
?<div style="text-align: center;">
? ? ?<a th:if="${page.hasPreviousPage}" th:href="@{/employee/page/1}">首页</a>
? ? ?<a th:if="${page.hasPreviousPage}" th:href="@{'/employee/page/'+${page.prePage}}">上一页</a>
? <!--num是当前导航分页展示的页码,当前数组循环出来是几就给num,从而让页面展示几-->
? ? ?<span th:each="num : ${page.navigatepageNums}">
? ? ? ? ?<!--设置我们当前访问的页码变色+[]-->
? ? ? ? ?<a th:if="${page.pageNum == num}" style="color: red;" th:href="@{'/employee/page/'+${num}}" th:text="'['+${num}+']'"></a>
? ? ? ? ?<a th:if="${page.pageNum != num}" th:href="@{'/employee/page/'+${num}}" th:text="${num}"></a>
? ? ?</span>
? ? ?<a th:if="${page.hasNextPage}" th:href="@{'/employee/page/'+${page.nextPage}}">下一页</a>
? ? ?<a th:if="${page.hasNextPage}" th:href="@{'/employee/page/'+${page.pages}}">末页</a>
?</div>
?</body>
?</html>
③访问测试分页功能
localhost:8080/employee/page/1
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。 如若内容造成侵权/违法违规/事实不符,请联系我的编程经验分享网邮箱:veading@qq.com进行投诉反馈,一经查实,立即删除!
