引言
JeecgBoot是一个基于Spring Boot、MyBatis、Ant Design Vue等主流技术栈的快速开发平台,它可以帮助开发者快速构建企业级中后台应用。本文将带你从入门到精通,一步步掌握JeecgBoot框架,并通过实战案例加深理解。
一、JeecgBoot框架简介
1.1 框架特点
- 快速开发:提供丰富的组件和模板,降低开发难度,提高开发效率。
- 模块化设计:采用模块化设计,便于扩展和维护。
- 前后端分离:支持前后端分离,方便团队协作。
- 可视化配置:提供可视化配置界面,简化开发流程。
1.2 框架组成
- 后端:基于Spring Boot,采用MyBatis作为持久层框架。
- 前端:基于Ant Design Vue,提供丰富的UI组件和模板。
- 数据库:支持多种数据库,如MySQL、Oracle等。
二、JeecgBoot框架入门
2.1 环境搭建
- Java环境:安装JDK 1.8及以上版本。
- IDE:推荐使用IntelliJ IDEA或Eclipse。
- Maven:安装Maven 3.5及以上版本。
2.2 创建项目
- 下载JeecgBoot源码:从GitHub下载JeecgBoot源码。
- 导入IDE:将源码导入IDE,选择Maven项目。
- 运行项目:运行项目,访问http://localhost:8080/。
2.3 熟悉项目结构
- src/main/java:存放后端代码。
- src/main/resources:存放配置文件和静态资源。
- src/main/webapp:存放前端代码。
三、JeecgBoot框架实战
3.1 创建模块
- 进入项目根目录:
cd jeecg-boot - 执行命令:
mvn jeecg:module -DmoduleType=web -DmoduleCode=module1 - 查看模块:在
src/main/java目录下,可以看到新创建的模块。
3.2 创建实体类
- 进入模块目录:
cd module1 - 创建实体类:在
src/main/java目录下创建实体类,如User.java。 - 编写实体类代码:
package com.example.module1.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
@TableName("user")
public class User implements Serializable {
@TableId(type = IdType.AUTO)
private Long id;
private String name;
private Integer age;
// ... 其他属性和方法
}
3.3 创建Mapper接口
- 创建Mapper接口:在
src/main/java目录下创建Mapper接口,如UserMapper.java。 - 编写Mapper接口代码:
package com.example.module1.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.module1.entity.User;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends BaseMapper<User> {
// ... 自定义方法
}
3.4 创建Service接口和实现类
- 创建Service接口:在
src/main/java目录下创建Service接口,如UserService.java。 - 编写Service接口代码:
package com.example.module1.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.module1.entity.User;
public interface UserService extends IService<User> {
// ... 自定义方法
}
- 创建Service实现类:在
src/main/java目录下创建Service实现类,如UserServiceImpl.java。 - 编写Service实现类代码:
package com.example.module1.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.module1.entity.User;
import com.example.module1.mapper.UserMapper;
import com.example.module1.service.UserService;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
// ... 自定义方法
}
3.5 创建Controller类
- 创建Controller类:在
src/main/java目录下创建Controller类,如UserController.java。 - 编写Controller类代码:
package com.example.module1.controller;
import com.example.module1.entity.User;
import com.example.module1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@GetMapping
public List<User> list() {
return userService.list();
}
@PostMapping
public User add(@RequestBody User user) {
return userService.save(user);
}
// ... 其他方法
}
3.6 启动项目
- 进入项目根目录:
cd jeecg-boot - 运行项目:
mvn spring-boot:run
四、JeecgBoot框架进阶
4.1 自定义代码生成器
- 创建代码生成器:在
src/main/java目录下创建代码生成器,如CodeGenerator.java。 - 编写代码生成器代码:
package com.example.module1.generator;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
public class CodeGenerator {
public static void main(String[] args) {
// 1. 创建代码生成器对象
AutoGenerator mpg = new AutoGenerator();
// 2. 全局配置
GlobalConfig gc = new GlobalConfig();
gc.setOutputDir(System.getProperty("user.dir") + "/src/main/java");
gc.setAuthor("example");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 3. 数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/your_database?useUnicode=true&useSSL=false&characterEncoding=utf8");
dsc.setDriverName("com.mysql.cj.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("root");
mpg.setDataSource(dsc);
// 4. 包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("module1");
pc.setParent("com.example");
mpg.setPackageInfo(pc);
// 5. 策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setNaming(NamingStrategy.underline_to_camel);
strategy.setColumnNaming(NamingStrategy.underline_to_camel);
strategy.setEntityLombokModel(true);
strategy.setRestControllerStyle(true);
strategy.setInclude("user"); // 设置需要生成的表名
mpg.setStrategy(strategy);
// 6. 执行生成
mpg.execute();
}
}
- 运行代码生成器:
mvn java -Dexec.mainClass="com.example.module1.generator.CodeGenerator"
4.2 自定义模板
- 创建模板文件:在
src/main/resources/templates目录下创建模板文件,如user.html。 - 编写模板文件代码:
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<h1>User List</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<#list users as user>
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
</tr>
</#list>
</tbody>
</table>
</body>
</html>
- 修改Controller类:在
UserController.java中,修改list方法,返回User列表。
@GetMapping
public List<User> list() {
return userService.list();
}
- 修改前端页面:在
src/main/webapp/pages/user/list.html中,引入模板文件。
<!DOCTYPE html>
<html>
<head>
<title>User List</title>
</head>
<body>
<#include "user.html">
</body>
</html>
五、总结
通过本文的学习,相信你已经掌握了JeecgBoot框架的基本使用方法。在实际开发过程中,你可以根据自己的需求进行扩展和定制。希望本文能帮助你快速上手JeecgBoot框架,并祝你开发顺利!
