Spring Boot与MyBatis的集成
SQL语句:
# 创建一个名称为tb_user的表
CREATE TABLE tb_user ( id int(32) PRIMARY KEY AUTO_INCREMENT, username varchar(32), address varchar(256) );
# 插入3条数据
INSERT INTO tb_user VALUES ('', '小韩', '北京市海淀区');
INSERT INTO tb_user VALUES ('', '小石', '北京市昌平区');
INSERT INTO tb_user VALUES ('', '小陈', '北京市顺义区');
添加pom.xml依赖:
<!-- MyBatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency><!-- MySQL驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
文件3-1 application.properties
#DB Configuration
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3307/microservice
spring.datasource.username=root
spring.datasource.password=123456
#logging
logging.level.com.xc.springboot=debug
文件3-2 User.java
package com.xc.springboot.po;
public class User {
private Integer id;
private String username;
private String address;
//get set...
}
文件3-3 UserMapper.java
package com.xc.springboot.mapper; @Mapper
@Repository
public interface UserMapper { // 查询所有用户
@Select("select * from tb_user")
List<User> getAllUsers(); // 删除用户
@Delete("DELETE FROM tb_user WHERE id =#{id}")
void delete(Integer id); }
文件3-4 UserService.java
package com.xc.springboot.service;
public interface UserService {
// 查询所有
List<User> getAllUsers();
// 删除数据
void deleteUser(Integer id);
}
文件3-5 UserServiceImpl.java
package com.xc.springboot.service.impl; @Transactional
@Service
public class UserServiceImpl implements UserService { //注入用户Mapper
@Autowired
private UserMapper userMapper; @Override
public List<User> getAllUsers() {
return userMapper.getAllUsers();
} @Override
public void deleteUser(Integer id) {
System.out.println("id:" + id);
userMapper.delete(id);
} }
文件3-6 UserController.java
package com.xc.springboot.controller; @RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; // 查询所有用户
@RequestMapping("/userList")
public List<User> getAllUsers() {
return userService.getAllUsers();
} // 删除用户
@RequestMapping("/delete/{id}")
public void delete(@PathVariable Integer id) {
userService.deleteUser(id);
} }
文件3-7 user.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
<link rel="stylesheet" type="text/css" href="ui/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="ui/themes/icon.css">
<script type="text/javascript" src="ui/jquery.min.js"></script>
<script type="text/javascript" src="ui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="ui/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript"> $(function () {
$('#grid').datagrid({
url: 'user/userList',
fit: true,
columns: [[
{field: 'id', title: '编号', width: 50},
{field: 'username', title: '姓名', width: 200},
{field: 'address', title: '地址', width: 200},
{field: 'del', title: '删除', width: 100}]]
});
}); </script>
</head> <body>
<table id="grid"></table>
</body> </html>


Spring Boot与MyBatis的集成的更多相关文章
- 【分页工具-spring boot】Mybatis PageHelper 集成Spring boot
官方文档:https://github.com/pagehelper/pagehelper-spring-boot 1.引入包,测试过以下版本兼容性还是比较好的 <!--Mybatis-Spri ...
- Spring Boot学习笔记——Spring Boot与MyBatis的集成(项目示例)
1.准备数据库环境 # 创建数据库 CREATE DATABASE IF NOT EXISTS zifeiydb DEFAULT CHARSET utf8 COLLATE utf8_general_c ...
- Spring Boot(六)集成 MyBatis 操作 MySQL 8
一.简介 1.1 MyBatis介绍 MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及高级映射.MyBatis 避免了几乎所有的 JDBC代码和手动设置参数以及获取结果集. ...
- Spring boot 与mybatis 多数据源问题
https://www.cnblogs.com/ityouknow/p/6102399.html Spring Boot 集成Mybatis实现多数据源 https://blog.csdn.net/m ...
- Spring Boot 整合 Mybatis 实现 Druid 多数据源详解
摘要: 原创出处:www.bysocket.com 泥瓦匠BYSocket 希望转载,保留摘要,谢谢! “清醒时做事,糊涂时跑步,大怒时睡觉,独处时思考” 本文提纲一.多数据源的应用场景二.运行 sp ...
- 使用intelliJ创建 spring boot + gradle + mybatis站点
Spring boot作为快速入门是不错的选择,现在似乎没有看到大家写过spring boot + gradle + mybatis在intellij下的入门文章,碰巧.Net同事问到,我想我也可以写 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- Spring boot整合Mybatis
时隔两个月的再来写博客的感觉怎么样呢,只能用“棒”来形容了.闲话少说,直接入正题,之前的博客中有说过,将spring与mybatis整个后开发会更爽,基于现在springboot已经成为整个业界开发主 ...
- Spring Boot微服务如何集成fescar解决分布式事务问题?
什么是fescar? 关于fescar的详细介绍,请参阅fescar wiki. 传统的2PC提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...
随机推荐
- Vue 实例成员
Vue 一. 什么是Vue 可以独立完成前后端分离时 Web项目的JavaScript框架 二.为什么学Vue 前端三大主流框架:Angular React Vue Vue结合了其他框架优点.轻量级. ...
- Arrays.asList 存在的坑
引语: 阿里巴巴java开发规范说到使用工具类Arrays.asList()方法把数组转换成集合时,不能使用其修改集合相关的方法,它的add/remove/clear方法会抛出UnsupportedO ...
- k8s-yaml
apiVersion: v1 #指定api版本,此值必须在kubectl apiversion中 kind: Pod #指定创建资源的角色/类型 metadata: #资源的元数据/属性 name: ...
- Shell脚本之sed的使用
1.sed命令:主要作用是查找:新增 删除 和修改替换. user.txt daokr#cat user.txt ID Name Sex Age zhang M wang G cheng M huah ...
- 洛谷P1514引水入城
题目 搜索加贪心其实并不需要用到\(DP\),搜索也是比较简单地搜索. 对于每个第一行的城市进行类似于滑雪那道题的搜索,然后记录最后一行它所覆盖的区间,易得一个一行城市只会有一个区间.然后可以在最后进 ...
- 数据结构实验之排序一:一趟快排( SDUT 3398)
#include <stdio.h> #include <string.h> int a[110000]; void qusort(int l, int r, int a[]) ...
- [golang]A modern, fast and scalable websocket framework with elegant API written in Go
A modern, fast and scalable websocket framework with elegant API written in Go http://bit.ly/neffos- ...
- Python逆向(五)—— Python字节码解读
一.前言 前些章节我们对python编译.反汇编的原理及相关模块已经做了解读.读者应该初步掌握了通过反汇编获取python程序可读字节码的能力.python逆向或者反汇编的目的就是在没有源码的基础上, ...
- Perl寻路A*算法实现
A*算法:A*(A-Star)算法是一种静态路网中求解最短路径最有效的直接搜索方法.估价值与实际值越接近,估价函数取得就越好. 公式表示为: f(n)=g(n)+h(n),其中 f(n) 是从初始点经 ...
- 【转】反编译获取任何微信小程序源码(完)
一.前言最近在学习微信小程序开发,半个月学习下来,很想实战一下踩踩坑,于是就仿写了一个阿里妈妈淘宝客小程序的前端实现,过程一言难尽,差不多两周时间过去了,发现小程序的坑远比想象的要多的多!!在实际练手 ...