声明:教程是网上找的,代码是自己敲的

项目目录大致如下:

1. 首先创建Maven工程,在pom.xml中加入项目所需依赖:

<?xml version="1.0" encoding="UTF-8"?>
<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>com.fitsoft</groupId>
<artifactId>shopmgr</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <!-- 配置的全局的属性 -->
<properties>
<junit.version>4.11</junit.version>
<jsp.api.version>8.5.5</jsp.api.version>
<jstl.version>1.2</jstl.version>
<spring.version>4.1.6.RELEASE</spring.version>
<aspectjweaver.version>1.8.5</aspectjweaver.version>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.spring.version>1.2.2</mybatis.spring.version>
<c3p0.version>0.9.5</c3p0.version>
<mysql.version>8.0.11</mysql.version>
<log4j.version>1.2.17</log4j.version>
<json.version>2.4</json.version>
</properties> <!-- 配置项目需要哪些jar包 -->
<dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<!-- 该jar包保留到测试 -->
<scope>test</scope>
</dependency>
<!-- jsp-api、servlet-api、el -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
<version>${jsp.api.version}</version>
<!-- 该jar包最终由Web容器提供 -->
<scope>provided</scope>
</dependency>
<!-- jstl -->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- Spring4 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectjweaver.version}</version>
</dependency>
<!-- mybatis3 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis.spring.version}</version>
</dependency>
<!-- c3p0 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>${c3p0.version}</version>
</dependency>
<!-- mysql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<!-- log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<!-- json-lib -->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>${json.version}</version>
<!-- 指定jar包jdk版本的依赖 -->
<classifier>jdk15</classifier>
</dependency>
</dependencies>
</project>

2. 由于项目使用c3p0连接池,因此需要在resource目录下配置c3p0.properties,主要配置数据库驱动、地址、用户名、密码等属性。文件内容如下:

#连接数据库的地址
#配置数据库的驱动
c3p0.driverClass=com.mysql.cj.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost:3306/imoocdb?&characterEncoding=utf8&useSSL=false&serverTimezone=GMT&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
c3p0.user=root
c3p0.password=
#c3p0.maxPoolSize=20
#c3p0.minPoolSize=2
#c3p0.initialPoolSize=2
#c3p0.maxIdleTime=60

3. 项目启动首先要进入web.xml文件,因此Spring的监听器就放在这里配置

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
version="4.0"> <!-- 配置Spring的核心监听器 -->
<!-- 提取Spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/spring/application*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener> </web-app>

4. 监听器用来加载application.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:p="http://www.springframework.org/schema/p"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
"> <!-- 配置一个扫描器 -->
<context:component-scan base-package="com.fitsoft.shop"/> <!-- 配置数据源的配置 C3P0作为数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"/> <!-- 配置连接工厂sqlSessionFactory
Spring + Mybatis
-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" p:dataSource-ref="dataSource">
<!-- 扫描类型,为类型配置别名,默认配置的别名就是类名
以后映射文件中无需写类型的全限名
-->
<property name="typeAliasesPackage" value="com.fitsoft.shop.bean"/>
</bean> <!-- 配置扫描数据访问层接口的路径,为数据访问层接口做实现类对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer" p:sqlSessionFactoryBeanName="sqlSessionFactory">
<!-- 配置数据访问接口:采用包扫描的方式到基础包下扫描所有的类,作为MyBatis2的数据访问接口,
并创建这些类的代理对象,创建出来后会把这些代理对象交给Spring容器管理,bean的id名默认为接口的类名前面首字母小写
多个包之间用;分隔 -->
<property name="basePackage" value="com.fitsoft.shop.repository"/>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <!-- 申明式事务,使用事务注解的方式去控制业务层的事务 开启annotation注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

Spring+Mybatis整合的练手小项目(一)项目部署的更多相关文章

  1. Springmvc+Spring+Mybatis整合开发(架构搭建)

    Springmvc+Spring+Mybatis整合开发(架构搭建) 0.项目结构 Springmvc:web层 Spring:对象的容器 Mybatis:数据库持久化操作 1.导入所有需要的jar包 ...

  2. Spring + mybatis整合方案总结 结合实例应用

    Spring + mybatis整合实例应用 项目结构图 (Spring3.0.2 +mybatis3.0.4) 方案一: 通过配置文件整合Spring和mybatis 应用数据库 -- --数据库 ...

  3. SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发。

    SSM 即所谓的 Spring MVC + Spring + MyBatis 整合开发.是目前企业开发比较流行的架构.代替了之前的SSH(Struts + Spring + Hibernate) 计划 ...

  4. Spring+Mybatis整合时 Failed to read candidate component class,Caused by:IllegalArgumentException

    Spring+Mybatis整合时Caused by: java.lang.IllegalArgumentException错误 org.springframework.beans.factory.B ...

  5. 3.springMVC+spring+Mybatis整合Demo(单表的增删该查,这里主要是贴代码,不多解释了)

    前面给大家讲了整合的思路和整合的过程,在这里就不在提了,直接把springMVC+spring+Mybatis整合的实例代码(单表的增删改查)贴给大家: 首先是目录结构: 仔细看看这个目录结构:我不详 ...

  6. SpringMVC+Spring+Mybatis整合

    SpringMVC+Spring+Mybatis整合 导包 配置jdbc.properties.log4j.properties jdbc.driver=com.mysql.jdbc.Driver j ...

  7. 【Python】【辅助程序】练手小程序:记录外网动态IP地址

    练手小程序 程序作用:对IP实时记录: 1.定时获取外网IP,存储在本地文件中: 编写思路: 1)收集获取外网的API接口       http://bbs.125.la/thread-1383897 ...

  8. 【Python精华】100个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. [程序1] 题目:有1.2.3.4个数字,能组成多少个互不相同 ...

  9. 整理了适合新手的20个Python练手小程序

    100个Python练手小程序,学习python的很好的资料,覆盖了python中的每一部分,可以边学习边练习,更容易掌握python. 本文附带基础视频教程:私信回复[基础]就可以获取的 [程序1] ...

随机推荐

  1. centos7.x 安装系统/配置网络/设置主机名

    1.安装系统     系统的安装就不多说了,自行查找百度,如:https://www.cnblogs.com/wcwen1990/p/7630545.html   2.配置网络(局域网上网) 修改配置 ...

  2. SQL Server避免漏加where条件导致的批量误操作

    很多开发人员,包括数据库管理员都有马失前蹄的时候,update/delete时忘记了添加where条件,导致不必要的麻烦.一旦失误,必须要尝试各种恢复手段来恢复数据,尤其是正在使用的生产数据库,造成的 ...

  3. 开发者工具conloseLog的使用

  4. mybatis多表查询之多对多关系查询的实现-xml方式

    Mybatis对于多对多关系下的查询提供了集合(collection)的概念来解决,collection属性是resultMap高级结果映射的子集,首先,在本例中我们使用的是集合元素来解决多对多的查询 ...

  5. 《Java 8 in Action》Chapter 5:使用流

    流让你从外部迭代转向内部迭代,for循环显示迭代不用再写了,流内部管理对集合数据的迭代.这种处理数据的方式很有用,因为你让Stream API管理如何处理数据.这样Stream API就可以在背后进行 ...

  6. ssh-key生成密钥及SSH无密码登录的配置

    文章作者:foochane  原文链接:https://foochane.cn/article/2019061601.html 1 ssh-keygen命令 ssh-keygen命令说明: -t :指 ...

  7. Linux下串口配置初步探寻

    一.在struct termios结构体中,对串口进行基本配置(如波特率设置,校验位和停止位设置 等). (一): struct termios   //串口的设置主要是设置struct termio ...

  8. String类中intern方法的原理分析

    一,前言 ​ 昨天简单整理了JVM内存分配和String类常用方法,遇到了String中的intern()方法.本来想一并总结起来,但是intern方法还涉及到JDK版本的问题,内容也相对较多,所以今 ...

  9. HashMap原理。图文并茂式解读。这些注意点你一定还不了解

    目录 概述 属性详解 table entrySet size modCount threshold.loadFactor 源码知识点必备 getGenericInterfaces和getInterfa ...

  10. 选择排序、快速排序、归并排序、堆排序、快速排序实现及Sort()函数使用

    1.问题来源 在刷题是遇到字符串相关问题中使用 strcmp()函数. 在函数比较过程中有使用 排序函数 Sort(beg,end,comp),其中comp这一项理解不是很彻底. #include & ...