声明:教程是网上找的,代码是自己敲的
项目目录大致如下:
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>