springboot + freemaker + mybatisplus + mysql 搭建简单项目

直接上代码

简单crud:  https://www.jianshu.com/p/ceb1df475021

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;

import lombok.Data;

@Data
public class UserVo extends User {

    private String className;

}

 

package com.mumu.domain;

import lombok.Data;

@Data
public class UserVi extends User {

    private String sex;

    private Integer age;

}

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 数据表user和class, 手动在Navicat里面创建, 反正这两张表很简单

------------------------------------------------------------------------------------------------

11.index.ftl(这个还没写完,看看哪里需要修改的,可以全选复制后试试效果),user.ftl 没有写,是个空文件

    这个是使用了freemaker控制的html文件

<!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>

 

 

12.到这里,将前台的数据就可以传到后台的,但是后台数据传到前台并且【展示出来】 还没有写,不知道 何时写...

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值