欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

[框架整合]spring+springmvc+mybatis(1)

程序员文章站 2022-05-23 19:51:36
...

引用于慕课网的一系列课程 Spring+SpringMVC+MyBatis整合参考视频
很优美很规范的整合版本,视频中有很多思想在里面!小伙伴点进去学习吧~

想要源代码的朋友点击这里下载哦~~

写这篇博客是为了加深学习的印象,在整合的过程中遇到什么问题,欢迎留言。

整合顺序

  1. 搭建项目,导入jar包。(这里我使用maven搭建,也可以自己导入)
  2. 先整合Mybatis和Spring
    (关键配置文件有mybatis-config.xml, spring-dao.xml, spring-service.xml
  3. 再整合SpringMVC
    (关键配置文件有 spring-web.xml, web.xml)
  4. 最后集成测试

那么来吧!


博主maven环境已经搭建好了。
还没搭建好的小伙伴可以点击此处学习


cmd执行命令创建项目seckill

mvn archetype:generate -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=maven-archetype-webapp -DarchetypeCatalog=local

编写jar包依赖 pom.xml

<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/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>org.seckill</groupId> 
  <artifactId>seckill</artifactId>
  <packaging>war</packaging>
  <version>1</version>
  <name>seckill Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>

  <!-- 使用junit4,因为4可以使用注解的方式单元测试 -->
   <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
   <!-- 补全依赖 -->
   <!-- 1:日志; java日志:slf4j,log4j,logback,common-logging slf4j接口/规范 log4j,logback,common-logging,实现 此处使用slf4j+logback -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.6</version>
    </dependency>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-core</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- 实现slf4j日志 -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- 2:数据库相关依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.37</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <!-- Dao框架- MyBatis依赖 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.3.0</version>
    </dependency>
    <!-- mybatis自身实现的spring整合依赖 -->
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.3</version>
    </dependency>

    <!--3: Servlet web 相关依赖 -->
    <dependency>
        <groupId>taglibs</groupId>
        <artifactId>standard</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.4</version>
    </dependency>

    <!-- 4: spring 依赖 -->
    <!-- 1)spring核心依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- 2)spring dao层依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- 3)spring web依赖 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- 4)spring test依赖 -->
    <dependency>
        <groupId>org.springframework</groupId> 
        <artifactId>spring-test</artifactId>
        <version>4.1.7.RELEASE</version>
    </dependency>
    <!-- redis 客户端 Jedis-->
    <dependency>
        <groupId>redis.clients</groupId> 
        <artifactId>jedis</artifactId>
        <version>2.7.2</version>
    </dependency> 
    <!-- 高效率 的序列化jar [protostuff] -->
    <dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-core</artifactId>
        <version>1.0.8</version>
    </dependency>
    <dependency>
        <groupId>com.dyuproject.protostuff</groupId>
        <artifactId>protostuff-runtime</artifactId>
        <version>1.0.8</version>
    </dependency>
    <!-- Map 工具类 -->
    <dependency>
        <groupId>commons-collections</groupId>
        <artifactId>commons-collections</artifactId>
        <version>3.2</version>
    </dependency>
  </dependencies>
  <build>
    <finalName>seckill</finalName>
  </build>
</project>

jar包导入完毕后,创建目录,如下
[框架整合]spring+springmvc+mybatis(1)

在下一篇中,将进行整合第二步,整合mybatis+spring。

转载于:https://my.oschina.net/antgan/blog/697216