工程结构:

  

  首先在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>

  在启动类中开启自动扫包

 package com.liu.spring_boot_mybatis;

 import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
@MapperScan("com.liu.*")//扫描:该包下相应的class,主要是MyBatis的持久化类.
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}

  在application.properties中配置mysql和thymeleaf

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

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

 package com.liu.spring_boot_mybatis.bean;

 public class Userinfo {
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;
} }

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

 package com.liu.spring_boot_mybatis.mapper;

 import org.apache.ibatis.annotations.*;

 import com.liu.spring_boot_mybatis.bean.Userinfo;

 import java.util.List;

 @Mapper
public interface UserinfoMapper { @Select("select * from userinfo")
public List<Userinfo> Finduser(Userinfo userinfo); // #{id} 参数占位符
@Select("select * from userinfo where id=#{id}")
public Userinfo getid(int id); @Insert("insert into userinfo(id,username,password) values(#{id},#{username},#{password})")
public int insert(Userinfo userinfo); @Update("update userinfo set username=#{username},password=#{password} where id=#{id} ")
public int update(Userinfo userinfo); @Delete("delete from userinfo where id=#{id}")
public int delete(int id); }

  service

 package com.liu.spring_boot_mybatis.service;

 import java.util.List;

 import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.liu.spring_boot_mybatis.bean.Userinfo;
import com.liu.spring_boot_mybatis.mapper.UserinfoMapper; @Service
public class UserinfoService {
@Autowired
private UserinfoMapper userinfoMapper; public List<Userinfo> Finduser(Userinfo userinfo){return userinfoMapper.Finduser(userinfo);}
public Userinfo getid(int id ) {
return userinfoMapper.getid(id);
}
public int insert(Userinfo userinfo) {
return userinfoMapper.insert(userinfo);
}
public int update(Userinfo userinfo){return userinfoMapper.update(userinfo);}
public int delete(int id){return userinfoMapper.delete(id);}
}

  Controller

 package com.liu.spring_boot_mybatis.controller;

 import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import com.liu.spring_boot_mybatis.bean.Userinfo;
import com.liu.spring_boot_mybatis.service.UserinfoService; @Controller
public class UserinfoController { @Autowired
private UserinfoService usService; //查询全部
@RequestMapping(value="/Finduser")
public String Finduser(Model model, Userinfo userinfo) {
List<Userinfo> list = new ArrayList<>();
list = usService.Finduser(userinfo);
model.addAttribute("list", list);
model.addAttribute("contens",usService.Finduser(userinfo));
return "list"; } @RequestMapping(value="getid")
public String getid(Model model, int id) {
Userinfo Userinfo = usService.getid(id);
model.addAttribute("user",Userinfo);
return "userEdit";
} @RequestMapping(value="edit")
public String update(Userinfo user){
this.usService.update(user);
return "redirect:/Finduser";
} @RequestMapping(value="delete")
public String delete(int id){
this.usService.delete(id);
return "redirect:/Finduser";
} //增加跳转
@RequestMapping(value="toAdd")
public String add()
{
return "userAdd";
} @RequestMapping(value="/insert")
public String insert(Model model,Userinfo userinfo) {
this.usService.insert(userinfo);
return "redirect:/Finduser";
}
}

   编写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

 <!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="@{/edit}" th:object="${user}" method="post">
<input type="hidden" name="id" th:value="*{id}" />
<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" th:value="*{username}" placeholder="userName"/>
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label" >Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" name="password" id="password" th:value="*{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;
<a href="/Finduser" th:href="@{/Finduser}" class="btn btn-info">Back</a>
</div> </div>
</form>
</div>
</body>
</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

 

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

  1. spring boot 学习(二)spring boot 框架整合 thymeleaf

    spring boot 框架整合 thymeleaf spring boot 的官方文档中建议开发者使用模板引擎,避免使用 JSP.因为若一定要使用 JSP 将无法使用. 注意:本文主要参考学习了大神 ...

  2. Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结

    Spring Boot + MyBatis + Druid + Redis + Thymeleaf 整合小结 这两天闲着没事想利用**Spring Boot**加上阿里的开源数据连接池**Druid* ...

  3. Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客

    ==他的博客应该不错,没有细看 Spring Boot学习记录(二)--thymeleaf模板 - CSDN博客 http://blog.csdn.net/u012706811/article/det ...

  4. Spring boot Mybatis 整合(注解版)

    之前写过一篇关于springboot 与 mybatis整合的博文,使用了一段时间spring-data-jpa,发现那种方式真的是太爽了,mybatis的xml的映射配置总觉得有点麻烦.接口定义和映 ...

  5. Spring Boot:整合Spring Security

    综合概述 Spring Security 是 Spring 社区的一个顶级项目,也是 Spring Boot 官方推荐使用的安全框架.除了常规的认证(Authentication)和授权(Author ...

  6. Spring Boot:整合Spring Data JPA

    综合概述 JPA是Java Persistence API的简称,是一套Sun官方提出的Java持久化规范.其设计目标主要是为了简化现有的持久化开发工作和整合ORM技术,它为Java开发人员提供了一种 ...

  7. Spring Boot:整合Shiro权限框架

    综合概述 Shiro是Apache旗下的一个开源项目,它是一个非常易用的安全框架,提供了包括认证.授权.加密.会话管理等功能,与Spring Security一样属基于权限的安全框架,但是与Sprin ...

  8. Spring boot Mybatis 整合(完整版)

    个人开源项目 springboot+mybatis+thymeleaf+docker构建的个人站点开源项目(集成了个人主页.个人作品.个人博客) 朋友自制的springboot接口文档组件swagge ...

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

    上一篇我们梳理了Spring Boot 2 整合log4j2的配置过程,其中讲到了Spring Boot 2原装适配logback,并且在非异步环境下logback和log4j2的性能差别不大,所以对 ...

随机推荐

  1. F12调试模式下使用console自动提交

    F12调试模式下使用console自动提交(F12 的console->输入代码->按enter即可运行) 1.使用定时器setInterval进行自动提交 //方法中可使用jquery调 ...

  2. 【Sqlsever系列】日期和时间

    1   概述 本文将集合MSDN简要概述Sqlserver中日期和时间函数. 2   具体内容 2.1  date and time data types 2.2  日期和时间功能 3   参考文献 ...

  3. linux OSI七层模型、TCP/IP协议栈及每层结构大揭秘

    学习Linux,就算是像小编我这样的小萌新,也知道OSI模型.什么?!你不知道!!! 好吧,这篇秘籍拿走,不谢~~~ 一.两个协议 (1)OSI 协议模型(7层)国际协议    PDU:协议数据单元对 ...

  4. Spring Boot实战之逐行释义HelloWorld

    一.前言  研究Spring boot也有一小段时间了,最近会将研究东西整理一下给大家分享,大概会有10~20篇左右的博客,整个系列会以一个简单的博客系统作为基础,因为光讲理论很多东西不是特别容易理解 ...

  5. 用IFeatureWorkspaceAnno.CreateAnnotationClass 创建注记图层时报“The application is not licensed to modify or create schema”的错误的解决方案。

    用IFeatureWorkspaceAnno.CreateAnnotationClass 的方法创建注记图层的时候报"The application is not licensed to m ...

  6. idea svn 的使用----转载,挺好的

    Intellij IDEA svn的使用记录 这里的忽略一直灰色的,可以进入 这里的版本控制里进行忽略选择 或者 这里进行添加 这里有三个选择 按照顺序 1.忽略指定的文件 2.忽略文件夹下所有文件 ...

  7. Intellij IDEA设置运行时Java参数

    1.选中类,点击顶部菜单:Run-->Edit Configuration,如下所示: 2.选择Application,指定的类,在右边选择Configuration,并在VM options中 ...

  8. iOS 科学计数法保留N位有效数字

    iOS开发 项目中用到了将一个很大的数值转换成科学计数法的需求,转换成科学计数法的方式在iOS中其实是很好做的,使用NSNumber 的 kCFNumberFormatterScientificSty ...

  9. VM虚拟机连Linux黑屏问题

    在尝试了关闭VM的加速3D图形后,若仍黑屏(但是挂起时却能显示),可以尝试在以管理员身份cmd中输入netsh winsock reset,重启后可以恢复正常.这个问题似乎与网络某个端口有关,我上次打 ...

  10. Git 企业开发者教程

      为什么要写这样一个面向企业开发者的Git教程?这个问题也困扰我自己很久.其实我使用git的时间也不短了,但是就和正在阅读本文的每一位一样,常用的基本就是那么几个(git clone, git pu ...