SSM+JSP+Bootstrap+Ajax小实例(员工CRUD系统)(二:编写ssm相关配置文件)

        原文再续,书接上回,接下来进入最恶心的一步(我自己认为极端恶心):编写ssm相关的配置文件。

        在进入恶心的配置编写之前,我们先进行一个令人(起码是令我)心情愉悦的工作:创建包。我们在src/main/java下创建com.sunsy.crud.bean,com.sunsy.crud.dao,com.sunsy.crud.service,com.sunsy.crud.controller,com.sunsy.crud.utils,com.sunsy.crud.test等几个空包。

        接下来我们正式开始编写配置文件,首先我们写一下web.xml,各个配置的作用我都写在了文件的注释里,各位看官自己看吧,我就不再敲一遍了,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">

	<!-- 需要加载的spring配置文件,可以配多个,在value里用逗号隔开 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>

	<!-- 启动容器时自动装配applicationContext里边的配置信息 -->
	<listener>
    	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	
	<!-- 提供Spring Web MVC的集中访问点,而且负责职责的分派,可去源码或百度谷歌查询该类的作用 -->
	<servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <!-- 字符编码过滤器,防止web开发中出现的乱码问题 -->
	<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>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>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	
	<!-- 使用Rest风格,浏览器form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,
	spring3.0添加了该过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、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>
	
	
</web-app>

        接下来,我们在WEB-INF下创建dispatcherServelet-servlet.xml文件,注意:SpringMVC默认配置文件为"/WEB-INF/[servlet名字]-servlet.xml",只要以-servlet.xml结尾就成,叫啥无所谓,配置如下(别忘了创建views文件夹,我在配置文件注释里写了):

<?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/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
    	">
    
    <!-- Spring默认配置文件为"/WEB-INF/[servlet名字]-servlet.xml" -->

	<!-- SpringMVC配置文件 -->
	<context:component-scan base-package="com.sunsy" use-default-filters="false">
		<!-- 只扫描controller控制器 -->
		<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 配置视图解析器,需要我们自己再WEB-INF下创建一个views文件夹,以后我们的jsp文件就放在这里。。当然你想起别的名字也可以 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/WEB-INF/views/"></property>
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 如果发现是静态资源的请求,就将该请求转由Web应用服务器默认的Servlet处理,
		    如果不是静态资源的请求,才由DispatcherServlet继续处理 -->
	<mvc:default-servlet-handler/>
	
	<!-- 如果没有该项,所有的请求都不会走controller,不会映射动态请求 -->
	<mvc:annotation-driven/>
   
</beans>

        接下来是Spring配置文件,我们放在/src/main/resources文件夹下,名字是applicationContext.xml,在配置文件里,我们配置了mybatis的Mapper文件的存放位置是/src/main/resources/mapper文件夹,所以需要你手动创建该文件夹配置如下:

<?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
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    ">

	<!-- 该配置文件Spring配置文件 -->
	
	<!-- 不再扫描带有controller注解的类 -->
	<context:component-scan base-package="com.sunsy">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	</context:component-scan>
	
	<!-- 配置数据源、事务控制 -->
	<context:property-placeholder location="classpath:dbconfig.properties"/>
	<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- 配置MyBatis整合相关 -->   
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 指定mybatis全局配置文件位置 -->
		<property name="configLocation" value="classpath:mybatis-config.xml"></property>
		<!-- mybatis数据源,上边配置的那个 -->
		<property name="dataSource" ref="pooledDataSource"></property>
		<!-- mapper文件的位置 -->
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
	</bean>

	<!-- 配置扫描器,把mybatis接口的实现加入到ioc容器中 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="com.sunsy.crud.dao"></property>
	</bean>
	
	<!-- 事务控制相关 ,题外话:在springboot里这个DatasourceTransactionManager会被自动配置,方便多了,下边这些配置就都不用了-->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="pooledDataSource"></property>
	</bean>
	
	<!-- 基于注解的事务 -->
	<aop:config>
		<aop:pointcut expression="execution(* com.sunsy.crud.service..*(..))" id="txPoint"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
	</aop:config>
	
	<!-- 事务增强,事务如何切入 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- 所有的方法都是事务方法 -->
			<tx:method name="*"/>
			<!-- get开头的所有方法都是readonly的 -->
			<tx:method name="get*" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	
</beans>

        以上的配置文件在配置数据源的时候我们用了另外一个配置文件,dbconfig.properties,该配置文件内容如下:

jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=123456

        最后一个配置文件是mybatis的主配置文件,我是直接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>
	<settings>
		<!-- 驼峰命名规则 -->
  		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>
	<typeAliases>
		<!-- 批量定义别名(别名为实体类下的类名,第一个字母大小写都可以) -->
		<package name="com.sunsy.crud.bean"/>
	</typeAliases>
</configuration>

        OK,最恶心的配置文件环节结束了,用惯了Springboot的我对这些玩意已经基本忘了。。多亏尚**公开课帮我回忆起来不少,弄清这些配置其实对理解springboot里的注解也是有很大帮助的。下一讲我们会使用mybatis的逆向工程来生成表对应的实体类,mapper接口类以及mapper.xml文件,方便的一。。。

        最后我们来点轻松愉悦的工作为下次逆向工程做准备,这个工作就是。。。创建表。首先创建一个名为ssm_crud的数据

库,然后导入如下两个sql文件生成tbl_emp表和tbl_dept表,sql如下:

/*
Navicat MySQL Data Transfer

Source Server         : 127.0.0.1Mysql
Source Server Version : 50718
Source Host           : localhost:3306
Source Database       : ssm_crud

Target Server Type    : MYSQL
Target Server Version : 50718
File Encoding         : 65001

Date: 2018-12-12 19:37:47
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tbl_emp
-- ----------------------------
DROP TABLE IF EXISTS `tbl_emp`;
CREATE TABLE `tbl_emp` (
  `emp_id` int(11) NOT NULL AUTO_INCREMENT,
  `emp_name` varchar(255) NOT NULL,
  `gender` char(1) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `d_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`emp_id`),
  KEY `fk_emp_dept` (`d_id`),
  CONSTRAINT `fk_emp_dept` FOREIGN KEY (`d_id`) REFERENCES `tbl_dept` (`dept_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
/*
Navicat MySQL Data Transfer

Source Server         : 127.0.0.1Mysql
Source Server Version : 50718
Source Host           : localhost:3306
Source Database       : ssm_crud

Target Server Type    : MYSQL
Target Server Version : 50718
File Encoding         : 65001

Date: 2018-12-12 19:37:40
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for tbl_dept
-- ----------------------------
DROP TABLE IF EXISTS `tbl_dept`;
CREATE TABLE `tbl_dept` (
  `dept_id` int(11) NOT NULL AUTO_INCREMENT,
  `dept_name` varchar(255) NOT NULL,
  PRIMARY KEY (`dept_id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;

        OK,下次我们就能用mybatis的逆向工程又快又好的生成各种文件了,欲知后事如何,且待下回分解。

猜你喜欢

转载自blog.csdn.net/u014627099/article/details/84974986