springboot 多模块将dao(mybatis)项目拆分出去

网友投稿 357 2023-02-06


springboot 多模块将dao(mybatis)项目拆分出去

前言:

以前我们在建项目的时候, 要么将所有的package建在一个项目里面, 在处理引用的时候, 真的很方便. 不用担心, 有些东西配置不到或者读取不到.

或者, 将package独立出去, 到一个项目中或者子项目中. 这时候, 项目中的引用处理, 还是有些麻烦的. 不过好处更多, 不再表述.

在 idea 里面, 推荐使用 多模块 建项目, 而不再是 eclipse 里面的那种方式. 那这里, 就试着将一个springboot 的项目拆分到子模块中去, 看看效果如何.

项目拆分:

1. 目录变化

2. 父项目

父pom.xml理论上来说, 应该是对子项目中的引用进行一个约束, 主要是版本约束.

所以父 pom.xm 中, 应该使用dependencyManagement 来约束 子项目中 jar 包版本.

然后对于一些子项目都用得着的引用, 可以提到 父项目中去.

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

pom

parent

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

pojo

web

dao

service

simpl

UTF-8

UTF-8

1.8

1.1.3

1.3.1

5.1.44

com.alibaba

druid

${druid.version}

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis.boot.starter.version}

mysql

mysql-connector-java

${mysql.connector.java.version}

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

<artifactId>spring-boot-starter-test

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

pom

parent

Demo project for Spring Boot

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

pojo

web

dao

service

simpl

UTF-8

UTF-8

1.8

1.1.3

1.3.1

5.1.44

com.alibaba

druid

${druid.version}

org.mybatis.spring.boot

mybatis-spring-boot-starter

${mybatis.boot.starter.version}

mysql

mysql-connector-java

${mysql.connector.java.version}

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

<artifactId>spring-boot-starter-test

org.springframework.boot

spring-boot-maven-plugin

3. 子项目 - pojo

3.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

pojo

0.0.1-SNAPSHOT

jar

pojo

Demo project for Spring Boot

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

pojo

0.0.1-SNAPSHOT

jar

pojo

Demo project for Spring Boot

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-maven-plugin

3.2 db实体

package cn.elvinle.pojo;

/**

* @author: elvin

*/

public class User {

private int id;

private String name;

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

对于pojo和dao中的内容, 可以使用mybatis逆向工程生成, 可以省去很多麻烦.

4. 子项目 - dao

4.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

dao

0.0.1-SNAPSHOT

jar

dao

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

com.alibaba

druid

mysql

mysql-connector-java

org.mybatis.spring.boot

mybatis-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

dao

0.0.1-SNAPSHOT

jar

dao

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

com.alibaba

druid

mysql

mysql-connector-java

org.mybatis.spring.boot

mybatis-spring-boot-starter

org.springframework.boot

spring-boot-maven-plugin

4.2 mapper

UserMapper.java:

public interface UserMapper {

public List getAll();

}

UserMapper.xml:

select * from user

5. 子项目 - service

5.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

service

0.0.1-SNAPSHOT

jar

service

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

service

0.0.1-SNAPSHOT

jar

service

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

5.2 UserService.java

public interface UserService {

public List getAll();

}

6. 子项目 - simpl

6.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

simpl

0.0.1-SNAPSHOT

jar

simpl

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

cn.elvinle

service

0.0.1-SNAPSHOT

cn.elvinle

dao

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

simpl

0.0.1-SNAPSHOT

jar

simpl

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

pojo

0.0.1-SNAPSHOT

cn.elvinle

service

0.0.1-SNAPSHOT

cn.elvinle

dao

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

6.2 UserSImpl

@Service

public class UserSImpl implements UserService {

@Autowired

private UserMapper userMapper;

@Override

public List getAll() {

System.out.println("UserSImpl getAll");

return userMapper.getAll();

}

}

7. 子项目 - web

关键的地方来了.

7.1 pom.xml

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

web

0.0.1-SNAPSHOT

jar

web

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

simpl

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

cn.elvinle

parent

0.0.1-SNAPSHOT

cn.elvinle

web

0.0.1-SNAPSHOT

jar

web

Demo project for Spring Boot

UTF-8

UTF-8

1.8

cn.elvinle

simpl

0.0.1-SNAPSHOT

org.springframework.boot

spring-boot-maven-plugin

7.2 application.yml

server:

context-path: /parent

port: 8080

mybatis:

config-location: /mapper/*.xml

spring:

datasource:

username: root

password: root

url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf-8

driver-class-name: com.mysql.jdbc.Driver

7.3 控制器

@RestController

@RequestMapping("api")

public class ApiController {

@Autowired

private UserService userService;

@RequestMapping("index")

public List index(){

List all = userService.getAll();

return all;

}

}

到目前为止, 没什么特别的, 都是正常修改, 接下来, 会出现与不分模块不同的地方.

7.4 入口处修改

@ComponentScan({"cn.elvinle"})

@SpringBootApplication

public class WebApplication {

public static void main(String[] args) {

SpringApplication.run(WebApplication.class, args);

}

}

这时候, 如果直接运行起程序, 是不会成功访问的. 原因在于, mybatis的自动化配置和自动化创建, 没有支持到多模块中.

这时候, 需要我们手动进行配置和创建.

7.5 Mybatis java 配置

可以对照着上面的配置文件, 进行java配置

MybatisConfig:

@Configuration

public class MybatisConfig {

@Value("${mybatis.config-location}")

private String mapperLocationPattern;

@Bean

@ConfigurationProperties(prefix = "spring.datasource")

public DataSource dataSource(){return new com.alibaba.druid.pool.DruidDataSource();

}

@Bean(name="sqlSessionFactory")

public SqlSessionFactory sqlSessionFactory() throws Exception{

SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

sqlSessionFactoryBean.setDataSource(dataSource());

PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

sqlSessionFactoryBean.setMapperLocations(resolver.getResources(mapperLocationPattern));

return sqlSessionFactoryBean.getObject();

}

}

MybatisMapperScannerConfig:

@Configuration

@AutoConfigureAfter(MybatisConfig.class)

@MapperScan("cn.elvinle.dao.mapper")

public class MybatisMapperScannerConfig {

public MapperScannerConfigurer mapperScannerConfigurer(){

MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();

mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");

mapperScannerConfigurer.setBasePackage("cn.elvinle.dao.mapper");

return mapperScannerConfigurer;

}

}

OK, 到这里, 就可以把程序跑起来了, 看一下结果:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:vue裁切预览组件功能的实现步骤
下一篇:在 Vue 项目中引入 tinymce 富文本编辑器的完整代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~