校园商铺-2项目设计和框架搭建-6逐层完成SSM的各项配置

1. 创建/src/main/resources/jdbc.properties文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/o2o?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123456

2. 创建/src/main/resources/mybatis-config.xml

<?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>
		<!-- 使用jdbc的getGeneratedKeys获取数据库自增主键值️-->
		<setting name="useGeneratedKeys" value="true" />
		<!-- 使用列别名替换列名-->
		<setting name="useColumnLabel" value="true" />
		<!-- 开启驼峰命名替换:Table(create_time)->Entity{createTime}-->
		<setting name="mapUnderscoreToCamelCase" value="true" />
	</settings>
</configuration>

3. 创建/src/main/resources/spring/spring-dao.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"
       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">
	<!--配置整合mybatis过程-->
	<!--1.配置数据库相关参数properties的属性:${url}-->
	<context:property-placeholder location="classpath:jdbc.properties" />
	<!--2.数据库连接池-->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" >
		<!--配置连接池属性-->
		<property name="driverClass" value="${jdbc.driver}" />
		<property name="jdbcUrl" value="${jdbc.url}" />
		<property name="user" value="${jdbc.username}" />
		<property name="password" value="${jdbc.password}" />
		<!--c3p0连接池的私有属性-->
		<property name="maxPoolSize" value="30" />
		<property name="minPoolSize" value="10" />
		<!--关闭连接后不自动commit-->
		<property name="autoCommitOnClose" value="false" />
		<!--获取连接超时时间-->
		<property name="checkoutTimeout" value="10000" />
		<!--当获取连接失败后,重试次数-->
		<property name="acquireRetryAttempts" value="2" />
	</bean>
	<!--3.配置SqlSessionFactory对象-->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!--注入数据库连接池-->
		<property name="dataSource" ref="dataSource" />
		<!--配置MyBatis全局配置文件:mybatis-config.xml-->
		<property name="configLocation" value="classpath:mybatis-config.xml" />
		<!--扫描entity包,使用别名-->
		<property name="typeAliasesPackage" value="com.csj2018.o2o.entity" />
		<!--扫描sql配置文件:mapper需要的xml文件-->
		<property name="mapperLocations" value="classpath:mapper/*.xml" />
	</bean>
	<!--4.配置扫描Dao接口包,动态实现Dao接口,注入到Spring容器中-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--注入sqlSessionFactory-->
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
		<!--给出需要扫描Dao接口包-->
		<property name="basePackage" value="com.csj2018.o2o.dao" />
	</bean>
</beans>

4. 创建/src/main/resources/spring/spring-service.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: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/tx  
    http://www.springframework.org/schema/tx/spring-tx.xsd">  
    <!-- 扫描service包下所有使用注解的类型 -->  
    <context:component-scan base-package="com.csj2018.o2o.service" />  
  
    <!-- 配置事务管理器 -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <!-- 注入数据库连接池 -->  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  
    <!-- 配置基于注解的声明式事务 -->  
    <tx:annotation-driven transaction-manager="transactionManager" />  
</beans>

6. 创建/src/main/resources/spring/spring-web.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: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
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
    <!-- 配置SpringMVC -->
    <!-- 1.开启SpringMVC注解模式 自动识别controller类,不需要xml中做bean的配置-->
    <mvc:annotation-driven />
    <!-- 2.静态资源默认servlet配置
    	(1)加入对静态资源的处理:js,gif,png
    	(2)允许使用"/"做整体映射 -->
    <mvc:resources mapping="/resources/**" location="/resources/" />
    <mvc:default-servlet-handler />
    <!-- 3.定义视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
    	<property name="prefix" value="/WEB_INF/html/"></property>
    	<property name="suffix" value=".html"></property>
    </bean>
    <!-- 4.扫描web相关的bean -->
    <context:component-scan base-package="com.csj2018.o2o.web" />
</beans>

7. 修改/src/main/webapp/WEB-INF/web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app version="3.1"
	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_3_1.xsd"
	metadata-complete="true">
	<display-name>Archetype Created Web Application</display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
	<servlet>
		<servlet-name>spring-dispatcher</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-*.xml</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>spring-dispatcher</servlet-name>
		<!-- 默认匹配所有的请求 -->
		<url-pattern>/</url-pattern>
	</servlet-mapping>
</web-app>

8. 总结

*    1.首先在pom.xml中指定工程所依赖的jar包
*    2.在jdbc.properties中指定了数据库的连接方式
*    3.创建mybatis-config文件对mybatis做配置
*    4.创建spring-dao文件,将jdbc.properties和mybatiso-config都加载了进来,创建dataSource连接池,同时配置了mybatis与数据库进行交互的方式
*    5.创建spring-service配置文件,用于事务管理将spring-dao配置到的dataSource注入到事务管理器里面,便于service层做操纵
*    6.创建spring-web文件,定义dispatcher如何响应我们的url请求,即定义了controller的一些行为
*    7.在web.xml中dispatcher作为servlet注册进来响应前端请求,同时将spring打头的文件都注册进来
posted on 2019-09-19 21:48  singleSpace  阅读(352)  评论(0编辑  收藏  举报