SpringBoot五步配置Mybatis
第一步:Maven里面添加mybatis的引用jar包:
<!--mybatis-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
</dependency>
第二步:在application.properties文件里面添加如下代码
#配置mysql数据源
spring.datasource.url=jdbc:mysql://your-mysql-url/database-name?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
spring.datasource.username=username
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver #security.basic.enabled=false mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl ## Mybatis 配置
mybatis.type-aliases-package=com.xfind.core.entity.xianyu
mybatis.mapper-locations=classpath:*.xml
#使全局的映射器启用或禁用缓存。
mybatis.configuration.cache-enabled=true
#全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。
mybatis.configuration.lazy-loading-enabled=true
#当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。
mybatis.configuration.aggressive-lazy-loading=true
#是否允许单条sql 返回多个数据集 (取决于驱动的兼容性) default:true
mybatis.configuration.multiple-result-sets-enabled=true
#是否可以使用列的别名 (取决于驱动的兼容性) default:true
mybatis.configuration.use-column-label=true
#允许JDBC 生成主键。需要驱动器支持。如果设为了true,这个设置将强制使用被生成的主键,有一些驱动器不兼容不过仍然可以执行。 default:false
mybatis.configuration.use-generated-keys=true
#指定 MyBatis 如何自动映射 数据基表的列 NONE:不隐射\u3000PARTIAL:部分 FULL:全部
mybatis.configuration.auto-mapping-behavior=partial
#这是默认的执行类型 (SIMPLE: 简单; REUSE: 执行器可能重复使用prepared statements语句;BATCH: 执行器可以重复执行语句和批量更新)
mybatis.configuration.default-executor-type=simple
#使用驼峰命名法转换字段。
mybatis.configuration.map-underscore-to-camel-case=true
#设置本地缓存范围 session:就会有数据的共享 statement:语句范围 (这样就不会有数据的共享 ) defalut:session
mybatis.configuration.local-cache-scope=session
#设置但JDBC类型为空时,某些驱动程序 要指定值,default:OTHER,插入空值时不需要指定类型
mybatis.configuration.jdbc-type-for-null=null
#如果数据为空的字段,则该字段省略不显示,可以通过添加配置文件,规定查询数据为空是则返回null。
mybatis.configuration.call-setters-on-nulls=true
第三步:设置启动类:
@SpringBootApplication
@EnableTransactionManagement//开启事务管理
@MapperScan("com.xfind.core.mybatis")//与dao层的@Mapper二选一写上即可(主要作用是扫包)
public class StartUp {
public static void main(String[] args) {
SpringApplication.run(StartUp.class, args);
}
}
第四步:添加mapper文件和编写dao代码以及service和controller代码,
1、我是在core的modules里面的resources文件夹下新建mapper文件夹,下面保存所有数据库访问的sql。
2、新建实体类,我是在entity文件夹下创建的
2、在dao层下新建mapper里面的方法
3、在service层新建调用dao层类的逻辑代码
4、在controller层新建调用service层的逻辑代码
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.xfind.core.mybatis.UserDao">
<select id="findAllUser" resultType="User">
SELECT * from users
</select>
</mapper>
User.java
package com.xfind.core.entity.xianyu; import com.fasterxml.jackson.annotation.JsonIgnore; import java.util.Date; /**
* Created by zhangwei on 2018/6/1.
*/
public class User {
private String id;
private String username;
private String phone;
private String email;
@JsonIgnore
private String password;
private String ip;
private String mac;
private int type;
private int delFlag;
private String memo;
private Date lastPasswordResetDate; private Date lastLoginDate; private int iosTest; private Date createdDt; private Date updatedDt; public String getId() {
return id;
} public void setId(String id) {
this.id = id;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPhone() {
return phone;
} public void setPhone(String phone) {
this.phone = phone;
} public String getEmail() {
return email;
} public void setEmail(String email) {
this.email = email;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public String getIp() {
return ip;
} public void setIp(String ip) {
this.ip = ip;
} public String getMac() {
return mac;
} public void setMac(String mac) {
this.mac = mac;
} public int getType() {
return type;
} public void setType(int type) {
this.type = type;
} public int getDelFlag() {
return delFlag;
} public void setDelFlag(int delFlag) {
this.delFlag = delFlag;
} public String getMemo() {
return memo;
} public void setMemo(String memo) {
this.memo = memo;
} public Date getLastPasswordResetDate() {
return lastPasswordResetDate;
} public void setLastPasswordResetDate(Date lastPasswordResetDate) {
this.lastPasswordResetDate = lastPasswordResetDate;
} public Date getLastLoginDate() {
return lastLoginDate;
} public void setLastLoginDate(Date lastLoginDate) {
this.lastLoginDate = lastLoginDate;
} public int getIosTest() {
return iosTest;
} public void setIosTest(int iosTest) {
this.iosTest = iosTest;
} public Date getCreatedDt() {
return createdDt;
} public void setCreatedDt(Date createdDt) {
this.createdDt = createdDt;
} public Date getUpdatedDt() {
return updatedDt;
} public void setUpdatedDt(Date updatedDt) {
this.updatedDt = updatedDt;
}
}
UserDao.java
@Repository
public interface UserDao {
List<User> findAllUser();
}
UserServiceImpl.java
@Service
public class XyUserServiceImpl implements XyUserService { @Autowired
UserDao userDao; @Override
public List<User> selectUsers() {
return userDao.findAllUser();
}
}
UserController.java
@RestController
@RequestMapping("/xianyu")
public class UserController { @Autowired
XyUserServiceImpl xyUserService; @GetMapping("/user")
public ResponseEntity<?> getUsers(){
List<User> users = xyUserService.selectUsers();
return ResponseEntity.ok(users);
}
}
第五步:访问试试是否已经设置成功并返回数据
SpringBoot五步配置Mybatis的更多相关文章
- SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例
SpringBoot 使用yml配置 mybatis+pagehelper+druid+freemarker实例 这是一个简单的SpringBoot整合实例 这里是项目的结构目录 首先是pom.xml ...
- 万里长征第二步——django个人博客(第五步 ——配置后台admin)
在urls.py文件中配置admin路径 from django.conf.urls import url from django.contrib import admin from blog.vie ...
- 转-spring-boot 注解配置mybatis+druid(新手上路)-http://blog.csdn.net/sinat_36203615/article/details/53759935
spring-boot 注解配置mybatis+druid(新手上路) 转载 2016年12月20日 10:17:17 标签: sprinb-boot / mybatis / druid 10475 ...
- springboot添加多数据源连接池并配置Mybatis
springboot添加多数据源连接池并配置Mybatis 转载请注明出处:https://www.cnblogs.com/funnyzpc/p/9190226.html May 12, 2018 ...
- SpringBoot(十一):springboot2.0.2下配置mybatis generator环境,并自定义字段/getter/settetr注释
Mybatis Generator是供开发者在mybatis开发时,快速构建mapper xml,mapper类,model类的一个插件工具.它相对来说对开发者是有很大的帮助的,但是它也有不足之处,比 ...
- SpringBoot入门之基于Druid配置Mybatis多数据源
上一篇了解了Druid进行配置连接池的监控和慢sql处理,这篇了解下使用基于基于Druid配置Mybatis多数据源.SpringBoot默认配置数据库连接信息时只需设置url等属性信息就可以了,Sp ...
- SpringBoot配置mybatis
一直都说SpringBoot是零配置,当然,真正实现零配置是不可能的,但是在配置mybatis这里真的是太简单了,哈哈,下面我们一起看一下. 1.先导入基于SpringBoot的mybatis依赖包 ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
- SpringBoot整合mybatis——配置mybatis驼峰命名规则自动转换
一.简述 mybatis驼峰式命名规则自动转换: 使用前提:数据库表设计按照规范“字段名中各单词使用下划线"_"划分”: 使用好处:省去mapper.xml文件中繁琐编写表字段列表 ...
随机推荐
- Real DOM和 Virtual DOM 的区别?优缺点?
一.是什么 Real DOM,真实DOM, 意思为文档对象模型,是一个结构化文本的抽象,在页面渲染出的每一个结点都是一个真实DOM结构,如下: Virtual Dom,本质上是以 JavaScript ...
- 【LeetCode】1248. 统计「优美子数组」
1248. 统计「优美子数组」 知识点:数组:前缀和: 题目描述 给你一个整数数组 nums 和一个整数 k. 如果某个 连续 子数组中恰好有 k 个奇数数字,我们就认为这个子数组是「优美子数组」. ...
- Hive——join的使用
Hive--join的使用 hive中常用的join有:inner join.left join .right join .full join.left semi join.cross join.mu ...
- etcd学习(3)-grpc使用etcd做服务发现
grpc通过etcd实现服务发现 前言 服务注册 服务发现 负载均衡 集中式LB(Proxy Model) 进程内LB(Balancing-aware Client) 独立 LB 进程(Externa ...
- 单点登录详解(token简述)(七)
前言 为什么整理单点登录? 主要的原因还是自己以前学习的时候曾经用过,但是时间太久,忘记了里面用到了哪些技术.及如何实现的,每次想到单点登录总是感觉即会又不会,这次整理session时,又涉及到了单点 ...
- 【硬核】MMU是如何完成地址翻译的
目录 1. 什么是虚拟内存? 2. 虚拟内存的作用 3. 虚拟内存与物理内存 3.1 CPU存取数据 3.2 物理地址常用术语 3.3 虚拟地址常用术语 3.4 页表常用术语 3.5 页命中/缺页 4 ...
- LintCode 550 · Top K Frequent Words II
题目描述 题目链接 思路 由于要统计每个字符串的次数,以及字典序,所以,我们需要把用户每次add的字符串封装成一个对象,这个对象中包括了这个字符串和这个字符串出现的次数. 假设我们封装的对象如下: p ...
- SQL根据两个日期生成年、月、日
1 DECLARE @beginTime DATETIME, @endTime DATETIME 2 SET @beginTime ='2019-03-01' 3 SET @endTime ='201 ...
- SpringBoot数据访问之整合Mybatis配置文件
环境搭建以及前置知识回顾 SpringBoot中有两种start的形式: 官方:spring-boot-starter-* 第三方:*-spring-boot-starter Mybatis属于第三方 ...
- MSTP
目录 一.生成树存在的问题 二.MSTP 三.MSTP的网络层次 四.MSTP的端口状态 五.MSTP的保护功能 一.生成树存在的问题 STP和RSTP的问题 PVST的问题 二.MSTP 多生成树 ...