SpringBoot整合SpringMVC+Mybatis+Thymeleaf+HTML

SpringBoot整合SpringMVC+Mybatis

1、创建springboot项目,配置pom文件,添加依赖。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ybjt</groupId>
    <artifactId>springBoot-CRUD</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--引入父项目依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>


    <dependencies>
        <!--SpringBoot启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--web启动器-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

        <!--Mybatis的启动器-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <!--数据库连接驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.21</version>
        </dependency>
        <!-- druid 数据库连接池 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.0.9</version>
        </dependency>

        <!--配置文件提示-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <version>2.1.3.RELEASE</version>
        </dependency>
    </dependencies>
    <!--打包插件-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties

# 数据库连接参数配置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ibtais
spring.datasource.username=root
spring.datasource.password=root

# 数据库连接池配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

# 扫描实体类设置别名,减少重复书写
mybatis.type-aliases-package=com.ybjt.bean

2、准备数据库

CREATE TABLE `user` (
  `ID` int(11) NOT NULL AUTO_INCREMENT,
  `USERNAME` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `PASSWORD` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=24 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

3、实体类

package com.ybjt.bean;

/**
 * @author zx_h
 * @create 2019-05-22 9:52
 */
public class User {
    private int id;
    private String username;
    private String password;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                '}';
    }
}

4、mapper接口

package com.ybjt.mapper;

import com.ybjt.bean.User;

/**
 * @author zx_h
 * @create 2019-05-22 9:54
 */
public interface UserMapper {

    //根据id查询
    User selectOneByID(Integer id);
}

5、xxxMapper.xm文件

<?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.ybjt.mapper.UserMapper">
    <select id="selectOneByID" resultType="user">
        select * from user where id=#{id}
    </select>
</mapper>

6、service接口

package com.ybjt.service;

import com.ybjt.bean.User;

/**
 * @author zx_h
 * @create 2019-05-22 10:09
 */
public interface UserService {
    /**
     * 根据id查询
     * @param id
     * @return
     */
    User selectOneByID(Integer id);
}

7、service实现类

package com.ybjt.service.impl;

import com.ybjt.bean.User;
import com.ybjt.mapper.UserMapper;
import com.ybjt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zx_h
 * @create 2019-05-22 10:11
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User selectOneByID(Integer id) {
       return this.userMapper.selectOneByID(id);
    }
}

8、controller

package com.ybjt.controller;

import com.ybjt.bean.User;
import com.ybjt.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author zx_h
 * @create 2019-05-22 10:16
 */
@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * restful风格跳转页面
     * @param page
     * @return
     */
    @RequestMapping("/{page}")
    public String execute(@PathVariable String page){
        return page;
    }

    @RequestMapping("show")
    public String selectOneByID(Integer id, Model model){
        System.out.println(id);
        User user = this.userService.selectOneByID(id);
        System.out.println(user);
        model.addAttribute("user",user);
        return "show";
    }
}

9、html页面

select.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>查询</title>
</head>
<body>
    <h4>查询</h4>
    <form th:action="@{/user/show}" method="get">
        <input type="text" name="id"/>
        <input type="submit" value="查询"/>
    </form>
</body>
</html>

show.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据展示</title>
</head>
<body>
    <table border="1px">
        <tr>
            <th>编号</th>
            <th>姓名</th>
            <th>密码</th>
        </tr>
        <tr>
            <td th:text="${user.id}"></td>
            <td th:text="${user.username}"></td>
            <td th:text="${user.password}"></td>
        </tr>
    </table>
</body>
</html>

10、启动类

package com;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author zx_h
 * @create 2019-05-22 10:23
 * @MapperScan扫描MyBatis的Mapper接口
 */
@SpringBootApplication
@MapperScan("com.ybjt.mapper")
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class,args);
    }
}

11、访问路径

http://localhost:8080/user/select
项目结构

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SpringBoot+SpringMVC+Mybatis+Thymeleaf+LayUI模板引擎可以用于开发Web应用程序,实现编辑和增加功能。下面是一个示例代码: 1. 编辑功能: ```java @Controller @RequestMapping("/edit") public class EditController { @Autowired private UserService userService; @GetMapping("/{id}") public String editUser(@PathVariable("id") int id, Model model) { User user = userService.getUserById(id); model.addAttribute("user", user); return "edit"; } @PostMapping("/{id}") public String saveUser(@PathVariable("id") int id, @ModelAttribute User user) { userService.updateUser(user); return "redirect:/user/" + id; } } ``` 2. 增加功能: ```java @Controller @RequestMapping("/add") public class AddController { @Autowired private UserService userService; @GetMapping public String addUser(Model model) { model.addAttribute("user", new User()); return "add"; } @PostMapping public String saveUser(@ModelAttribute User user) { userService.addUser(user); return "redirect:/user"; } } ``` 在Thymeleaf模板中,可以使用LayUI模板引擎来实现页面的美化和交互效果。例如,编辑页面的模板代码如下: ```html <!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Edit User</title> <link rel="stylesheet" th:href="@{/layui/css/layui.css}"> </head> <body> <form class="layui-form" th:object="${user}" th:action="@{/edit/{id}(id=${user.id})}" method="post"> <div class="layui-form-item"> <label class="layui-form-label">Name</label> <div class="layui-input-block"> <input type="text" th:field="*{name}" class="layui-input"> </div> </div> <div class="layui-form-item"> <label class="layui-form-label">Age</label> <div class="layui-input-block"> <input type="text" th:field="*{age}" class="layui-input"> </div> </div> <div class="layui-form-item"> <div class="layui-input-block"> <button class="layui-btn" lay-submit lay-filter="formDemo">Save</button> </div> </div> </form> <script src="/layui/layui.js"></script> </body> </html> ``` 在上述代码中,使用了Thymeleaf的表达式语法来绑定表单数据和提交地址。同时,引入了LayUI的样式和脚本文件,实现了表单的美化和提交按钮的交互效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值