Spring boot(三)整合mybaties+thymeleaf实现基础crud

工程结构:

  

  首先在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.liu</groupId>
    <artifactId>spring-boot-mybatis</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>spring-boot-mybatis</name>
    <url>http://maven.apache.org</url>

    <!-- spring boot parent节点,引入这个之后,在下面和spring boot相关的就不需要引入版本了; -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <!-- 指定一下jdk的版本 ,这里我们使用jdk 1.8 ,默认是1.6 -->
        <java.version>1.8</java.version>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <version>1.3.0.RELEASE</version>
            <optional>true</optional>
        </dependency>

        <!-- web支持: 1、web mvc; 2、restful; 3、jackjson支持; 4、aop ........ -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- mysql 数据库驱动. -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

        <!-- spring-boot mybatis依赖: 请不要使用1.0.0版本,因为还不支持拦截器插件, 1.1.1 是博主写帖子时候的版本,大家使用最新版本即可 -->
        <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>
            <version>1.5.2.RELEASE</version>
        </dependency>
    </dependencies>
</project>

  在启动类中开启自动扫包

 1 package com.liu.spring_boot_mybatis;
 2
 3 import org.mybatis.spring.annotation.MapperScan;
 4 import org.springframework.boot.SpringApplication;
 5 import org.springframework.boot.autoconfigure.SpringBootApplication;
 6
 7 @SpringBootApplication
 8 @MapperScan("com.liu.*")//扫描:该包下相应的class,主要是MyBatis的持久化类.
 9 public class App {
10     public static void main(String[] args) {
11         SpringApplication.run(App.class, args);
12     }
13 }

  在application.properties中配置mysql和thymeleaf

########################################################
###datasource
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password =
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
########################################################
###thymeleaf
########################################################
spring.thymeleaf.cache=false
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.prefix=classpath:/templates/

  根据数据库中表类型创建实体类

 1 package com.liu.spring_boot_mybatis.bean;
 2
 3
 4 public class Userinfo {
 5     private int id;
 6     private String username;
 7     private String password;
 8     public int getId() {
 9         return id;
10     }
11     public void setId(int id) {
12         this.id = id;
13     }
14     public String getUsername() {
15         return username;
16     }
17     public void setUsername(String username) {
18         this.username = username;
19     }
20     public String getPassword() {
21         return password;
22     }
23     public void setPassword(String password) {
24         this.password = password;
25     }
26
27 }

  根据业务创建mapper文件,本次整合只整合了crud的基础操作示例

 1 package com.liu.spring_boot_mybatis.mapper;
 2
 3 import org.apache.ibatis.annotations.*;
 4
 5 import com.liu.spring_boot_mybatis.bean.Userinfo;
 6
 7 import java.util.List;
 8
 9 @Mapper
10 public interface UserinfoMapper {
11
12     @Select("select * from userinfo")
13     public List<Userinfo> Finduser(Userinfo userinfo);
14
15     // #{id} 参数占位符
16     @Select("select * from userinfo where id=#{id}")
17     public Userinfo getid(int id);
18
19     @Insert("insert into userinfo(id,username,password) values(#{id},#{username},#{password})")
20     public int insert(Userinfo userinfo);
21
22     @Update("update userinfo set username=#{username},password=#{password} where id=#{id} ")
23     public int update(Userinfo userinfo);
24
25     @Delete("delete from userinfo where id=#{id}")
26     public  int delete(int id);
27
28 }

  service

 1 package com.liu.spring_boot_mybatis.service;
 2
 3 import java.util.List;
 4
 5 import org.apache.ibatis.annotations.Select;
 6 import org.springframework.beans.factory.annotation.Autowired;
 7 import org.springframework.stereotype.Service;
 8
 9 import com.liu.spring_boot_mybatis.bean.Userinfo;
10 import com.liu.spring_boot_mybatis.mapper.UserinfoMapper;
11
12 @Service
13 public class UserinfoService {
14     @Autowired
15     private UserinfoMapper userinfoMapper;
16
17     public List<Userinfo>  Finduser(Userinfo userinfo){return userinfoMapper.Finduser(userinfo);}
18     public Userinfo getid(int id ) {
19         return userinfoMapper.getid(id);
20     }
21     public  int insert(Userinfo userinfo) {
22         return userinfoMapper.insert(userinfo);
23     }
24     public  int update(Userinfo userinfo){return  userinfoMapper.update(userinfo);}
25     public  int delete(int id){return  userinfoMapper.delete(id);}
26 }

  Controller

 1 package com.liu.spring_boot_mybatis.controller;
 2
 3 import java.util.ArrayList;
 4 import java.util.List;
 5
 6 import javax.servlet.http.HttpServletRequest;
 7
 8 import org.springframework.beans.factory.annotation.Autowired;
 9 import org.springframework.stereotype.Controller;
10 import org.springframework.ui.Model;
11 import org.springframework.web.bind.annotation.RequestMapping;
12 import org.springframework.web.bind.annotation.RestController;
13
14 import com.liu.spring_boot_mybatis.bean.Userinfo;
15 import com.liu.spring_boot_mybatis.service.UserinfoService;
16
17 @Controller
18 public class UserinfoController {
19
20     @Autowired
21     private UserinfoService usService;
22
23     //查询全部
24     @RequestMapping(value="/Finduser")
25     public String Finduser(Model model, Userinfo userinfo) {
26         List<Userinfo> list = new ArrayList<>();
27         list = usService.Finduser(userinfo);
28         model.addAttribute("list", list);
29         model.addAttribute("contens",usService.Finduser(userinfo));
30         return "list";
31
32     }
33
34
35     @RequestMapping(value="getid")
36     public String getid(Model model, int id) {
37         Userinfo Userinfo = usService.getid(id);
38         model.addAttribute("user",Userinfo);
39         return "userEdit";
40     }
41
42     @RequestMapping(value="edit")
43     public  String update(Userinfo user){
44         this.usService.update(user);
45         return "redirect:/Finduser";
46     }
47
48     @RequestMapping(value="delete")
49     public  String delete(int id){
50         this.usService.delete(id);
51         return "redirect:/Finduser";
52     }
53
54     //增加跳转
55     @RequestMapping(value="toAdd")
56     public String  add()
57     {
58         return  "userAdd";
59     }
60
61     @RequestMapping(value="/insert")
62     public String insert(Model model,Userinfo userinfo) {
63         this.usService.insert(userinfo);
64         return "redirect:/Finduser";
65     }
66 }

   编写html 注意static,默认放的是静态文件例如css,js,在templates中放的是html模板,html引入静态资源的时候默认路径是/根目录下的

  list.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>userList</title>
    <link rel="stylesheet" th:href="@{/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>用户列表</h1>
<br/><br/>
<div class="with:80%">
    <table class="table table-hover">
        <thead>
        <tr>
            <th>#</th>
            <th>User Name</th>
            <th>Password</th>
            <th>Edit</th>
            <th>Delete</th>
        </tr>
        </thead>
        <tbody>
        <tr  th:each="list : ${list}">
            <th scope="row" th:text="${list.id}">1</th>
            <td th:text="${list.username}">neo</td>
            <td th:text="${list.password}">Otto</td>
            <td><a th:href="@{/getid(id=${list.id})}">edit</a></td>
            <td><a th:href="@{/delete(id=${list.id})}">delete</a></td>
        </tr>
        </tbody>
    </table>
</div>
<div class="form-group">
    <div class="col-sm-2 control-label">
        <a href="/toAdd" th:href="@{/toAdd}" class="btn btn-info">add</a>
    </div>
</div>

</body>
</html>

  edit.html

 1 <!DOCTYPE html>
 2 <html lang="en" xmlns:th="http://www.thymeleaf.org">
 3 <head>
 4     <meta charset="UTF-8"/>
 5     <title>user</title>
 6     <link rel="stylesheet" th:href="@{/bootstrap.css}"></link>
 7 </head>
 8 <body class="container">
 9 <br/>
10 <h1>修改用户</h1>
11 <br/><br/>
12 <div class="with:80%">
13     <form class="form-horizontal"   th:action="@{/edit}" th:object="${user}"  method="post">
14         <input type="hidden" name="id" th:value="*{id}" />
15         <div class="form-group">
16             <label for="username" class="col-sm-2 control-label">userName</label>
17             <div class="col-sm-10">
18                 <input type="text" class="form-control" name="username"  id="username" th:value="*{username}" placeholder="userName"/>
19             </div>
20         </div>
21         <div class="form-group">
22             <label for="password" class="col-sm-2 control-label" >Password</label>
23             <div class="col-sm-10">
24                 <input type="password" class="form-control" name="password" id="password"  th:value="*{password}" placeholder="Password"/>
25             </div>
26         </div>
27         <div class="form-group">
28             <div class="col-sm-offset-2 col-sm-10">
29                 <input type="submit" value="Submit" class="btn btn-info" />
30                 &nbsp; &nbsp; &nbsp;
31                 <a href="/Finduser" th:href="@{/Finduser}" class="btn btn-info">Back</a>
32             </div>
33
34         </div>
35     </form>
36 </div>
37 </body>
38 </html>

  add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>user</title>
    <link rel="stylesheet" th:href="@{/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>添加用户</h1>
<br/><br/>
<div class="with:80%">
    <form class="form-horizontal"   th:action="@{/insert}"  method="post">
        <div class="form-group">
            <label for="username" class="col-sm-2 control-label">username</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="username"  id="username" placeholder="username"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-10">
                <label for="password" class="col-sm-2 control-label" >Password</label>
                <input type="password" class="form-control" name="password" id="password" placeholder="Password"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="Submit" class="btn btn-info" />
                &nbsp; &nbsp; &nbsp;
                <input type="reset" value="Reset" class="btn btn-info" />
            </div>

        </div>
    </form>
</div>
</body>
</html>

  css太多不贴了

  直接测试

  

  大功告成~

 源码地址:https://github.com/liu119631/spring-boot

 

原文地址:https://www.cnblogs.com/mmmmyblog/p/8301891.html

时间: 2024-08-01 18:14:06

Spring boot(三)整合mybaties+thymeleaf实现基础crud的相关文章

Spring Boot:整合Spring Security

综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Authorization)之外,Spring Security还提供了诸如ACLs,LDAP,JAAS,CAS等高级特性以满足复杂场景下的安全需求.另外,就目前而言,Spring Security和Shiro也是当前广大应用使用比较广泛的两个安全框架. Spring Security 应用级别的安全主要包含两

Spring Boot学习记录(二)--thymeleaf模板

Spring Boot学习记录(二)–thymeleaf模板 标签(空格分隔): spring-boot 自从来公司后都没用过jsp当界面渲染了,因为前后端分离不是很好,反而模板引擎用的比较多,thymeleaf最大的优势后缀为html,就是只需要浏览器就可以展现页面了,还有就是thymeleaf可以很好的和spring集成.下面开始学习. 1.引入依赖 maven中直接引入 <dependency> <groupId>org.springframework.boot</gr

Spring Boot 应用系列 5 -- Spring Boot 2 整合logback

上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对于那些日志量不算太高的项目来说,选择logback更简单方便. 1. pom.xml pom.xml不需要添加任何依赖. 2. logback的配置文件 系统启动时,logback按照下列顺序加载第一个找到的配置文件: (1) classpath: logback-test.xml (2) clas

Spring Boot:整合MyBatis框架

综合概述 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集.MyBatis 可以使用简单的 XML 或注解来配置和映射原生类型.接口和 Java 的 POJO(Plain Old Java Objects,普通老式 Java 对象)为数据库中的记录.MyBatis是一款半ORM框架,相对于Hibernate这样的完全ORM框架,MyBatis显得更加灵活,因为可以直接控制SQL语句,所

Rabbit MQ和Spring Boot的整合

消息服务 背景:有时需与其它系统集成来完成相关业务功能,原始的做法是程序内部相互调用,除此之外,还可用消息服务中间件来进行业务处理,使用消息服务中间件处理业务能够提升系统的异步通信和扩展解耦的能力,个人有点面向切面的意思. 一.为什么要使用消息服务? 因为它有很多好处,能解决很多问题: 1.异步处理 2.流量消峰 3.提高效率和可靠性 二.RabbitMQ消息中间件的原理和工作模式 RabbitMQ消息中间件的原理: 1.消息发布者P向RabbitMQ代理(Broker)指定虚拟主机服务器发送消

Spring Boot 2.x教程-Thymeleaf 原理是什么

layout: post title: Spring Boot 2.x教程-Thymeleaf 原理是什么 categories: SpringBoot description: Spring Boot 2.x教程-Thymeleaf 原理是什么 keywords: SpringBoot, Spring, Thymeleaf --- 如要要理清楚 Thymeleaf 的原理,那么就要从模板引擎的原理说起.Thymeleaf只不过是众多模板中的一员,功能是一致的. 例如 JSP 也是一种模板. 1

Spring Boot:整合Swagger在线文档

综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于移动端.在实际开发过程中,这些接口还要提供给开发测试进行相关的白盒测试,那么势必存在如何在多人协作中共享和及时更新API开发接口文档的问题. 假如你已经对传统的wiki文档共享方式所带来的弊端深恶痛绝,那么尝试一下Swagger2 方式,一定会让你有不一样的开发体验. 使用 Swagger 集成文档

spring boot 资料整合

spring boot 资料整合 原文地址:https://www.cnblogs.com/lhuser/p/11087410.html

《深入实践Spring Boot》阅读笔记之一:基础应用开发

上上篇「1718总结与计划」中提到,18年要对部分项目拆分,进行服务化,并对代码进行重构.公司技术委员会也推荐使用spring boot,之前在各个技术网站中也了解过,它可以大大简化spring配置和各个组件的使用,与其关系密切的Spring Cloud可以很好支持微服务的开发. 为了后续项目的应用,想利用这2天看下<深入实践Spring Boot>,这本书是17年双十一期间在京东上买的,一直懒着没看.这本书偏应用,适合初学者看,正文内容也就240多页,看的会比较轻松. 目前,看完了第一部分,