最近闲来无事,搭建了一个多模块的框架,使用框架技术为springboot、jpa、mysql,框架结构层次如图:
1、搭建步骤:
(1)新建父工程File->New->Project,如图:
填写对应的GroupId和ArtifactId,点击next,然后finish。
(2)在新建的父工程上单击右键new->module,新建子模块service,web,dao,common
(3)在父工程的pom.xml中添加所需的依赖,如下:
4.0.0
com.tian
springboot-jpa
pom
1.0-SNAPSHOT
springboot-web
springboot-service
springboot-dao
springboot-common
org.springframework.boot
spring-boot-starter-web
2.1.0.RELEASE
org.springframework.boot
spring-boot-starter-test
2.1.0.RELEASE
test
org.springframework.boot
spring-boot-starter-data-jpa
2.1.0.RELEASE
mysql
mysql-connector-java
8.0.13
org.projectlombok
lombok
1.18.4
provided
这里只是添加了很少的一部分依赖,请按需添加;dependencyManagement标签不知道用法的请自行百度,这里不做过多的表述。
(4)在web子模块中的pom.xml中添加依赖,如下:
springboot-jpa
com.tian
1.0-SNAPSHOT
4.0.0
springboot-web
com.tian
springboot-service
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
(5)在service子模块的pom.xml中添加依赖:
springboot-jpa
com.tian
1.0-SNAPSHOT
4.0.0
springboot-service
com.tian
springboot-dao
1.0-SNAPSHOT
(6)在dao子模块中的pom.xml中添加依赖,如下:
springboot-jpa
com.tian
1.0-SNAPSHOT
4.0.0
springboot-dao
com.tian
springboot-common
1.0-SNAPSHOT
org.springframework.boot
spring-boot-starter-data-jpa
mysql
mysql-connector-java
org.projectlombok
lombok
(7)在common子模块的pom.xml中添加依赖,如下:
springboot-jpa
com.tian
1.0-SNAPSHOT
4.0.0
springboot-common
org.projectlombok
lombok
到此,框架的大体结构基本完成,但是由于是springboot项目,所以还需要在web子模块中添加启动类,web的结构如下图:
其中SpringWebApplication.class就是启动类,代码如下:
package com.tian.springbootweb;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
@SpringBootApplication
@ComponentScan(basePackages = {"com.tian.springbootservice","com.tian.springbootweb"})
@EnableJpaRepositories(value = {"com.tian.springbootdao.repository"})
@EntityScan(value = {"com.tian.springbootdao.entity"})
public class SpringWebApplication {
public static void main(String[] args) {
SpringApplication.run(SpringWebApplication.class,args);
}
}
@ComponentScan注解是为了扫描service和controlelr文件
@EnableJpaRepositories注解扫描repository文件
@EntityScan注解扫描entity文件
application.properties配置文件:
# 服务端口
server.port=8082
# 数据库链接驱动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据库url
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=Asia/Shanghai
# 数据库用户名
spring.datasource.username=root
# 数据库密码
spring.datasource.password=root
# 数据库类型
spring.jpa.database=mysql
# 显示sql语句
spring.jpa.show-sql=true
# 更新表结构
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
至此框架已经搭建完成
下面新建测试文件:
新建controller文件:
package com.tian.springbootweb.controller;
import com.tian.springbootdao.entity.User.UserEntity;
import com.tian.springbootservice.User.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService){
this.userService = userService;
}
@GetMapping(value = "/users")
public List getUsers(){
return userService.getUsers();
}
}
新建service文件:
package com.tian.springbootservice.User;
import com.tian.springbootdao.entity.User.UserEntity;
import com.tian.springbootdao.repository.User.UserRepository;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
private UserRepository userRepository;
public UserService(UserRepository userRepository){
this.userRepository = userRepository;
}
public List getUsers(){
return userRepository.findAll();
}
}
新建UserRepository文件:
package com.tian.springbootdao.repository.User;
import com.tian.springbootdao.entity.User.UserEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
public interface UserRepository extends JpaRepository {
}
新建UserEntity文件:
package com.tian.springbootdao.entity.User;
import lombok.Data;
import javax.persistence.*;
import java.io.Serializable;
@Entity
@Table(name = "t_user")
@Data
public class UserEntity implements Serializable {
@Id
@GeneratedValue
private Long id;
@Column(name = "name")
private String name;
@Column(name = "age")
private String age;
@Column(name = "grade")
private String grade;
}
启动服务,访问地址:http://localhost:8082/users,结果如图: