SpringBoot整合Thymeleaf增删改查

application.yml配置文件

CustomerMapper.xml两表查询

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.cjb.customers01.mapper.CustomerMapper">

    <resultMap id="findCustomer" type="com.cjb.customers01.pojo.Customer">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="remark" column="remark"/>
        <result property="phone" column="phone"/>
        <result property="address" column="address"/>
        <result property="typeId" column="typeId"/>

        <association property="types" javaType="com.cjb.customers01.pojo.Types">
            <id property="id" column="id"/>
            <result property="typeName" column="typeName"/>
        </association>

    </resultMap>

    <select id="findAll" resultMap="findCustomer">
        select * from customer c,types t
            where c.typeId = t.id
    </select>

    <insert id="save" parameterType="customer">
        INSERT INTO customer(name,remark,phone,address,typeId)
            VALUES (#{name},#{remark},#{phone},#{address},#{typeId})
    </insert>
    <delete id="del" parameterType="int">
        delete from customer where id = #{id}
    </delete>
    <update id="update" parameterType="Customer">
        UPDATE customer
            <set>
                <if test="name!=null">
                    name = #{name},
                </if>
                <if test="remark!=null">
                    remark = #{remark},
                </if>
                <if test="phone!=null">
                    phone = #{phone},
                </if>
                <if test="address!=null">
                    address = #{address},
                </if>
                <if test="typeId!=null">
                    typeId = #{typeId},
                </if>
            </set>
        where id = #{id}
    </update>
    <select id="findById" resultType="Customer">
        select * from customer where id = #{id}
    </select>
    <select id="find" resultType="types">
        select * from types
    </select>
</mapper>

实体类

 

 

接口定义:

@Mapper
public interface CustomerMapper {
    /**
     * 查询全部
     * @return
     */
    List<Customer> findAll();

    /**
     * 根据id删除
     * @param id
     * @return
     */
    int del(int id);

    /**
     * 新增信息
     * @param customer
     * @return
     */
    int save(Customer customer);

    /**
     * 修改信息
     * @param customer
     * @return
     */
    int update(Customer customer);

    /**
     * 根据id查询
     * @param id
     * @return
     */
    Customer findById(int id);

    /**
     * 
     * 小标查询全部
     * @return
     */
    List<Types> find();
}

controller类:

@Controller
public class CustomerController {
    @Autowired
    private CustomerService customerService;

    @RequestMapping("/{page}.html")
    public String topage(@Param("page") String page){
        return page;
    }

    @RequestMapping("findAll")
    public String findAll(Model model){
        List<Customer> list = customerService.findAll();
        model.addAttribute("list",list);
        return "list";
    }
    @RequestMapping("save")
    public String save(Customer customer){
        int count = customerService.save(customer);
        if (count>0){
            return "redirect:/findAll";
        }else{
            return "404";
        }
    }
    @RequestMapping("add")
    public String add(){
        return "add";
    }
    @RequestMapping("update")
    public String update(){
        return "update";
    }
    @RequestMapping("findById")
    public String findById(int id,Model model){
        Customer byId = customerService.findById(id);
        model.addAttribute("byId",byId);
        return "update";
    }
    @RequestMapping("findDel")
    public String indDel(int id){
        int count = customerService.del(id);
        if (count>0){
            return "redirect:/findAll";
        }else{
            return "404";
        }
    }
    @RequestMapping("findUpdate")
    public String findUpdate(Customer customer){
        int count = customerService.update(customer);
        if (count>0){
            return "redirect:/findAll";
        }else{
            return "404";
        }
    }
}

查询页面  list.html

<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>查询客户信息</title>
</head>
<body>
<table align="center" border="1">
    <tr>
        <td>名称</td>
        <td>备注</td>
        <td>手机号码</td>
        <td>地址</td>
        <td>类型名称</td>
        <td>操作</td>
    </tr>
    <tr th:each="cus:${list}">
        <td th:text="${cus.name}"></td>
        <td th:text="${cus.remark}"></td>
        <td th:text="${cus.phone}"></td>
        <td th:text="${cus.address}"></td>
        <td th:text="${cus.types.typeName}"></td>
        <td>
            <a th:href="@{'findDel?id='+${cus.id}}">删除</a>
            <a th:href="@{'findById?id='+${cus.id}}">修改</a>
        </td>
    </tr>
</table>
</body>
</html>

新增页面    add.html

<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>新增客户信息</title>
</head>
<body>
    <form th:action="@{save}" method="post">
        <table border="1" align="center">
            <tr>
                <td>
                    名称:<input type="text" name="name">
                </td>
            </tr>
            <tr>
                <td>
                    备注:<input type="text" name="remark">
                </td>
            </tr>
            <tr>
                <td>
                    手机:<input type="text" name="phone">
                </td>
            </tr>
            <tr>
                <td>
                    住址:<input type="text" name="address">
                </td>
            </tr>
            <tr>
                <td>
                    类型id:<input type="text" name="typeId">
                </td>
            </tr>
            <tr align="center">
                <td>
                    <input type="submit" value="提交">
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

修改页面     update.html

<!DOCTYPE html>
<!--suppress ALL-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改客户信息</title>
</head>
<body>
<form th:action="@{findUpdate}" method="post">
    <table border="1" align="center">
        <input type="text" name="id" th:value="${byId.id}" hidden>
        <tr>
            <td>
                名称:<input type="text" name="name" th:value="${byId.name}">
            </td>
        </tr>
        <tr>
            <td>
                备注:<input type="text" name="remark" th:value="${byId.remark}">
            </td>
        </tr>
        <tr>
            <td>
                手机:<input type="text" name="phone" th:value="${byId.phone}">
            </td>
        </tr>
        <tr>
            <td>
                住址:<input type="text" name="address" th:value="${byId.address}">
            </td>
        </tr>
        <tr>
            <td>
                类型id:<input type="text" name="typeId" th:value="${byId.typeId}">
            </td>
        </tr>
        <tr align="center">
            <td>
                <input type="submit" value="修改">
            </td>
        </tr>
    </table>
</form>
</body>
</html>

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以参考以下步骤来实现Spring Boot整合Thymeleaf增删改查示例: 1. 首先,使用Spring Initializr进行项目初始化。你可以参考\[1\]中提供的博客文章来了解如何新建一个Spring Boot项目,并在勾选依赖时选择与MyBatis相关的依赖。 2. 导入必要的依赖。在你的项目的pom.xml文件中,添以下依赖: ``` <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.5</version> </dependency> ``` 这些依赖将帮助你实现Spring Boot与MyBatis、Thymeleaf和分页插件的整合。\[3\]提供了一个示例的依赖配置。 3. 创建实体类和Mapper接口。根据你的需求,创建相应的实体类和Mapper接口,用于定义数据库表和操作。 4. 编Mapper.xml文件。在resources目录下创建一个与Mapper接口对应的Mapper.xml文件,编SQL语句来实现增删改查操作。 5. 创建Service层和Controller层。在你的项目中创建Service层和Controller层,用于处理业务逻辑和接收请求。 6. 编Thymeleaf模板。在resources/templates目录下创建相应的Thymeleaf模板文件,用于展示数据和接收用户输入。 通过以上步骤,你就可以实现Spring Boot整合Thymeleaf增删改查示例了。你可以参考\[2\]提供的博客文章来了解如何进一步完善你的项目,实现多表查询、模糊查询和分页展示等功能。 #### 引用[.reference_title] - *1* *2* [springboot整合mybatis实现简单的单表增删改查(完整代码可下载)](https://blog.csdn.net/qq_51580852/article/details/127327287)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [SpringBoot+Mybatis+thymeleaf实现增删改查](https://blog.csdn.net/weixin_63920305/article/details/127975650)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值