使用IDEA创建springboot多模块项目
刚下载使用IDEA的朋友可以参考以下设置:
-
IDEA基本设置 : https://blog.csdn.net/weixin_40816738/article/details/90116150.
-
IDEA中已配置阿里镜像,但maven无法下载jar包的问题 https://blog.csdn.net/redhat0921/article/details/104969687.
最新Maven阿里云仓库配置【亲测有效】
: https://www.cnblogs.com/JaxYoun/p/12699291.html. -
IDEA每次建新项目都要重新配置maven的设置 https://www.cnblogs.com/Marydon20170307/p/13277870.html?utm_source=tuicool.
首先创建一个maven项目
删除src目录
创建Module
依次创建dao entity service 子模块
注意:使用springboot创建web或API模块(controller层)
删除无用文件,只留下src
测试项目是否能正常运行
添加模块之间的依赖
目前有四个子模块:web、service、dao、entity
依赖关系是:
- web模块依赖service模块
- service模块依赖dao模块
- dao模块依赖entity模块
添加web模块pom.xml的依赖
<!-- 这是web模块原先的父依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>-->
<!-- 替换成 -->
<parent>
<artifactId>zm</artifactId>
<groupId>org.example</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<dependencies>
<!-- 添加service模块依赖 -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>service</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
添加service模块pom.xml的依赖
<dependencies>
<!-- 添加dao模块依赖 -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>dao</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
添加dao模块pom.xml的依赖
<dependencies>
<!-- 添加entity模块依赖 -->
<dependency>
<groupId>${project.parent.groupId}</groupId>
<artifactId>entity</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
entity模块pom.xml只需要添加lombok依赖
<dependencies>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
最后修改最外层pom.mxl添加springboot依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/>
</parent>
注意:项目如果报红,刷新maven即可
整合mybatis
在entity模块创建实体类
package com.zm.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
private Integer age;
private String skill;
private String evaluate;
private Integer fraction;
}
在dao模块pom.xml添加对应依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- 引入阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
dao模块代码如下
package com.zm.dao;
import com.zm.entity.User;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Mapper
@Repository
public interface UserDao {
User selectUserInfoByName(String name);
}
UserDao.mxl
<?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">
<mapper namespace="com.zm.dao.UserDao">
<resultMap id="BaseResultMap" type="com.zm.entity.User">
<result column="id" property="id"/>
<result column="name" property="name"/>
<result column="age" property="age"/>
<result column="skill" property="skill"/>
<result column="evaluate" property="evaluate"/>
<result column="fraction" property="fraction"/>
</resultMap>
<select id="selectUserInfoByName" resultMap="BaseResultMap">
SELECT * FROM user_info WHERE name = #{name}
</select>
</mapper>
service模块代码如下
package com.zm.service;
import com.zm.entity.User;
public interface UserService {
User selectUserInfoByName(String name);
}
package com.zm.service.impl;
import com.zm.dao.UserDao;
import com.zm.entity.User;
import com.zm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public User selectUserInfoByName(String name) {
return userDao.selectUserInfoByName(name);
}
}
配置application.yml
server:
port: 8080
spring:
datasource:
username: root
password: root
#?serverTimezone=UTC解决时区的报错
url: jdbc:mysql://localhost:3306/user?serverTimezone=UTC&useUnicode=true&charactUserDao.xmlerEncoding=utf-8
driver-class-name: com.mysql.cj.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#Spring Boot 默认是不注入这些属性值的,需要自己绑定
#druid 数据源专有配置
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
#配置监控统计拦截的filters,stat:监控统计、log4j:日志记录、wall:防御sql注入
#如果允许时报错 java.lang.ClassNotFoundException: org.apache.log4j.Priority
#则导入 log4j 依赖即可,Maven 地址:https://mvnrepository.com/artifact/log4j/log4j
filters: stat,wall,log4j
maxPoolPreparedStatementPerConnectionSize: 20
useGlobalDataSourceStat: true
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
mybatis:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.zm.entity
配置WebApplication
package com.zm.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication(scanBasePackages = "com.zm")
@MapperScan("com.zm.dao")
public class WebApplication {
public static void main(String[] args) {
SpringApplication.run(WebApplication.class, args);
}
}
配置最外层pom.xml的build
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.yml</include>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
新建一个user数据库导入测试sql
Date: 2020-09-26 12:01:31
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `user_info`
-- ----------------------------
DROP TABLE IF EXISTS `user_info`;
CREATE TABLE `user_info` (
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
`name` varchar(32) DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`skill` varchar(32) DEFAULT NULL COMMENT '技能',
`evaluate` varchar(64) DEFAULT NULL COMMENT '评价',
`fraction` bigint(11) DEFAULT NULL COMMENT '分数',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=16 DEFAULT CHARSET=utf8mb4 COMMENT='学生信息表';
-- ----------------------------
-- Records of user_info
-- ----------------------------
INSERT INTO `user_info` VALUES ('1', '小明', '20', '画画', '该学生在画画方面有一定天赋', '89');
INSERT INTO `user_info` VALUES ('2', '小兰', '19', '游戏', '近期该学生由于游戏的原因导致分数降低了', '64');
INSERT INTO `user_info` VALUES ('3', '张张', '18', '英语', '近期该学生参加英语比赛获得二等奖', '90');
INSERT INTO `user_info` VALUES ('4', '大黄', '20', '体育', '该学生近期由于参加篮球比赛,导致脚伤', '76');
INSERT INTO `user_info` VALUES ('5', '大白', '17', '绘画', '该学生参加美术大赛获得三等奖', '77');
INSERT INTO `user_info` VALUES ('7', '小龙', '18', 'JAVA', '该学生是一个在改BUG的码农', '59');
启动测试一下,大功告成,这就是一个简单的多模块创建了
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/133966.html