springboot + freemaker + mybatisplus + mysql 搭建简单项目
程序员文章站
2022-03-26 16:53:42
...
直接上代码
1.项目结构
2.pom.xml(核心部分,不要全选覆盖)
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- mybatisPlus 核心库 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<!-- 引入阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3.application.yml 和application-dev.yml
spring:
profiles:
active: dev
# 配置端口
server:
port: 8089
spring:
# 配置数据源
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1130?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
4.UserMapper(接口) ,集成mybatispls自带接口
package com.mumu.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.mumu.domain.User;
import com.mumu.domain.UserVo;
import org.springframework.stereotype.Repository;
@Repository
public interface UserMapper extends BaseMapper<User> {
UserVo findUserById(Integer id);
}
5.UserService.java(为了方便,没有写service的接口)
package com.mumu.service;
import com.mumu.domain.User;
import com.mumu.domain.UserVo;
import com.mumu.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserService {
@Autowired
private UserMapper userMapper;
public List<User> findUserList(){
return userMapper.selectList(null);
}
public String saveUser(User user){
userMapper.insert(user);
return "insert success";
}
public String updateUserById(User user){
userMapper.updateById(user);
return "update success";
}
public String deleteUserById(Integer id){
userMapper.deleteById(id);
return "delete success";
}
public UserVo findUserById(Integer id) {
return userMapper.findUserById(id);
}
}
6.UserController.java
package com.mumu.controller;
import com.mumu.domain.User;
import com.mumu.domain.UserVi;
import com.mumu.domain.UserVo;
import com.mumu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
@ResponseBody
private List<User> findUserList(){
List<User> userList = userService.findUserList();
return userList;
}
@RequestMapping("/saveUser")
@ResponseBody
private String saveUser(UserVi userVi){
System.out.println(userVi);
//return userService.saveUser(user);
return null;
}
@RequestMapping("/updateUser")
@ResponseBody
private String updateUser(User user){
return userService.updateUserById(user);
}
@RequestMapping("/deleteUser")
@ResponseBody
private String deleteUser(User user){
return userService.deleteUserById(user.getId());
}
@RequestMapping("/findUserById")
private String findUserById(@RequestParam("id") Integer id , ModelMap model){
UserVo user = userService.findUserById(id);
model.addAttribute("userById" , user);
return "user/user";
}
}
7.User.java
package com.mumu.domain;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;
@Data
public class User {
@TableId(value = "id")
private Integer id;
private String name;
private String psw;
private Integer classid;
}
8.vo和vi
package com.mumu.domain;
public class UserVo extends User {
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
package com.mumu.domain;
public class UserVi extends User {
private String sex;
private Integer age;
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserVi{" +
"sex='" + sex + '\'' +
", age=" + age +
" name=" + super.getName() +
" psw=" + super.getPsw() +
" class=" + super.getClassid() +
'}';
}
}
9.UserMapper.xml(两张表,一张是user,一张是class,需要创建这两张表)
<?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.mumu.mapper.UserMapper">
<!-- 通用查询映射结果 -->
<resultMap id="BaseResultMap" type="com.mumu.domain.UserVo">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="psw" property="psw" />
<result column="classid" property="classid"/>
</resultMap>
<select id="findUserById" resultType="com.mumu.domain.UserVo">
select u.id id , u.name name , u.psw psw , u.classid classid , c.name className from user u left join class c on u.classid = c.id where u.id = #{id}
</select>
</mapper>
10.index.ftl(这个还没写完,看看哪里需要修改的,可以全选复制后试试效果),user.ftl 没有写,是个空文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>what are you doing</title>
</head>
<body>
<form action="/user/saveUser" method="post">
<h1 align="center">添加用户信息</h1>
<table align="center">
<tr>
<td>姓名</td>
<td><input type="text" name="name"></td>
</tr>
<tr>
<td>密码</td>
<td>
<input type="password" name="psw">
</td>
</tr>
<tr>
<td>性别</td>
<td>
<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女
</td>
</tr>
<tr>
<td>年龄</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td>所在班级</td>
<td>
<select name="classid">
<option value="1">普通班</option>
<option value="2">实验班</option>
<option value="3">火箭班</option>
<option value="4">无人管教班</option>
<option value="5">尚未分班</option>
</select>
</td>
</tr>
<tr>
<td>
<button type="submit">提交</button>
</td>
<td>
<button type="reset">重置</button>
</td>
</tr>
</table>
</form>
</body>
</html>
11.到这里,将前台的数据就可以传到后台的,但是后台数据传到前台并且【展示出来】 还没有写,不知道 何时写...
推荐阅读
-
如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能
-
IDEA快速搭建SpringBoot的项目(一)最简单的demo
-
springboot + mybatisPlus 搭建项目实例demo
-
SpringBoot+MyBatisPlus+Vue 前后端分离项目快速搭建过程(前端篇)
-
springboot+druid+mybatis 多数据源项目搭建(2个mysql+1个sqlserver)
-
最全面的springBoot集成mybatis+mysql项目搭建经验分享
-
基于 IDEA 搭建一个简单的 SpringBoot 项目
-
如何利用IDEA搭建SpringBoot项目整合mybatis实现简单的登录功能
-
springBoot之如何搭建第一个简单的springBoot项目(Hello World)
-
快速搭建第一个简单的SpringBoot项目