1.新建maven webapp工程,配置pom文件
1.1添加tomcat插件
< plugins > < plugin > < groupId > org.apache.tomcat.maven</ groupId > < artifactId > tomcat7-maven-plugin</ artifactId > < version > 2.2</ version > < configuration > < server > tomcat7</ server > < update > true</ update > < port > 8080</ port > < path > /</ path > </ configuration > </ plugin > </ plugins >
1.2配置文件扫描,将路径中的xml文件,properities文件,ini文件添加到classpath中
< resources >
< resource >
< directory > src/main/java</ directory >
< includes >
< include > **/*.xml</ include >
</ includes >
< filtering > true</ filtering >
</ resource >
< resource >
< directory > src/main/resources</ directory >
< includes >
< include > **/*.properties</ include >
< include > **/*.xml</ include >
< include > **/*.ini</ include >
</ includes >
< filtering > false</ filtering >
</ resource >
</ resources >
2.pom文件添加各个框架所需要的依赖jar包
2.1添加spring
< dependency >
< groupId > org.springframework</ groupId >
< artifactId > spring-context</ artifactId >
< version > 5.0.8.RELEASE</ version >
</ dependency >
2.2添加spring_webmvc(springmvc)
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> < dependency > < groupId > org.springframework</ groupId > < artifactId > spring-webmvc</ artifactId > < version > 5.0.8.RELEASE</ version > </ dependency >
2.3添加mybatis
< dependency > < groupId > org.mybatis</ groupId > < artifactId > mybatis</ artifactId > < version > 3.4.6</ version > </ dependency >
2.4添加mysql
< dependency > < groupId > mysql</ groupId > < artifactId > mysql-connector-java</ artifactId > < version > 5.1.34</ version > </ dependency >
2.5添加spring_mybatis
< dependency > < groupId > org.mybatis</ groupId > < artifactId > mybatis-spring</ artifactId > < version > 1.3.2</ version > </ dependency >
2.6添加servlet
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api --> < dependency > < groupId > javax.servlet</ groupId > < artifactId > javax.servlet-api</ artifactId > < version > 4.0.1</ version > < scope > provided</ scope > </ dependency >
2.7添加数据库连接池,使用阿里的druid
< dependency > < groupId > com.alibaba</ groupId > < artifactId > druid</ artifactId > < version > 1.1.10</ version > </ dependency >
2.8添加json,使用谷歌的jackson
< dependency > < groupId > com.fasterxml.jackson.core</ groupId > < artifactId > jackson-databind</ artifactId > < version > 2.9.6</ version > </ dependency >
2.9添加aspectj
< dependency > < groupId > org.aspectj</ groupId > < artifactId > aspectjweaver</ artifactId > < version > 1.9.1</ version > </ dependency >
2.10添加spring_jdbc,用来读取properities,连接数据库
< dependency > < groupId > org.springframework</ groupId > < artifactId > spring-jdbc</ artifactId > < version > 5.0.8.RELEASE</ version > </ dependency >
3.创建工程目录结构
3.1在java文件夹下创建dao、service、controller、pojo、utils文件夹
3.2在resources文件夹下创建spring、mybatis、resource文件夹
4.创建各种配置文件,资源文件
4.1配置mybatis_config.xml
在mybatis文件夹下新建文件mybatis_config.xml,添加头:
<? 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" >
工程使用spring托管mybatis,所以mybatis的配置文件只需要配置一下插件,数据库的连接放在spring的配置文件中
4.2配置properities文件
在resource文件夹下新建db.properities文件,用来储存相关变量
mydriver = com.mysql.jdbc.Driver myurl = jdbc:mysql://localhost:3306/world myusername = 数据库用户名 mypassword = 数据库密码 maxCreateTaskCount = 100 minIdle = 5
4.3配置spring的xml文件
4.3.1配置dao层的spring文件
<? 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" >
<!-- 引入配置文件,扫描注解-->
< context :property-placeholder location= "classpath:resource/db.properties" /> < context :component-scan base-package= "usts.dao" />
<!-- 配置数据库连接池-->
< bean id= "dataSource" class= "com.alibaba.druid.pool.DruidDataSource" > < property name= "driver" value= "${mydriver}" /> < property name= "url" value= "${myurl}" /> < property name= "username" value= "${myusername}" /> < property name= "password" value= "${mypassword}" /> < property name= "maxCreateTaskCount" value= "${maxCreateTaskCount}" /> < property name= "minIdle" value= "${minIdle}" /> </ bean >
<!-- 配置sqlSessionFactory-->
< bean id= "sqlSessionFactoryBean" class= "org.mybatis.spring.SqlSessionFactoryBean" > < property name= "dataSource" ref= "dataSource" /> < property name= "configLocation" value= "classpath:mybatis/mybatis_config.xml" /> < property name= "mapperLocations" value= "classpath:usts/dao/*Mapper.xml" /> </ bean >
<!--spring 托管mybatis-->
< bean id= "mapperScannerConfigurer" class= "org.mybatis.spring.mapper.MapperScannerConfigurer" > < property name= "basePackage" value= "usts.dao" /> </ bean > </ beans >
4.3.2配置service的spring文件
<? 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" >
<!-- 添加注解扫描-->
< context :component-scan base-package= "usts.service" /> </ beans >
4.3.3配置事务的spring文件
<? 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: tx = "http://www.springframework.org/schema/tx"
xmlns: aop = "http://www.springframework.org/schema/aop"
xsi :schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" >
<!-- 配置事务管理器-->
< bean id= "dataSourceTransactionManager" class= "org.springframework.jdbc.datasource.DataSourceTransactionManager" > < property name= "dataSource" ref= "dataSource" /> </ bean >
<!-- 事务的详细配置-->
<!--通知的详细配置-->
< tx :advice id= "adv" > < tx :attributes > < tx :method name= "*" propagation= "REQUIRED" /> </ tx :attributes > </ tx :advice >
<!-- 配置aop,通知,切入点-->
< aop :config > < aop :pointcut id= "point" expression= "execution(* usts.service.*.*(..))" /> < aop :advisor advice-ref= "adv" pointcut-ref= "point" /> </ aop :config > </ beans >
4.3.4配置springmvc的配置文件
<? 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"
xmlns: mvc = "http://www.springframework.org/schema/mvc"
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 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd" > < context :component-scan base-package= "usts.controller" /> < mvc :annotation-driven /> < mvc :default-servlet-handler /> < bean id= "resourceViewResolver" class= "org.springframework.web.servlet.view.InternalResourceViewResolver" > < property name= "suffix" value= ".html" /> < property name= "prefix" value= "/" /> </ bean > < bean id= "multipartResolver" class= "org.springframework.web.multipart.commons.CommonsMultipartResolver" > < property name= "defaultEncoding" value= "utf-8" ></ property > < property name= "maxUploadSize" value= "100000" ></ property > </ bean > </ beans >
5.web.xml的配置
5.1添加springmvc的servlet
< servlet > < servlet-name > dispatcherServlet</ servlet-name > < servlet-class > org.springframework.web.servlet.DispatcherServlet</ servlet-class > < init-param > < param-name > contextConfigLocation</ param-name > < param-value > classpath:spring/springmvc_config.xml</ param-value > </ init-param > </ servlet > < servlet-mapping > < servlet-name > dispatcherServlet</ servlet-name > < url-pattern > /</ url-pattern > </ servlet-mapping >
5.2配置启动项
< web-app > < display-name > Archetype Created Web Application</ display-name > < context-param > < param-name > contextConfigLocation</ param-name > < param-value > classpath:spring/spring_config_*.xml
</ param-value > </ context-param > < listener > < listener-class > org.springframework.web.context.ContextLoaderListener</ listener-class > </ listener >