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提交协议,会持有一个全局性的锁,所有局部事务预提交成功后一起提交,或有一个局部事务预提交失败后一起回滚,最 ...
随机推荐
- LightOJ - 1354 - IP Checking(进制)
链接: https://vjudge.net/problem/LightOJ-1354 题意: An IP address is a 32 bit address formatted in the f ...
- IList<> IEnumerable<> ReadOnlyCollection<> 使用方向
无论你把它声明为IList<string>,IEnumerable<string>,ReadOnlyCollection<string>或别的东西,是你...... ...
- 【转发】c#做端口转发程序支持正向连接和反向链接
可以通过中转server来连接sql server,连接的时候用ip,port,不是冒号,是逗号 但试过local port 21想连接AS400的FTP却不成功...为咩涅... https://w ...
- learning java AWT MenuBar Menu MenuItem菜单
import java.awt.*; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; import java ...
- GDB的安装
1.下载GDB7.10.1安装包 #wget http://ftp.gnu.org/gnu/gdb/gdb-7.10.1.tar.gz或者可以远程看下有哪些版本 http://ftp.gnu.org/ ...
- 洛谷 P3258 [JLOI2014]松鼠的新家 题解
P3258 [JLOI2014]松鼠的新家 题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他 ...
- 如何在Eclipse中写Processing的sketch
有时候人们需要写更复杂的sketch,此时Processing提供的IDE就略显单薄,下面将介绍如何在eclipse中开发Processing. 一共分4步: 一.搭建环境:安装JRE.JDK.Ecl ...
- hbase错误记录部分总结
错误1:org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired ...
- DM-移除几何上的洞方法二
原视频下载地址:http://yunpan.cn/cujkVABuZXc9t 访问密码 ba61
- 【Vue.js游戏机实战】- Vue.js实现大转盘抽奖总结
大家好!先上图看看本次案例的整体效果. 实现思路: Vue component实现大转盘组件,可以嵌套到任意要使用的页面. css3 transform控制大转盘抽奖过程的动画效果. 抽奖组件内使用钩 ...