IDEA+Maven搭建模块化项目

一.SSM集成

1.1 创建项目

IDEA+Maven搭建模块化项目_第1张图片

1.2 pom.xml



<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>cn.itsource.crmgroupId>
    <artifactId>crmartifactId>
    <version>1.0-SNAPSHOTversion>
    <packaging>warpackaging>

    <name>crm Maven Webappname>
    
    <url>http://www.example.comurl>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <maven.compiler.source>1.8maven.compiler.source>
        <maven.compiler.target>1.8maven.compiler.target>
        <org.springframework.version>4.2.5.RELEASEorg.springframework.version>
        <org.hibernate.version>4.3.8.Finalorg.hibernate.version>
        <spring-data-jpa.version>1.9.0.RELEASEspring-data-jpa.version>
        <com.fasterxml.jackson.version>2.5.0com.fasterxml.jackson.version>
    properties>
    <dependencies>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-coreartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-contextartifactId>
            <version>4.2.5.RELEASEversion>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-txartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-jdbcartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-ormartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-aopartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-testartifactId>
            <version>${org.springframework.version}version>
            <scope>testscope>
        dependency>
        
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webartifactId>
            <version>${org.springframework.version}version>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-webmvcartifactId>
            <version>${org.springframework.version}version>
        dependency>
        
        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-ioartifactId>
            <version>1.3.2version>
        dependency>
        
        <dependency>
            <groupId>commons-fileuploadgroupId>
            <artifactId>commons-fileuploadartifactId>
            <version>1.2.2version>
        dependency>
        
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-coreartifactId>
            <version>${com.fasterxml.jackson.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-annotationsartifactId>
            <version>${com.fasterxml.jackson.version}version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>${com.fasterxml.jackson.version}version>
        dependency>

        <dependency>
            <groupId>commons-dbcpgroupId>
            <artifactId>commons-dbcpartifactId>
            <version>1.2.2version>
        dependency>

        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.6version>
        dependency>

        <dependency>
            <groupId>org.apache.commonsgroupId>
            <artifactId>commons-lang3artifactId>
            <version>3.5version>
        dependency>
        
        <dependency>
            <groupId>junitgroupId>
            <artifactId>junitartifactId>
            <version>4.12version>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>javax.servletgroupId>
            <artifactId>javax.servlet-apiartifactId>
            <version>3.1.0version>
            
            <scope>providedscope>
        dependency>
        <dependency>
            <groupId>commons-logginggroupId>
            <artifactId>commons-loggingartifactId>
            <version>1.2version>
        dependency>

        
        
        
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatisartifactId>
            <version>3.2.1version>
        dependency>
        
        <dependency>
            <groupId>org.mybatisgroupId>
            <artifactId>mybatis-springartifactId>
            <version>1.2.0version>
        dependency>
        
        
        <dependency>
            <groupId>log4jgroupId>
            <artifactId>log4jartifactId>
            <version>1.2.17version>
        dependency>
        
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-apiartifactId>
            <version>1.7.2version>
        dependency>
        <dependency>
            <groupId>org.slf4jgroupId>
            <artifactId>slf4j-log4j12artifactId>
            <version>1.7.2version>
        dependency>

        
        <dependency>
            <groupId>org.mybatis.generatorgroupId>
            <artifactId>mybatis-generator-coreartifactId>
            <version>1.3.5version>
        dependency>

        
        <dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>5.0.1version>
        dependency>

    dependencies>

    <build>
        <finalName>crmfinalName>
            <plugins>
                <plugin>
                    <groupId>org.mybatis.generatorgroupId>
                    <artifactId>mybatis-generator-maven-pluginartifactId>
                    <version>1.3.2version>
                    <configuration>
                        <verbose>falseverbose>
                        <overwrite>trueoverwrite>
                    configuration>
                plugin>
                <plugin>
                    <artifactId>maven-clean-pluginartifactId>
                    <version>3.1.0version>
                plugin>
                
                <plugin>
                    <artifactId>maven-resources-pluginartifactId>
                    <version>3.0.2version>
                plugin>
                <plugin>
                    <artifactId>maven-compiler-pluginartifactId>
                    <version>3.8.0version>
                plugin>
                <plugin>
                    <artifactId>maven-surefire-pluginartifactId>
                    <version>2.22.1version>
                plugin>
                <plugin>
                    <artifactId>maven-war-pluginartifactId>
                    <version>3.2.2version>
                plugin>
                <plugin>
                    <artifactId>maven-install-pluginartifactId>
                    <version>2.5.2version>
                plugin>
                <plugin>
                    <artifactId>maven-deploy-pluginartifactId>
                    <version>2.8.2version>
                plugin>
            plugins>
    build>
project>

1.3 项目基本结构图

IDEA+Maven搭建模块化项目_第2张图片
注意:修改配置文件(根据自己项目的情况)

1.4 mapper层:

xml中的SQL是代码生成器生成的(自己写也可以)!

BaseMapper

public interface BaseMapper<T> {

    int deleteByPrimaryKey(Long id);

    int insert(T record);

    T selectByPrimaryKey(Long id);

    List<T> selectAll();

    int updateByPrimaryKey(T record);
}

DepartmentMapper

public interface DepartmentMapper extends BaseMapper<Department> {

}

1.5 Service层

IBaseService

public interface IBaseService<T> {
    void save(T t);
    void update(T t);
    void delete(Long id);
    T findOne(Long id);
    List<T> findAll();
}

BaseServiceImpl

package cn.itsource.crm.service.impl;

import cn.itsource.crm.mapper.BaseMapper;
import cn.itsource.crm.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    @Transactional
    public void save(T t) {
        mapper.insert(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.updateByPrimaryKey(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public T findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public List<T> findAll() {
        return mapper.selectAll();
    }
}

IDepartmentService

public interface IDepartmentService extends IBaseService<Department> {
}

DepartmentServiceImpl

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department>
        implements IDepartmentService {
}

1.6 完成Controller

  • 准备web.xml
  • 基本展示出来即可!!

二 集成easyui

2.1 加入最小级别的easyui

IDEA+Maven搭建模块化项目_第3张图片

2.2 上下文路径

配置了上下文路径

IDEA+Maven搭建模块化项目_第4张图片

解决上下文路径的方案

  • 方案一: ${pageContext.request.contextPath}
  • 方案二: base标签
<%
    String path = request.getContextPath();
    //   http://localhost:80/crm/
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%--所有访问前都会访问这个--%>
<base href="<%=basePath %>">

2.3 easyui的CRUD

2.4 准备了一个AjaxResult

IDEA+Maven搭建模块化项目_第5张图片

三 分页

3.1 引入mybatis的分页插件

pom.xml中引入jar包

<dependency>
    <groupId>com.github.pagehelpergroupId>
    <artifactId>pagehelperartifactId>
    <version>5.0.1version>
dependency>

applicationContext中加上插件

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        
        <property name="dataSource" ref="dataSource" />
        
        <property name="typeAliasesPackage" value="cn.itsource.crm.domain" />
        
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <value>helperDialect=mysqlvalue>
                    property>
                bean>
            array>
        property>
        
        <property name="mapperLocations" value="classpath:cn/itsource/crm/mapper/*.xml" />

    bean>

3.2 准备Query

query包中

  • BaseQuery
public class BaseQuery {

    //这两个名称是easyui支持的名称
    private int page = 1; //当前页
    private int rows = 10; //每页条数

    //省略getter,setter
}
  • DepartmentQuery
public class DepartmentQuery extends BaseQuery {
    //查询条件
}

3.3 准备返回的PageResult

util包中

/**
 * Easyui需要的分页结果
 * @param 
 */
public class PageResult<T>  {
    private long total; //总条数
    private List<T> rows; //数据

    public PageResult() {}
    public PageResult(int total, List<T> rows) {
        this.total = total;
        this.rows = rows;
    }
    //省略getter,setter
}

3.4 mapper中准备查询

  • BaseMapper
 List findByQuery(BaseQuery query);
  • departmentMapper.xml

  <select id="findByQuery" parameterType="cn.itsource.crm.query.DepartmentQuery" resultMap="BaseResultMap" >
    select id, sn, name, dirPath, state, manager_id, parent_id
    from t_department
  select>

3.5 service完成功能

  • IBaseService
PageResult<T> findByQuery(BaseQuery query);
  • BaseServiceImpl
@Override
public PageResult findByQuery(BaseQuery query) {
    PageHelper.startPage(query.getPage(), query.getRows());
    //拿到所有数据
    Page page = (Page) mapper.selectAll();
    //创建一个easyui能认识的分页对象
    PageResult pageResult = new PageResult();
    pageResult.setTotal(page.getTotal());
    pageResult.setRows(page.getResult());
    return pageResult;
}

你可能感兴趣的:(IDEA+Maven搭建模块化项目)