基于maven,构建父子项目

一直想用maven建立一个父子项目,对现有的架构进行重构一下,减少代码的耦合度,直到冗余的代码越来越多,维护得有些吃力了,才痛下决心,把这个项目建立起来。

环境说明

开发工具:Spring Tool Suite 3.8.3.RELEASE
org.springframework.boot 2.1.4.RELEASE
开发环境jdk、maven路径等等,一定要提前配置好了。

架构说明

实体类、Dao层、Service层、Controller层、公共类、管理后台、前台等各为一个项目。
目的:实现实体类、Dao层、Service层、公共类代码复用,管理后台、前台不再只需维护一套以上的代码,减少维护的压力。

第一步,file->new maven project,建立父项目。
图-1

图-2

删除父项目下的src文件,只留pom文件。


图-3
pom文件的配置如下:


  4.0.0
  
  
    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE
     
  
  com.example
  example-root
  0.0.1-SNAPSHOT  
  
  pom
  这是一个父项目
  所有子项目的父项目  
  
    1.8
    Greenwich.SR1
        UTF-8
  
  
        
            
                org.springframework.cloud
                spring-cloud-dependencies
                ${spring-cloud.version}
                pom
                import
            
        
    
    
    
        
     
  
      
  

第二步建立子项目,选中父项目,点击右键,选择 other,在other里面选择maven moudle,然后next,最后finish。
图-4

图-5

这时,建成的子项目已经在父项目之中了。


图-6

从父项目的pom中可以看到,已经把子项目生成进去了。
图-7

打开子项目的pom文件,有些冗余的内容,就删掉了一些,只留必要的。



  4.0.0
  
    com.example
    example-root
    0.0.1-SNAPSHOT
  
  example-entity
  example-entity
  jar
  http://maven.apache.org  
  
  
  
    example-entity
  

就这样,以此类推,把其他需要的项目也创建好了。


图-8
第三步,项目建立好,就是项目之间的关系,对于example-service是需要调用example-dao和example-entity中的类的,这时只需要在pom以架包的形式引入即可。

       com.example
    example-dao
    0.0.1-SNAPSHOT
 
 
        com.example
    example-entity
    0.0.1-SNAPSHOT
 

对于每个项目都需要的架包,则在父类中引入,如果架包只是某一个项目中才用到的,就只需要在用到的项目中引入即可。
这时最后生成的项目的结构,也就是父子项目。


图-9
第四步,把每个项目中的代码完善一下。

1)连接数据库的配置文件,放在了common项目中。

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.alibaba.druid.pool.DruidDataSource;

@Configuration
@MapperScan("com.example.dao")
public class DruidDatabaseConfig {

    /**
     * DruidDataSource数据源中默认的是取druid开头的,所以这里需要设置一下前缀prefix
     * 使用com.alibaba的DruidDataSource数据源
     * @return
     *
     */
    @Bean
    @ConfigurationProperties(prefix="spring.datasource")
    public DataSource dataSource() {
        return new DruidDataSource();        
    }
}

2)增加配置文件:

spring.datasource.url=jdbc:mysql://localhost:3306/test1?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.example.entity

pom文件如下:



  4.0.0
  
    com.example
    example-root
    0.0.1-SNAPSHOT
  
  example-common
  example-common
  jar
  http://maven.apache.org  
    
  
  
    example-common
  

service项目,一个接口,一个实现类;

import java.util.List;
import com.example.entity.Test;

public interface ITestService {

    public void addTest(Test test);
    
    public void updateTest(Test test);
    
    public Test getTest(Test test);
    
    public List findTests();
}
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.example.dao.TestMapper;
import com.example.entity.Test;
import com.example.service.ITestService;

@Service("testServiceImpl")
public class TestServiceImpl implements ITestService {

    @Autowired
    private TestMapper testMapper;
    
    @Override
    public void addTest(Test test) {
        testMapper.insertSelective(test);
    }

    @Override
    public void updateTest(Test test) {
        testMapper.updateByPrimaryKeySelective(test);
    }

    @Override
    public Test getTest(Test test) {
        return testMapper.selectByPrimaryKey(test.getUserUid());
    }

    @Override
    public List findTests() {
        return null;
    }

}

dao项目,一个mapper类,一个mapper.xml配置文件,特别是xml文件,里面的类名对象名都要写对。这两个文件也有可能是工具生成的,生成的时候,包名一定要设置对了。

import com.example.entity.Test;

public interface TestMapper {
    
    int deleteByPrimaryKey(String userUid);

    int insert(Test record);

    int insertSelective(Test record);

    Test selectByPrimaryKey(String userUid);

    int updateByPrimaryKeySelective(Test record);

    int updateByPrimaryKey(Test record);
}




  
    
    
    
    
    
    
    
    
    
    
  
  
    user_uid, create_date, create_user, status, update_date, update_user, user_mobile, 
    user_name, user_pwd, user_birthday
  
  
  
    delete from t_test
    where user_uid = #{userUid,jdbcType=VARCHAR}
  
  
    insert into t_test (user_uid, create_date, create_user, 
      status, update_date, update_user, 
      user_mobile, user_name, user_pwd, 
      user_birthday)
    values (#{userUid,jdbcType=VARCHAR}, #{createDate,jdbcType=DATE}, #{createUser,jdbcType=VARCHAR}, 
      #{status,jdbcType=TINYINT}, #{updateDate,jdbcType=DATE}, #{updateUser,jdbcType=VARCHAR}, 
      #{userMobile,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{userPwd,jdbcType=VARCHAR}, 
      #{userBirthday,jdbcType=DATE})
  
  
    insert into t_test
    
      
        user_uid,
      
      
        create_date,
      
      
        create_user,
      
      
        status,
      
      
        update_date,
      
      
        update_user,
      
      
        user_mobile,
      
      
        user_name,
      
      
        user_pwd,
      
      
        user_birthday,
      
    
    
      
        #{userUid,jdbcType=VARCHAR},
      
      
        #{createDate,jdbcType=DATE},
      
      
        #{createUser,jdbcType=VARCHAR},
      
      
        #{status,jdbcType=TINYINT},
      
      
        #{updateDate,jdbcType=DATE},
      
      
        #{updateUser,jdbcType=VARCHAR},
      
      
        #{userMobile,jdbcType=VARCHAR},
      
      
        #{userName,jdbcType=VARCHAR},
      
      
        #{userPwd,jdbcType=VARCHAR},
      
      
        #{userBirthday,jdbcType=DATE},
      
    
  
  
    update t_test
    
      
        create_date = #{createDate,jdbcType=DATE},
      
      
        create_user = #{createUser,jdbcType=VARCHAR},
      
      
        status = #{status,jdbcType=TINYINT},
      
      
        update_date = #{updateDate,jdbcType=DATE},
      
      
        update_user = #{updateUser,jdbcType=VARCHAR},
      
      
        user_mobile = #{userMobile,jdbcType=VARCHAR},
      
      
        user_name = #{userName,jdbcType=VARCHAR},
      
      
        user_pwd = #{userPwd,jdbcType=VARCHAR},
      
      
        user_birthday = #{userBirthday,jdbcType=DATE},
      
    
    where user_uid = #{userUid,jdbcType=VARCHAR}
  
  
    update t_test
    set create_date = #{createDate,jdbcType=DATE},
      create_user = #{createUser,jdbcType=VARCHAR},
      status = #{status,jdbcType=TINYINT},
      update_date = #{updateDate,jdbcType=DATE},
      update_user = #{updateUser,jdbcType=VARCHAR},
      user_mobile = #{userMobile,jdbcType=VARCHAR},
      user_name = #{userName,jdbcType=VARCHAR},
      user_pwd = #{userPwd,jdbcType=VARCHAR},
      user_birthday = #{userBirthday,jdbcType=DATE}
    where user_uid = #{userUid,jdbcType=VARCHAR}
  

第五步,测试。

在Controller项目中,新建了一个测试文件:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.entity.Test;
import com.example.service.ITestService;

@RestController
public class TestController {
    
    @Autowired
    private ITestService testServiceImpl;

    @GetMapping("/index")
    public Object index(){
        Test test = new Test();
        test.setUserUid("20190724");
        test.setUserName("teststest");
        testServiceImpl.addTest(test);
        return test;
    }
    
    @GetMapping("/index1")
    public Object index1(){
        Test test = new Test();
        test.setUserUid("20190724");
        test = testServiceImpl.getTest(test);
        return test;
    }
}

测试结果图

总结:
在建立父子项目时,由于包名写错了,报service或mapper找不到的问题,这时,只要根据错误信息对报错的包进行修改就可以了。

父子项目,可以很好地实现对代码的复用,特别是像实体类、service层、工具类的复用。

使用工具生成的文件,一定要制定核对pom文件,pom文件生成时,有时会漏掉一些参数,这个还需要找一篇范文来比对一下。

你可能感兴趣的:(基于maven,构建父子项目)