声明:教程是网上找的,代码是自己敲的
项目目录大致如下:
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.0modelVersion> <groupId>com.fitsoftgroupId> <artifactId>shopmgrartifactId> <version>1.0-SNAPSHOTversion> <packaging>warpackaging> <properties> <junit.version>4.11junit.version> <jsp.api.version>8.5.5jsp.api.version> <jstl.version>1.2jstl.version> <spring.version>4.1.6.RELEASEspring.version> <aspectjweaver.version>1.8.5aspectjweaver.version> <mybatis.version>3.2.8mybatis.version> <mybatis.spring.version>1.2.2mybatis.spring.version> <c3p0.version>0.9.5c3p0.version> <mysql.version>8.0.11mysql.version> <log4j.version>1.2.17log4j.version> <json.version>2.4json.version> properties> <dependencies> <dependency> <groupId>junitgroupId> <artifactId>junitartifactId> <version>${junit.version}version> <scope>testscope> dependency> <dependency> <groupId>org.apache.tomcatgroupId> <artifactId>tomcat-jsp-apiartifactId> <version>${jsp.api.version}version> <scope>providedscope> dependency> <dependency> <groupId>jstlgroupId> <artifactId>jstlartifactId> <version>${jstl.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-webartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.springframeworkgroupId> <artifactId>spring-ormartifactId> <version>${spring.version}version> dependency> <dependency> <groupId>org.aspectjgroupId> <artifactId>aspectjweaverartifactId> <version>${aspectjweaver.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatisartifactId> <version>${mybatis.version}version> dependency> <dependency> <groupId>org.mybatisgroupId> <artifactId>mybatis-springartifactId> <version>${mybatis.spring.version}version> dependency> <dependency> <groupId>com.mchangegroupId> <artifactId>c3p0artifactId> <version>${c3p0.version}version> dependency> <dependency> <groupId>mysqlgroupId> <artifactId>mysql-connector-javaartifactId> <version>${mysql.version}version> dependency> <dependency> <groupId>log4jgroupId> <artifactId>log4jartifactId> <version>${log4j.version}version> dependency> <dependency> <groupId>net.sf.json-libgroupId> <artifactId>json-libartifactId> <version>${json.version}version> <classifier>jdk15classifier> 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"> <context-param> <param-name>contextConfigLocationparam-name> <param-value>classpath:/spring/application*.xmlparam-value> context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-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"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"/> <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"> <property name="basePackage" value="com.fitsoft.shop.repository"/> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" p:dataSource-ref="dataSource"/> <tx:annotation-driven transaction-manager="transactionManager"/> beans>