功能说明:
- 分页
- 数据校验
- jquery前端校验 + jsr303后端校验
- ajax
- Rest 风格的API
使用的技术:
- ssm
- mysql
- bootstrap
- maven
- 分页
- 逆向工程
- MyBatis Generator
查询页面:
- 访问 index.jsp 页面
- index.jsp 页面发送出查询员工列表请求
- EmployeeController 来接受请求,查出员工数据
- 来到 list.jsp 页面进行展示
查询 ajax:
- index.jsp页面直接发送ajax请求进行员工分页数据的查询
- 服务器将查出的数据,以 json 字符串的形式返回给浏览器
- 浏览器收到 json 字符串,可以使用 js 对 json 进行解析,使用 js 通过dom 增删改改变页面
- 返回 json,实现客户端无关性
依赖引入
在 pom.xml 中引入相关依赖:
<dependencies><!-- spring环境和springMVC --><dependency><groupId>org.springframework</groupId><artifactId>spring-webmvc</artifactId><version>5.3.1</version></dependency><!-- spring测试 --><dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.3.1</version></dependency><!-- AOP和AspectJ --><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.1</version></dependency><!-- 事务管理器 --><dependency><groupId>org.springframework</groupId><artifactId>spring-jdbc</artifactId><version>5.3.1</version></dependency><!-- Servlet --><dependency><groupId>javax.servlet</groupId><artifactId>javax.servlet-api</artifactId><version>4.0.1</version><scope>provided</scope></dependency><!-- jsp-api --><dependency><groupId>javax.servlet</groupId><artifactId>jsp-api</artifactId><version>2.0</version><scope>provided</scope></dependency><!-- jstl标签库 --><dependency><groupId>javax.servlet</groupId><artifactId>jstl</artifactId><version>1.2</version></dependency><!-- 测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.13.1</version><scope>test</scope></dependency><!-- 数据库连接 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.22</version></dependency><!-- 数据库连接池 --><dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.2.3</version></dependency><!-- mybatis和mybatis-spring整合包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.6</version></dependency><dependency><groupId>org.mybatis</groupId><artifactId>mybatis-spring</artifactId><version>2.0.6</version></dependency><!-- 分页插件 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper</artifactId><version>5.2.0</version></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.16</version></dependency><!-- MBG:mybatis代码生成器 --><dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.4.0</version></dependency><!-- jackson --><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.0</version></dependency></dependencies>
配置文件
1、web.xml:web容器的配置文件
<!-- 启动Spring父容器 --><context-param><!-- 父容器配置文件位置 --><param-name>contextConfigLocation</param-name><param-value>classpath:springContext.xml</param-value></context-param><!-- 配置spring核心监听器,默认会以 /WEB-INF/applicationContext.xml 作为配置文件 --><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><!-- 配置前端控制器 --><servlet><servlet-name>dispatcherServlet</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><!-- web容器启动时加载该Servlet --><load-on-startup>1</load-on-startup></servlet><servlet-mapping><!-- 拦截所有请求 --><servlet-name>dispatcherServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping><!-- 配置字符编码过滤器:一定要放在所有过滤器之前 --><filter><filter-name>encoding</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>encoding</filter-name><url-pattern>/*</url-pattern></filter-mapping><!-- Rest风格的URI:将页面普通的post请求转为指定的delete或post请求 --><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>
2、springMVC.xml:SpringMVC配置文件
<?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.example" use-default-filters="false"><!-- 只扫描控制器注解,需要禁用默认规则 --><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- 配置视图解析器 --><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/views/"/><property name="suffix" value=".jsp"/></bean><!-- 两个标准配置 --><!-- 静态资源映射,即添加 /** (优先级最低)的映射指向default的Servlet --><mvc:default-servlet-handler/><!-- 开启mvc注解驱动,JSR303校验、ajax、映射动态请求等高级功能 --><mvc:annotation-driven/></beans>
3、springContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"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/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- Spring的配置文件,配置主要业务逻辑 --><!-- 数据源,事务控制,mybatis整合 --><!-- ======================= 组件扫描 =================================== --><!-- 组件扫描 --><context:component-scan base-package="com.example"><!-- 除外Controller的类 --><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/></context:component-scan><!-- ======================= 数据源 =================================== --><!-- 引入数据库连接的配置文件 --><context:property-placeholder location="classpath:db.properties"/><!-- 配置Druid数据源 --><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driverClassName}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><!-- ======================= MyBatis整合 =================================== --><!-- 配置MyBatis整合 --><bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><!-- 指定mybatis的全局配置文件的位置 --><property name="configLocation" value="classpath:mybatis-config.xml"/><!-- 指定数据源 --><property name="dataSource" ref="dataSource"/><!-- mapper文件位置 --><property name="mapperLocations" value="classpath:mapper/*.xml"/></bean><!-- 配置扫描器,将mybatis接口的实现加入到ioc容器中 --><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><!-- 扫描所有dao接口的实现,加入到ioc容器中 --><property name="basePackage" value="com.example.dao"/></bean><!-- 配置一个可以批量执行的SqlSession --><bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory"/><constructor-arg name="executorType" value="BATCH"/></bean><!-- ======================= 事务管理 =================================== --><!-- 配置事务管理 --><bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><!-- 数据源 --><property name="dataSource" ref="dataSource" /></bean><!-- 开启基于注解的事务,使用xml配置形式的事务(必要主要的都是使用配置式) --><aop:config><!-- 切入点表达式 --><aop:pointcut id="txPoint" expression="execution(* com.example.service..*(..))"/><!-- 配置事务增强 --><aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/></aop:config><!-- 配置事务增强,事务如何切入 --><!-- 即存在多个事务管理器时,需要使用transaction-manager指定 --><tx:advice id="txAdvice"><tx:attributes><!-- 所有方法都是事务方法 --><tx:method name="*"/><!-- 以get开始的所有方法 --><tx:method name="get*" read-only="true"/></tx:attributes></tx:advice></beans>
