基于SpringBoot和Freemarker的用户管理系统
发布人:shili8
发布时间:2025-01-09 03:52
阅读次数:0
**基于SpringBoot和Freemarker的用户管理系统**
### 系统概述本系统是一个简单的用户管理系统,使用SpringBoot作为后端框架,Freemarker作为模板引擎。系统提供基本的用户管理功能,如注册、登录、修改个人信息等。
### 系统结构系统结构如下:
* **Controller**:负责处理请求和返回响应* **Service**:负责业务逻辑的实现* **DAO**:负责数据访问层的实现* **Model**:负责模型的定义* **Template**:使用Freemarker作为模板引擎###依赖配置在pom.xml中添加以下依赖:
xml<dependencies> <!-- SpringBoot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Freemarker --> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.31</version> </dependency> <!-- MySQL --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.27</version> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.9</version> </dependency> </dependencies>
### 配置文件在application.properties中添加以下配置:
propertiesspring.datasource.url=jdbc:mysql://localhost:3306/userspring.datasource.username=rootspring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driverspring.freemarker.cache=truespring.freemarker.template-loader-path=classpath:/templates/
### Controller**UserController.java**
java@RestController@RequestMapping("/user") public class UserController { @Autowired private UserService userService; @GetMapping("/list") public Listlist() { return userService.list(); } @PostMapping("/add") public Result add(@RequestBody User user) { userService.add(user); return new Result(true, "添加成功"); } }
### Service**UserService.java**
java@Servicepublic class UserService { @Autowired private UserDao userDao; public Listlist() { return userDao.list(); } public void add(User user) { userDao.add(user); } }
### DAO**UserDao.java**
java@Repositorypublic class UserDao { @Autowired private DruidTemplate druidTemplate; public Listlist() { return druidTemplate.queryForList("SELECT * FROM user"); } public void add(User user) { druidTemplate.update("INSERT INTO user (name, age) VALUES (?, ?)", user.getName(), user.getAge()); } }
### Model**User.java**
java@Entity@Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String name; private Integer age; }
### Template在templates目录下创建index.ftl:
ftl<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <h1>Hello, ${user.name}!</h1> </body> </html>
在UserController.java中添加以下方法:
java@GetMapping("/index") public String index(Model model) { User user = new User(); user.setName("张三"); user.setAge(18); model.addAttribute("user", user); return "index"; }
### 测试使用Postman或浏览器访问以下地址:
* GET /user/list* POST /user/add* GET /user/index