模块化的分层
一级每层需要使用的包
在父文件需要引入的包
<?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>cn.jiedada</groupId> <artifactId>crm_parent</artifactId> <packaging>pom</packaging> <version>1.0-SNAPSHOT</version> <modules> <module>basic_util</module> <module>crm_common</module> <module>basic_core</module> <module>crm_mapper</module> <module>crm_service</module> <module>crm_web</module> </modules> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> <!-- spring版本号 --> <spring.version>4.1.2.RELEASE</spring.version> <!-- mybatis版本号 --> <mybatis.version>3.2.1</mybatis.version> <!-- log4j日志文件管理包版本 --> <slf4j.version>1.7.2</slf4j.version> <log4j.version>1.2.17</log4j.version> <!-- jackson包版本 --> <jackson.version>2.5.0</jackson.version> <!--swagger测试--> <springfox.version>2.4.0</springfox.version> </properties> <dependencies> <!--Spring核心--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-expression</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <!--aop依赖包--> <dependency> <groupId>aopalliance</groupId> <artifactId>aopalliance</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>1.6.8</version> </dependency> </dependencies> </project>
crm_parent
除了web需要使用web程序外,其他的都是普通的maven项目
在第一个文件中有需要指定版本号,这样方便以后的修改等等
basic_core
因为需要用到事务所以需要导入事务的包
<dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> </dependencies>
BasicDomain
package cn.jiedada.domain; public class BasicDomain { private Long id; public Long getId() { return id; } public void setId(Long id) { this.id = id; } }
BasicMapper
package cn.jiedada.mapper; import java.io.Serializable; import java.util.List; public interface BasicMapper<T> { void save(T t); void delete(Serializable id); void update(T t); List<T> findAll(); T findOne(Serializable id); }
BasicQuery
package cn.jiedada.query; public class BasicQuery { private Long page; public Long getPage() { return page; } public void setPage(Long page) { this.page = page; } }
BasicService
package cn.jiedada.service; import java.io.Serializable; import java.util.List; public interface IBasicService<T> { void save(T t); void delete(Serializable id); void update(T t); List<T> findAll(); T findOne(Serializable id); }
BasicService.impl
package cn.jiedada.service.impl; import cn.jiedada.mapper.BasicMapper; import cn.jiedada.service.IBasicService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional; import java.io.Serializable; import java.util.List; @Transactional(propagation = Propagation.SUPPORTS,readOnly = true) public class BasicServiceImpl<T> implements IBasicService<T> { @Autowired private BasicMapper<T> basicMapper; @Override @Transactional public void save(T t) { basicMapper.save(t); } @Override @Transactional public void delete(Serializable id) { basicMapper.delete(id); } @Override @Transactional public void update(T t) { basicMapper.update(t); } @Override public List<T> findAll() { return basicMapper.findAll(); } @Override public T findOne(Serializable id) { return basicMapper.findOne(id); } }
baisc_util
需要一个分页,及AjaxResoult
AjaxResoult
package cn.jiedada; public class AjaxResoult { private Boolean success=true; private String msg="消息正确"; public Boolean getSuccess() { return success; } public AjaxResoult setSuccess(Boolean success) { this.success = success; return this; } public String getMsg() { return msg; } public AjaxResoult setMsg(String msg) { this.msg = msg; return this; } public static AjaxResoult getAjaxResoult(){ return new AjaxResoult(); } }
PageList
package cn.jiedada; import java.util.List; public class PageList<T> { private Long total; private List<T> rows; public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } public List<T> getRows() { return rows; } public void setRows(List<T> rows) { this.rows = rows; } }
crm_common
因为这里需要继承BasicDOmain,和BasicQuery
所以我们需要引入上面的文件
在pom.xml中
<dependencies> <dependency> <groupId>cn.jiedada</groupId> <artifactId>basic_core</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
crm_mapper
所以我们需要引入上面的文件
在pom.xml中
<dependencies> <dependency> <groupId>cn.jiedada</groupId> <artifactId>basic_common</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> </dependency> <!--Mybatis集成--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>${mybatis.version}</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.2.0</version> </dependency> <!--log4j--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <!--slf4j--> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> <scope>test</scope> </dependency> <!--数据库连接驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> </dependency> <!--数据库连接池--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>1.2.2</version> </dependency> <dependency> <groupId>commons-pool</groupId> <artifactId>commons-pool</artifactId> <version>1.5.3</version> </dependency> <dependency> <groupId>org.apache.maven.surefire</groupId> <artifactId>surefire-junit4</artifactId> <version>2.12.4</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies>
这里我们需要引入很多文件,DeptMapper需要继承就可以了
DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!--namespace是我们自己取的名字--> <mapper namespace="cn.jiedada.crm.mapper.DeptMapper"> <select id="findAll" resultType="dept"> select * from dept </select> </mapper>
appliactionContext-mybatis.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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <!--添加jdbc.properties文件--> <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder> <!--创建连接池--> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!--创建sqlSessionFactory--> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"></property> <!--读取*Mapper.xml--> <property name="mapperLocations" value="classpath:cn/jiedada/crm/mapper/*Mapper.xml" /> <!--使用别名--> <property name="typeAliasesPackage"> <value> cn.jiedada.crm.domain cn.jiedada.crm.query </value> </property> </bean> <!--动态代理自动生成实现类--> <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="cn.jiedada.crm.mapper"></property> </bean> </beans>
jdbc.properties
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/db1011 jdbc.username=root jdbc.password=123456
log4j.properties
log4j.rootLogger=ERROR, stdout #log4j.rootLogger=NONE log4j.logger.cn.jiedada=TRACE log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
crm_service
所以我们需要引入上面的文件
在pom.xml中
<dependencies> <dependency> <groupId>cn.jiedada</groupId> <artifactId>crm_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>cn.jiedada</groupId> <artifactId>basic_util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
appliactionContext-service.xml中需要引入我们appliactionContext-mybatis.xml的东西
所以
<import resource="classpath:applicationContext-mybatis.xml"></import>
<?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:tx="http://www.springframework.org/schema/tx" 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/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <import resource="classpath:applicationContext-mybatis.xml"></import> <!--扫描service层--> <context:component-scan base-package="cn.jiedada.crm.service"></context:component-scan> <!--事务配置--> <!--配置事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 --> <tx:annotation-driven transaction-manager="transactionManager" /> </beans>
crm_web
所以我们需要引入上面的文件
在pom.xml中
<dependencies> <dependency> <groupId>cn.jiedada</groupId> <artifactId>crm_service</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.2</version> </dependency> <!--jackson--> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>${jackson.version}</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>${jackson.version}</version> </dependency> <!-- JSTL标签类 --> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${springfox.version}</version> </dependency> <dependency> <groupId>cn.jiedada</groupId> <artifactId>basic_util</artifactId> <version>1.0-SNAPSHOT</version> </dependency> </dependencies>
appliactionContext-mvc.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: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 http://www.springframework.org/schema/mvc "> <!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器 --> <context:component-scan base-package="cn.jiedada.crm.web.controller"/> <context:component-scan base-package="cn.jiedada.crm.web.config"/> <!-- 启动SpringMVC的注解功能 --> <mvc:annotation-driven/> <!--静态资源放行--> <mvc:default-servlet-handler/> <!-- 定义跳转的文件的前后缀 ,视图解析器配置--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <!-- 配置文件上传解析器 --> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <!-- 默认编码 --> <property name="defaultEncoding" value="utf-8"/> <!-- 文件大小最大值 --> <property name="maxUploadSize" value="10485760000"/> </bean> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!--方案1:独立容器方案,spring和springmvc都有自己context--> <!--1.1 spring容器初始化--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-service.xml</param-value> </context-param> <!--1.2 配置springmvc--> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext-mvc.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <!--/*会拦截所有包括jsp--> <!--/会拦截所有但是不包含jsp--> <url-pattern>/</url-pattern> </servlet-mapping> <!-- post请求乱码解决 get请求实在tomcat猫中配置 --> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>