精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战
Posted 蓝盒子itbluebox
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战相关的知识,希望对你有一定的参考价值。
Gradle文章目录 | |
---|---|
Java之Gradle【IDEA版】入门到精通(上)(一篇文章精通系列)【安装+基本使用+项目创建+项目部署+文件操作+依赖管理+插件使用】 | |
Java之Gradle【IDEA版】入门到精通(下)(一篇文章精通系列)【创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战】 |
Java之Gradle【IDEA版】入门到精通(下)(一篇文章精通系列)【创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战】
1、创建 SpringBoot 项目
Spring Boot Gradle 插件在 Gradle 提供 Spring Boot 支持。它允许您打包可执行 jar 或 war 归档文件,运行 Spring Boot 应用程序,并使用 Spring-Boot-dependencies 提供的依赖管理。相关文档请参考:
https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/htmlsingle/#running-your-application
设置好对应的配置项
1.1、引入springboot 插件
该插件发布在 Gradle 的插件门户网站上,可以使用插件块来应用:
plugins
id 'org.springframework.boot' version '2.3.7.RELEASE' //维护springboot版本号,不单独使用,和下面两个插件一起用
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
//进行依赖管理,在引入其它boot依赖时省略版本号、解决jar包冲突问题id 'java'
1.2、引入所需要的依赖
implementation 'org.springframework.boot:spring-boot-starter-web'
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MainApp
public static void main(String[] args)
SpringApplication.run(MainApp.class,args);
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/actor")
public class ActorController
@GetMapping("/name")
public String getActorName()
return "itbluebox";
运行
运行成功
默认端口号是8080
访问:http://localhost:8080/actor/name
拓展
//类似maven当中的<dependencyManagement>标签,只做依赖的管理,不做实际依赖
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
1.3、将当前服务打包成jar
1.4、使用脚手架方式创建SpringBoot 项目
https://start.spring.io/
创建成功后自动下载压缩包
解压以后用IDEA打开
如果国外的打不开,可以使用国内的
https://start.aliyun.com/
用法一样
2、拓展spring-boot-gradle-plugin 插件
buildscript
repositories
maven
url 'https://maven.aliyun.com/repository/public'
dependencies
classpath 'org.springframework.boot:spring-boot-gradle-plugin:2.4.1'
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
运行一下
运行成功
http://localhost:8080/actor/name
3、基于SSM多模块项目案例
3.1 多项目模块划分
meinian-mobile-web: | 美年旅游项目的用户系统 |
meinian-web: | 美年旅游项目的管理员系统 |
meinian-service: | 美年旅游项目的业务逻辑层 |
meinian-dao : | 美年旅游项目的持久化层 |
meinian-bean : | 美年旅游项目的Model 封装 |
3.2 项目搭建前配置分析
3.3 代码演示
创建成功
创建成功
创建成功
创建成功
删除根目录下面的src
父工程引入依赖
group 'cn.itbluebox'
version '1.0-SNAPSHOT'
subprojects
//添加插件
apply plugin: 'java'
//基本JDK配置
sourceCompatibility = 1.8
targetCompatibility = 1.8
compileJava.options.encoding "UTF-8"
compileTestJava.options.encoding "UTF-8"
tasks.withType(JavaCompile)
options.encoding = "UTF-8"
group 'com.itbluebox'
version '1.0-SNAPSHOT'
repositories
mavenLocal()
maven url "https://maven.aliyun.com/repository/public"
maven url "https://maven.aliyun.com/repository/central"
maven url "https://maven.aliyun.com/repository/google"
maven url "https://maven.aliyun.com/repository/spring"
mavenCentral()
//依赖的配置:设置通用的依赖
dependencies
testImplementation 'org.junit.jupiter:junit-jupiter-api'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
implementation 'log4j:log4j:1.2.17'
test
useJUnitPlatform()
project("meinian-bean")
dependencies
compileOnly 'org.projectlombok:lombok:1.18.24'
project("meinian-dao")
apply plugin: 'java-library'//支持api
dependencies
api project(':meinian-bean')
implementation 'org.mybatis:mybatis-spring:1.2.3'
implementation 'com.alibaba:druid:1.0.15'
implementation 'org.mybatis:mybatis:3.3.0'
implementation 'mysql:mysql-connector-java:8.0.27'
project("meinian-service")
apply plugin: 'java-library'//支持api
dependencies
api project(':meinian-dao')
implementation 'org.springframework:spring-web:4.1.7.RELEASE'
implementation 'org.springframework:spring-test:4.0.5.RELEASE'
implementation 'org.springframework:spring-jdbc:4.1.7.RELEASE'
implementation 'org.aspectj:aspectjweaver:1.8.6'
project("meinian-web")
apply plugin: 'war'
dependencies
implementation project(':meinian-service')
implementation 'org.springframework:spring-webmvc:4.1.7.RELEASE'
implementation "com.fasterxml.jackson.core:jackson-databind:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.2.3"
compileOnly 'javax.servlet:servlet-api:2.5'
implementation 'jstl:jstl:1.2'
project("meinian-mobile-web")
apply plugin: 'war'
dependencies
//implementation project(':meinian-bean')
implementation project(':meinian-service')
implementation 'org.springframework:spring-webmvc:4.1.7.RELEASE'
implementation "com.fasterxml.jackson.core:jackson-databind:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-annotations:2.2.3"
implementation "com.fasterxml.jackson.core:jackson-core:2.2.3"
compileOnly 'javax.servlet:servlet-api:2.5'
implementation 'jstl:jstl:1.2'
package cn.itbluebox.bean;
import java.io.Serializable;
public class Admin implements Serializable
private Integer id;
private String username;
private String email;
public Integer getId()
return id;
public void setId(Integer id)
this.id = id;
public String getUsername()
return username;
public void setUsername(String username)
this.username = username;
public String getEmail()
return email;
public void setEmail(String email)
this.email = email;
@Override
public String toString()
return "Admin" +
"id=" + id +
", username='" + username + '\\'' +
", email='" + email + '\\'' +
'';
在数据集当中与之对应的内容
<?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="cn.itbluebox.mapper.AdminMapper">
<select id="getAdminList" resultType="cn.itbluebox.bean.Admin">
select id, username, email
from admin
</select>
</mapper>
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.userName=root
jdbc.password=root
import cn.itbluebox.bean.Admin;
import cn.itbluebox.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class AdminService
@Autowired
private AdminMapper adminMapper;
@Transactional
public List<Admin> getAdminList()
return adminMapper.getAdminList();
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 1.加载properties文件 -->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!-- 2.配置数据源 -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="username" value="$jdbc.userName"></property>
<property name="password" value="$jdbc.password"></property>
<property name="url" value="$jdbc.jdbcUrl"></property>
<property name="driverClassName" value="$jdbc.driverClass"></property>
</bean>
<!-- 0.配置扫描包 -->
<context:component-scan base-package="cn.itbluebox">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 4.配置数据源事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
<!-- 1.配置spring整合mybatis -->
<bean class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
</bean>
<!-- 2.配置扫描mapper接口的bean对象 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.itbluebox.mapper"/>
</bean>
</beans>
import cn.itbluebox.bean.Admin;
import cn.itbluebox.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/admin")
public class AdminController
@Autowired
private AdminService adminService;
@RequestMapping("/list")
@ResponseBody
public List<Admin> getAdminList()
System.out.println("dada");
return adminService.getAdminList();
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 1.配置扫描包 -->
<context:component-scan base-package="cn.itbluebox" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<!-- 2.配置内部资源视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>
</bean>
精通系列)创建 Springboot 项目+基于 ssm 多模块项目案例+微服务实战
精通系列SpringBoot集成ElasticSearch+项目实战
精通系列SpringBoot集成ElasticSearch+项目实战