maven(聚合)父子工程整合ssm

一、创建数据库
create table dept
(
deptno integer(4) not null,
dname varchar(14),
loc varchar(13)
);
alter table dept add constraint pk_dept primary key (deptno);
create table emp
(
empno integer(4) not null,
ename varchar(10),
job varchar(9),
mgr integer(4),
hiredate date,
sal decimal(7,2),
comm decimal(7,2),
deptno integer(2)
);
alter table emp add constraint pk_emp primary key (empno);
alter table emp add constraint fk_deptno foreign key (deptno) references dept (deptno);
create table salgrade
(
grade integer(1),
losal decimal(7,2),
hisal decimal(7,2)
);
insert into DEPT (DEPTNO, DNAME, LOC) values (10, 'ACCOUNTING', 'NEW YORK');
insert into DEPT (DEPTNO, DNAME, LOC) values (20, 'RESEARCH', 'DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC) values (30, 'SALES', 'CHICAGO');
insert into DEPT (DEPTNO, DNAME, LOC) values (40, 'OPERATIONS', 'BOSTON');
insert into SALGRADE (GRADE, LOSAL, HISAL) values (1, 700, 1200);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (2, 1201, 1400);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (3, 1401, 2000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (4, 2001, 3000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (5, 3001, 9999);
-- 下面要特别注意,%d-%m-%Y 里的 d 和 m 一定要小写
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7369, 'SMITH', 'CLERK', 7902, STR_TO_DATE('17-12-1980', '%d-%m-%Y'), 800.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7499, 'ALLEN', 'SALESMAN', 7698, STR_TO_DATE('20-02-1981', '%d-%m-%Y'), 1600.00, 300.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7521, 'WARD', 'SALESMAN', 7698, STR_TO_DATE('22-02-1981', '%d-%m-%Y'), 1250.00, 500.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7566, 'JONES', 'MANAGER', 7839, STR_TO_DATE('02-04-1981', '%d-%m-%Y'), 2975.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7654, 'MARTIN', 'SALESMAN', 7698, STR_TO_DATE('28-09-1981', '%d-%m-%Y'), 1250.00, 1400.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7698, 'BLAKE', 'MANAGER', 7839, STR_TO_DATE('01-05-1981', '%d-%m-%Y'), 2850.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7782, 'CLARK', 'MANAGER', 7839, STR_TO_DATE('09-06-1981', '%d-%m-%Y'), 2450.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7788, 'SCOTT', 'ANALYST', 7566, STR_TO_DATE('19-04-1987', '%d-%m-%Y'), 3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7839, 'KING', 'PRESIDENT', null, STR_TO_DATE('17-11-1981', '%d-%m-%Y'), 5000.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7844, 'TURNER', 'SALESMAN', 7698, STR_TO_DATE('08-09-1981', '%d-%m-%Y'), 1500.00, 0.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7876, 'ADAMS', 'CLERK', 7788, STR_TO_DATE('23-05-1987', '%d-%m-%Y'), 1100.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7900, 'JAMES', 'CLERK', 7698, STR_TO_DATE('03-12-1981', '%d-%m-%Y'), 950.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7902, 'FORD', 'ANALYST', 7566, STR_TO_DATE('03-12-1981', '%d-%m-%Y'), 3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO)
values (7934, 'MILLER', 'CLERK', 7782, STR_TO_DATE('23-01-1982', '%d-%m-%Y'), 1300.00, null, 10);
二、创建maven工程(maven project),打包方式为pom

ssm父工程pom.xml


    4.0.0
    com.xx
    elm
    0.0.1-SNAPSHOT
    pom
    
    
        controller
        service
        dao
    
    
    
        5.2.8.RELEASE
    
    
        
        
            org.springframework
            spring-context
            ${spring.version}
        
        
        
            org.springframework
            spring-webmvc
            ${spring.version}
        
        
          
              org.springframework  
              spring-jdbc  
              ${spring.version}  
        
        
        
            org.aspectj
            aspectjweaver
            1.8.7
        
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.9.0
        
        
        
            org.mybatis
            mybatis
            3.5.1
        
        
        
            org.mybatis
            mybatis-spring
            2.0.0
        
        
        
            mysql
            mysql-connector-java
            8.0.22
        
    
    
    
    
        
            
                org.apache.maven.plugins
                maven-war-plugin
                3.3.1 
            
        
    

三、分别创建子model(maven module)

controller层子module(打包方式为war包)
controller层pom.xml


  4.0.0
  
  
    com.xx
    elm
    0.0.1-SNAPSHOT
  
  
  controller
  war
   
  
   
      
          com.xx
          service
          0.0.1-SNAPSHOT
      
  

service层子module(打包方式为jar包)
service层pom.xml


  4.0.0
  
  
    com.xx
    elm
    0.0.1-SNAPSHOT
  
  
  service
  
  
  
      
          com.xx
          dao
          0.0.1-SNAPSHOT
      
  

dao层子module(打包方式为jar包)
dao层pom.xml


  4.0.0
   
  
    com.xx
    elm
    0.0.1-SNAPSHOT
  
  
  dao

四、整合用的配置文件

在controller子模块需要创建web.xml,并在类路径下放spring-mvc.xml

spring-mvc.xml:


    
    

web.xml


    controller
    
    
        
            org.springframework.web.context.ContextLoaderListener
        
    
    
    
        contextConfigLocation
        classpath:applicationContext.xml
    
      
      
        SpringMVC  
        org.springframework.web.servlet.DispatcherServlet  
          
            contextConfigLocation  
            classpath:spring-mvc.xml  
          
        1   
      
      
        SpringMVC  
          
        /  
     


在service子模块的类路径下放applicationContext.xml和db.properties

applicationContext.xml:


    
    
    
    
    
    
        
        
        
        
    
    
    
        
        
    
    
    
        
        
    
    
    
        
    
    
    

db.properties:
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/emp?characterEncoding=utf-8&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
dao子模块什么配置文件都不放

五、创建各模块需要的java类
controller模块 控制类:
package com.xx.ssm.controller; 

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
 
import com.xx.ssm.po.Dept;
import com.xx.ssm.service.DeptService; 
@Controller
public class DeptController {

    @Autowired
    @Qualifier("deptServiceImpl")
    private DeptService deptService;

    @ResponseBody
    @RequestMapping("/getDeptBy")
    public  List  getDeptBy() throws Exception{
        return deptService.getDeptBy();
    }
}
service模块 接口和实现类
package com.xx.ssm.service;

import java.util.List;

import com.xx.ssm.po.Dept;

public interface DeptService {
    public Dept getDeptById(Integer deptno);

    public List getDeptBy();
}
package com.xx.ssm.service.impl;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xx.ssm.mapper.DeptMapper;
import com.xx.ssm.po.Dept;
import com.xx.ssm.service.DeptService;
@Service
public class DeptServiceImpl implements DeptService{

    @Autowired
    private DeptMapper deptMapper;

    @Override 
    public Dept getDeptById(Integer deptno) {
        return deptMapper.getDeptById(deptno);
    } 
    @Override
    public List getDeptBy() { 
        return deptMapper.getDeptBy();
    } 
}
dao模块 接口和实体类
package com.xx.ssm.mapper;
import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import com.xx.ssm.po.Dept;
@Mapper
public interface DeptMapper {
    @Select("select * from dept where deptno = #{deptno}")
    public Dept getDeptById(int deptno);
    @Select("select * from dept")
    public List getDeptBy();
}
package com.xx.ssm.po;

public class Dept {
      private Integer deptno;
      private String dname;
      private String loc;
    public Integer getDeptno() {
        return deptno;
    }
    public void setDeptno(Integer deptno) {
        this.deptno = deptno;
    }
    public String getDname() {
        return dname;
    }
    public void setDname(String dname) {
        this.dname = dname;
    }
    public String getLoc() {
        return loc;
    } 
    public void setLoc(String loc) {
        this.loc = loc;
    }
    @Override
    public String toString() {
        return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + "]";
    }
}

整体目录结构
image.png
启动项目

右键工程以服务器启动
本地访问地址: http://localhost:8080/controller/getDeptBy

你可能感兴趣的:(maven(聚合)父子工程整合ssm)