springboot整合Mybatis(无xml)

1、pom文件 依赖引入
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.8.RELEASE</version>
<relativePath />
</parent> <dependencies>
<!-- SpringBoot 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <!-- mybatis 支持 SpringBoot -->
<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>
<version>5.1.38</version>
</dependency> <!-- SpringBoot web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、 application.yml 新增配置
spring:
datasource:
url: jdbc:mysql://localhost:3306/yys_springboot_mybatis?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
3、UserEntity.java
/**
* 用户管理
* Entity
* @author yys
*/ import com.fasterxml.jackson.annotation.JsonFormat; import java.io.Serializable;
import java.sql.Date; public class UserEntity implements Serializable { private Long id; private String name; private Integer age; private Byte status; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
private Date createTime; @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8")
private Date updateTime; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
} public Byte getStatus() {
return status;
} public void setStatus(Byte status) {
this.status = status;
} public Date getCreateTime() {
return createTime;
} public void setCreateTime(Date createTime) {
this.createTime = createTime;
} public Date getUpdateTime() {
return updateTime;
} public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
} }
4、UserController.java
/**
* 用户管理
* Controller
* @author yys
*/
@RestController
@RequestMapping("/user")
public class UserController { @Autowired
private UserService userService; @RequestMapping("/add")
public String addUser(String userName, Integer age) {
return userService.addUser(userName, age) ? "success" : "fail";
} @RequestMapping("/get")
public UserEntity getUserByName(String userName) {
return userService.getUserByName(userName);
} }
5、UserService.java
/**
* 用户管理
* Service
* @author yys
*/
@Service
public class UserService { @Autowired
private UserMapper userMapper; public boolean addUser(String userName, Integer age) {
return userMapper.insert(userName, age) > 0 ? true : false;
} public UserEntity getUserByName(String userName) {
return userMapper.findByName(userName);
} }
6、UserMapper.java
/**
* 用户管理
* Mapper
* @author yys
*/
public interface UserMapper { @Select("SELECT id, user_name AS name, age, status, create_time AS createTime, update_time AS updateTime FROM yys_user WHERE user_name = #{name}")
UserEntity findByName(@Param("name") String name); @Insert("INSERT INTO yys_user VALUES (NULL, #{name}, #{age}, 1, NOW(), NOW())")
int insert(@Param("name") String name, @Param("age") Integer age); }
7、启动类
@SpringBootApplication
@MapperScan("com.yys.mapper")
public class YysApp { public static void main(String[] args) {
SpringApplication.run(YysApp.class, args);
} }
8、初始化sql文件
CREATE TABLE `yys_user` (
`id` bigint(11) NOT NULL AUTO_INCREMENT COMMENT 'ID,自增列',
`user_name` varchar(32) NOT NULL COMMENT '用户名',
`age` int(11) NOT NULL COMMENT '用户年龄',
`status` tinyint(2) NOT NULL DEFAULT '' COMMENT '状态:-1-删除;1-正常;',
`create_time` datetime NOT NULL COMMENT '创建时间',
`update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
9、测试
http://localhost:8080/user/add?userName=yys&age=18
a、页面结果 - 如下图所示 :

_b、数据库结果 - 如下图所示 :_

http://localhost:8080/user/get?userName=yys
a、页面结果 - 如下图所示 :


springboot整合Mybatis(无xml)的更多相关文章
- SpringBoot整合Mybatis之xml
SpringBoot整合Mybatis mybatis ORM框架.几个重要的概念: Mapper配置 : 可以使用基于XML的Mapper配置文件来实现,也可以使用基于Java注解的Mybatis注 ...
- SpringBoot 整合 Mybatis + Mysql——XML配置方式
一.介绍 SpringBoot有两种方法与数据库建立连接,一种是集成Mybatis,另一种用JdbcTemplate,本文主要讨论集成Mybatis方式. SpringBoot整合Mybatis也有两 ...
- SpringBoot整合MyBatis之xml配置
现在业界比较流行的数据操作层框架 MyBatis,下面就讲解下 Springboot 如何整合 MyBatis,这里使用的是xml配置SQL而不是用注解.主要是 SQL 和业务代码应该隔离,方便和 D ...
- SpringBoot系列-整合Mybatis(XML配置方式)
目录 一.什么是 MyBatis? 二.整合方式 三.实战 四.测试 本文介绍下SpringBoot整合Mybatis(XML配置方式)的过程. 一.什么是 MyBatis? MyBatis 是一款优 ...
- SpringBoot从入门到精通二(SpringBoot整合myBatis的两种方式)
前言 通过上一章的学习,我们已经对SpringBoot有简单的入门,接下来我们深入学习一下SpringBoot,我们知道任何一个网站的数据大多数都是动态的,也就是说数据是从数据库提取出来的,而非静态数 ...
- springboot整合mybatis时无法读取xml文件解决方法(必读)
转 http://baijiahao.baidu.com/s?id=1588136004120071836&wfr=spider&for=pc 在springboot整合myba ...
- SpringBoot整合MyBatis,HiKari、Druid连接池的使用
SpringBoot整合MyBatis 1.创建项目时勾选mybatis.数据库驱动. mysql驱动默认是8.x的版本,如果要使用5.x的版本,创建后到pom.xml中改. 也可以手动添加依赖 ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
随机推荐
- 循序渐进VUE+Element 前端应用开发(1)--- 开发环境的准备工作
之前一直采用VS进行各种前端后端的开发,随着项目的需要,正逐步融合纯前端的开发模式,开始主要选型为Vue + Element 进行BS前端的开发,后续会进一步整合Vue + AntDesign的界面套 ...
- vue 使用cli脚手架手动创建项目 相关的选择配置及真正项目的开始
转载https://www.jianshu.com/p/635bd3ab7383 根据上述连接将基本的环境和命令和装好 使用命令行 vue create 项目名称 出现选项 选择手动(没有截图展 ...
- TP5.0登录验证码实现
<div class="loginbox-textbox"> <input class="form-control" placeholder= ...
- linux 去除^M 换行符
一般,在windows下写的shell脚本,都会去linux执行,都会有^M 符号,那么怎么去除呢? 第一种方法:cat -A filename 就可以看到windows下的断元字符 ^M要去除他,最 ...
- 设计一个多功能的MyTime类 代码参考
#include <iostream> #include <cstdio> using namespace std; class MyTime { private: int h ...
- List<T> 的扩展方法
//List<T>.Take(m) //取出 前m行 IEnumerable<Person> takeList = lstPerson.Take(4); foreac ...
- Spring Cloud 系列之 Apollo 配置中心(一)
背景 随着程序功能的日益复杂,程序的配置日益增多:各种功能的开关.参数的配置.服务器的地址等等. 对程序配置的期望值也越来越高:配置修改后实时生效,灰度发布,分环境.分集群管理配置,完善的权限.审核机 ...
- OAuth + Security -1 - 认证服务器配置
配置 基础包依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...
- Rocket - tilelink - HintHandler
https://mp.weixin.qq.com/s/MHW_aBSL72YNee9bVWWeaw 简单介绍HintHandler的实现. 1. 基本功能 实现Hint请求的处理 ...
- jchdl - GSL Node
https://mp.weixin.qq.com/s/Oa4qgjIUccu5Y-Jlqcyn_A org.jchdl.model.gsl.core.meta.Node.java gen ...