校园商铺-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打头的文件都注册进来
校园商铺-2项目设计和框架搭建-6逐层完成SSM的各项配置的更多相关文章
- 校园商铺-2项目设计和框架搭建-8升级mysql驱动相关的配置以支持mysql8
1.如何升级驱动 1.1步骤: a 确保当前程序能正常访问数据库 b 更新mysql驱动重新运行程序进行校验 maven依赖https://mvnrepository.com/artifact/mys ...
- 校园商铺-2项目设计和框架搭建-5配置maven
/src/main/java 存放业务的Java代码 /src/main/resources 存储项目所用到的资源文件,如各种Spring,batis,日志的配置文件 /src/test/java 单 ...
- 校园商铺-2项目设计和框架搭建-10验证controller
1.新建package:com.csj2018.o2o.web.superadmin 2.建立AreaController.java package com.csj2018.o2o.web.super ...
- 校园商铺-2项目设计和框架搭建-9验证Service
1. 新建接口 main: com.csj2018.o2o.service/AreaService.java package com.csj2018.o2o.service; import java. ...
- 校园商铺-2项目设计和框架搭建-7验证Dao
以最简单的地区表为例 1.插入数据 insert into tb_area (area_name, priority) values('东苑', 1),('南苑', 7),('北苑', 5); sel ...
- webapi框架搭建-安全机制(四)-可配置的基于角色的权限控制
webapi框架搭建系列博客 在上一篇的webapi框架搭建-安全机制(三)-简单的基于角色的权限控制,某个角色拥有哪些接口的权限是用硬编码的方式写在接口上的,如RBAuthorize(Roles = ...
- [SSM项目]二-项目设计和框架搭建
一 10个实体类 选择Integer 而不是int的原因 :当值为空时,int类型会自动为其初始化,这是我们不希望的. 二 配置Maven 目录结构: src/main/java:业务代码 src/m ...
- SSM到Spring Boot-从零开发校园商铺平台
第1章 开发准备 本章包含课程介绍,同时讲解开发网站所需要准备的事情,并且带领大家从零开始搭建一个Maven Web. 1-1 课程导学 1-2 开发准备 第2章 项目设计和框架搭建 本章主要先带领大 ...
- webapi框架搭建系列博客
webapi框架搭建系列博客 webapi框架搭建-创建项目(一) webapi框架搭建-创建项目(二)-以iis为部署环境的配置 webapi框架搭建-创建项目(三)-webapi owin web ...
随机推荐
- 2019ICPC南昌网络赛C Hello 2019
题意:给出一个字符串,每次询问一个区间[l,r],求使得这个区间含有9102但不含有8102最少要删掉几个字符 首先我们考虑将串反转,这样就变成了含有2019但不含有2018的问题了 我们构建一个状态 ...
- OAuth2.0实例说明
OAuth2.0 详细实列+Word文档清晰说明 实例下载地址:https://files.cnblogs.com/files/liyanbofly/OAuth2.0%E5%AE%9E%E4%BE%8 ...
- hdu5421 Victor and String 回文树(前后插入)
题目传送门 题意:对一个字符串支持四种操作,前插入字符,后插入字符,询问本质不同的回文串数量和所有回文串的数量. 思路: 就是在普通回文树的基础上,维护suf(最长回文后缀)的同时再维护一个pre(最 ...
- MYSQL分数排名
编写一个 SQL 查询来实现分数排名.如果两个分数相同,则两个分数排名(Rank)相同.请注意,平分后的下一个名次应该是下一个连续的整数值.换句话说,名次之间不应该有“间隔”. +----+----- ...
- leetcood学习笔记-104-二叉树的最大深度
题目描述: 第一次提交: class Solution(object): def maxDepth(self, root): """ :type root: TreeNo ...
- RDBMS关系型数据库与HBase的对比
关系型数据库 结构: * 数据库以表的形式存在 * 支持FAT.NTFS.EXT.文件系统 * 使用Commit log存储日志 * 参考系统是坐标系统 * 使用主键(PK) * 支持分区 * 使用行 ...
- Greenplum(PostgreSql)函数实现批量删除表
项目做库迁移,前期需要经常调整表结构语句,涉及多次的批量drop,本着偷懒精神写了这个函数.鉴于本函数在生产环境有巨大风险,建议测试完毕后立即删除. 主要步骤很简单:1)从pg_tables查询得到相 ...
- Java-Class-@I:org.springframework.web.bind.annotation.RestController
ylbtech-Java-Class-@I:org.springframework.web.bind.annotation.RestController 1.返回顶部 2.返回顶部 1. pack ...
- Centos6.5安装mysql5.7.19
一.安装前准备 安装采用二进制包方式,软件包5.7.19版本下载地址:https://dev.mysql.com/downloads/mysql/ 选择MYSQL Community Server版本 ...
- 常用的一些 linux 指令
1. mv linux下重命名文件或文件夹使用mv既可实现. 1.1 重命名 a.将一个名为abc.txt的文件重命名为1234.txt #mv abc.txt .txt b. 将目录A重命名为B ( ...