如果你有幸看到这篇博客,那么请出门左转,因为博主很渣,这只是一篇笔记,没有大神独白
-------------------------------------------------------------华丽的分割线--------------------------------------------------------------
Imooc SSM
第一步项目依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.imooc.study</groupId> <artifactId>study.web</artifactId> <packaging>war</packaging> <version>1.0-BETA</version> <name>study.web Maven Webapp</name> <url>http://maven.apache.org</url> <!--常量 版本号--> <properties> <spring.version>4.3.7.RELEASE</spring.version> <junit.version>4.11</junit.version> <slf4j.version>1.7.6</slf4j.version> <logback.version>1.2.2</logback.version> <spring.version>4.3.7.RELEASE</spring.version> <servlet-api.version>4.0.0-b03</servlet-api.version> <mybatis.version>3.4.2</mybatis.version> <mysql.version>6.0.6</mysql.version> </properties> <!--依赖管理--> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-framework-bom</artifactId> <version>${spring.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <!--项目依赖--> <dependencies> <!-- ===================编码辅助 依赖================== --> <!--单元测试--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!--日志组合 slf4j + logback--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>${logback.version}</version> </dependency> <!--slf4j接口整合--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>${logback.version}</version> </dependency> <!-- ===================数据库层 依赖================== --> <!--数据库依赖 驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> <scope>runtime</scope> </dependency> <!--数据库连接池--> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <!-- ===================Mybatis 依赖================== --> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <!--mybatis spring 整合依赖--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.1</version> </dependency> <!-- ===================Servlet web 依赖================ --> <!--jsp标签--> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <!--jstl标签--> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--Jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.7</version> </dependency> <!--Servlet api--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> </dependency> <!-- ===================Spring 依赖================== --> <!--spring 核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> </dependency> <!--spring dao层依赖 jdbc 事务--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> </dependency> <!--spring web层依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> </dependency> <!--spring test依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> </dependency> </dependencies> </project>
第二步 mybits 配置文件
<?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的getGenerateKey获取数据库自增主键--> <setting name="useGenerateKeys" value="true"/> <!--使用列别名替换列名--> <setting name="useColumnLabel" value="true"/> <!--驼峰命名转换:create_time >>> createTime 下划线--> <setting name="mapUnderscoreCamelCase" value="true"/> </settings> <!--映射具体的mapper sql.xml--> <mappers> <mapper resource="mapper/SeckillDao.xml"></mapper> <mapper resource="mapper/SuccesskilledDao.xml"></mapper> </mappers> </configuration> 数据库连接 转移至jdbc.properties 键值对配置文件 driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/imooc?serverTimezone=UTC&characterEncoding=utf-8 username=root password=1234利用spring bean特性 把数据库连接封装在一个bean里 实现代码简化
<?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--> <!--数据库参数--> <context:property-placeholder location="classpath:jdbc.properties"/> <!--数据库连接池--> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!--配置连接池属性--> <property name="driverClass" value="${driver}"/> <property name="jdbcUrl" value="${url}"/> <property name="user" value="${username}"/> <property name="password" value="${password}"/> </bean> </beans>