之前学了,这么多东西 thyemeafMyBatis 还有 配置文件等等,今天我们就来做一个小案例

CRUD,程序员的必备

项目结构

pom.xml

        <!-- mybatis 相关依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> <!-- thymeleaf 相关依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

3. application.properties

logging.level.org.springframework=DEBUG
#springboot mybatis
mybatis.mapper-locations = classpath:mapper/*Mapper.xml
mybatis.config-location = classpath:mapper/config/sqlMapConfig.xml
mybatis.type-aliases-package = com.spiritmark.demo.model #数据库
spring.datasource.driver-class-name= com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/springboot_test?serverTimezone=GMT
spring.datasource.username = root
spring.datasource.password = 123qwe
  1. UserController.java
package com.spiritmark.demo.controller;

import java.util.List;

import com.spiritmark.demo.model.User;
import com.spiritmark.demo.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.RequestMapping; @Controller
public class UserController { @Autowired
private UserService userService; @RequestMapping("/index")
public String showUser(Model model){
List<User> list = userService.findAll();
model.addAttribute("list",list);
return "index";
} // 进入添加界面
@RequestMapping("/intoAdd")
public String intoAdd(){
return "add";
} // 添加用户
@RequestMapping("/add")
public String add(User user){
userService.add(user);
return "redirect:/index";
} // 删除用户
@RequestMapping("/delete")
public String delete(int id){
userService.delete(id);
return "redirect:/";
} // 进入修改用户
@RequestMapping("/intoUpdate")
public String intoUpdate(Model model,int id){
User user = userService.findById(id);
model.addAttribute("user",user);
return "update";
} // 修改用户
@RequestMapping("/update")
public String update(User user){
userService.update(user);
return "redirect:/index";
} }
  1. UserMapper.java
package com.spiritmark.demo.mapper;

import com.spiritmark.demo.model.User;
import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper
public interface UserMapper { // 获取所有
List<User> findAll();
// 新增
void add(User user);
// 删除
void delete(int id);
// 编辑
void update(User user);
// 查询
User findById(int id); }
  1. UserServiceImpl.java
package com.spiritmark.demo.service.impl;

import java.util.List;
import java.util.Map; import com.spiritmark.demo.mapper.UserMapper;
import com.spiritmark.demo.model.User;
import com.spiritmark.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; @Service
public class UserServiceImpl implements UserService { @Autowired
private UserMapper userMapper; // 获取所有
@Override
public List<User> findAll() {
return userMapper.findAll();
}
// 新增
@Override
public void add(User user) {
userMapper.add(user);
}
// 删除
@Override
public void delete(int id) {
userMapper.delete(id);
}
// 修改
@Override
public void update(User user) {
userMapper.update(user);
}
// 查询
@Override
public User findById(int id) {
return userMapper.findById(id);
} }
  1. UserService.java
package com.spiritmark.demo.service;

import com.spiritmark.demo.model.User;
import java.util.List; public interface UserService {
// 获取所有
List<User> findAll();
// 新增
void add(User user);
// 删除
void delete(int id);
// 编辑
void update(User user);
// 查询
User findById(int id);
}
  1. UserMapper.xml
<?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.spiritmark.demo.mapper.UserMapper">
<select id="findAll" resultType="com.spiritmark.demo.model.User">
select * from user
</select> <insert id="add" parameterType="com.spiritmark.demo.model.User">
insert into user (username, password)
values (#{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR})
</insert> <delete id="delete" parameterType="java.lang.Integer">
delete from user where id= #{id,jdbcType=INTEGER}
</delete> <select id="findById" resultType="com.spiritmark.demo.model.User">
select * from user where id= #{id,jdbcType=INTEGER}
</select> <update id="update" parameterType="com.spiritmark.demo.model.User">
update user set
username= #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR}
where id= #{id,jdbcType=INTEGER}
</update>
</mapper>

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<a th:href="@{/intoAdd}">添加</a>
<table>
<thead>
<tr>
<th>id</th>
<th>username</th>
<th>password</th>
<th>op</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${list}">
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${user.password}"></td>
<td><a th:href="@{/delete(id=${user.id})}">删除</a><a th:href="@{/intoUpdate(id=${user.id})}">修改</a></td>
</tr>
</tbody>
</table>
</body>
</html>

add.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<form th:action="@{/add}" method="post">
<input type="text" id="username" name="username">
<input type="text" id="password" name="password">
<input type="submit">
</form>
</body>
</html>

update.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head>
<title></title>
</head>
<body>
<form th:action="@{/update}" th:object="${user}" method="post">
<input type="hidden" name="id" th:value="*{id}">
<input type="text" name="username" id="username" th:value="*{username}">
<input type="text" name="password" id="password" th:value="*{password}">
<input type="submit">
</form>
</body>
</html>

大功告成

SpringBoot从入门到精通教程(六)的更多相关文章

  1. SpringBoot从入门到精通教程(二)

    SpringBoot 是为了简化 Spring 应用的创建.运行.调试.部署等一系列问题而诞生的产物,自动装配的特性让我们可以更好的关注业务本身而不是外部的XML配置,我们只需遵循规范,引入相关的依赖 ...

  2. SpringBoot从入门到精通教程(七)

    今天,我们继续讲SpringBoot整合Redis ,也就缓存,它将与我们的Springboot整合 Redis 简介 Redis 是当前比较热门的NOSQL系统之一,它是一个开源的使用ANSI c语 ...

  3. SpringBoot从入门到精通教程(三)

    在上一篇中,我们已经讲了,SpringBoot 如何构建项目,和SpringBoot的HelloWorld, 那这一节我们继续讲 Thymeleaf Thymeleaf 官网: Thymeleaf T ...

  4. SpringBoot从入门到精通教程(一)

    写在前面的话: 在很早之前,记笔记时候,我就一直在思考一个问题,我记笔记是为了什么,我一直想不明白 ,后面发现技术跟新迭代的速度实在太快了,笔记刚纪完,技术又跟新了,于是我想了想干脆边写博客,边记笔记 ...

  5. SpringBoot从入门到精通教程(八)

    本主要介绍ElasticSearch 和 SpringBoot 的整合 ,对您有帮助的话,点个关注哦 ElastSearch 介绍 ElasticSearch是一个基于Lucene的搜索服务器.它提供 ...

  6. SpringBoot从入门到精通教程(五)

    上节,我们讲了 SpringBoot 如何使用MyBatis 今天我们讲讲 Springboot Logo自定义的问题, 我们在启动 SpringBoot 时,控制台会打印 SpringBoot Lo ...

  7. SpringBoot从入门到精通教程(四)

    前端时间整合SSM ,发现了一个现象,在整合的时候 配置文件过于复杂. 1.建工程,建目录,导入jar包. 2.配置 数据源 映射信息 等等 ... 3. 还有 各种 拦截器,控制器 ,头都大了... ...

  8. 深入浅出!springboot从入门到精通,实战开发全套教程!

    前言 之前一直有粉丝想让我出一套springboot实战开发的教程,我这边总结了很久资料和经验,在最近总算把这套教程的大纲和内容初步总结完毕了,这份教程从springboot的入门到精通全部涵盖在内, ...

  9. Spring Boot从入门到精通(六)集成Redis实现缓存机制

    Redis(Remote Dictionary Server ),即远程字典服务,是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言 ...

随机推荐

  1. selenium WebDriver提示Unable to find a matching set of capabilities解决方法

    问题出在:应该将火狐浏览器驱动添加到火狐浏览器安装目录下,并且将火狐浏览器安装目录放在path下面.(出现大意,忘了在火狐浏览器下放其对应的驱动) 亲测以下组合方式可用:   pycharm-comm ...

  2. Java(8)I/O

    目录 一.File类 1.File类概述 2.File类实例化 3.File类常用方法 二.IO流的原理 1.IO流的原理 2.input和output的理解 三.IO流的分类 1.分类 2.图示 3 ...

  3. vue 项目在 IE11 里呈现空白,不兼容的问题解决方案

    我用vue 2.6.11版本编写的项目,在谷歌浏览器上运行的好好地,但是放到ie11浏览器上却是一片空白. 这个问题遇到的时候,我是蒙蔽了,抓紧去搜了搜百度,百度上的答案倒是都很统一. 都是说ie不兼 ...

  4. LeetCode 044 Wildcard Matching

    题目要求:Wildcard Matching Implement wildcard pattern matching with support for '?' and '*'. '?' Matches ...

  5. 【2020.11.28提高组模拟】T1染色(color)

    [2020.11.28提高组模拟]T1染色(color) 题目 题目描述 给定 \(n\),你现在需要给整数 \(1\) 到 \(n\) 进行染色,使得对于所有的 \(1\leq i<j\leq ...

  6. 基于gin的golang web开发:Gin技术拾遗

    本文是对前几篇文章的一些补充,主要包含两部分:单元测试和实际项目中使用路由的小问题. 拾遗1:单元测试 Golang单元测试要求代码文件以_test结尾,单元测试方法以Test开头,参数为*testi ...

  7. 20200311_解决Could not resolve host: mirrorlist.centos.org

    [root@localhost ~]# yum -y install wget 已加载插件:fastestmirror Determining fastest mirrors Could not re ...

  8. 第14.1节 通过Python爬取网页的学习步骤

    如果要从一个互联网前端开发的小白,学习爬虫开发,结合自己的经验老猿认为爬虫学习之路应该是这样的: 一. 了解HTML语言及css知识 这方面的知识请大家通过w3school 去学习,老猿对于html总 ...

  9. Kubernetes的Local Persistent Volumes使用小记

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  10. SseEmitter推送

    后端代码SseController.java package com.theorydance.mywebsocket.server; import java.util.HashMap; import ...