Springboot mybatis 环境搭建
作者:互联网
本文为CSDN博主「闪耀太阳」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_16804847/article/details/116227307
1.1 环境切换
1.1.1 业务需求
软件一般会在多个不同的环境中运行. 开发阶段有一个dev环境.开发完成会进行测试.则会有测试环境.最终项目部署到用户的服务中 生产环境.
问题: 如果每次切换环境,如果都需要手动的修改配置文件,则造成诸多的不便.
需求: 如果能够简化环境切换带来的影响.
1.1.2 多环境编辑
要求: 如果采用多环境测试,则要求每个环境中的数据项都应该保持一致. 否则缺失可能导致项目启动异常.
多环境配置: 关键语法"—" 环境分割
定义环境名称:
spring:
config:
activate:
on-profile: prod
- 1
- 2
- 3
- 4
默认环境名称:
#默认环境选项
spring:
profiles:
#默认环境配置名称
active: test
- 1
- 2
- 3
- 4
- 5
全部配置:
#默认环境选项
spring:
profiles:
#默认环境配置名称
active: test
#环境分割
#YML文件语法
1.YML数据结构k-v结构
2.k与v 需要使用 :“空格” 连接
3.YMl配置文件有父子级关系 所以注意缩进项的位置
spring:
config:
activate:
on-profile: prod
server:
port: 80 #配置端口
servlet: #web项目发布路径
context-path: / #/表示根目录
#定义dept属性值 YML文件默认支持UTF-8
dept:
id: 100
name: 财务部
#环境分割线
每个环境都应该有自己的名称
spring:
config:
activate:
on-profile: test
server:
port: 8080 #配置端口
servlet: #web项目发布路径
context-path: / #/表示根目录
#定义dept属性值 YML文件默认支持UTF-8
dept:
id: 100
name: 集團本部
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
1.2 热部署
1.2.1 需求说明
在开发阶段每次修改完源码都要重启服务器,程序才能生效. 能否让程序自动的完成监控,重启服务器.
1.2.2 引入jar包
<!--支持热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
- 1
- 2
- 3
- 4
- 5
1.2.3 配置IDEA环境
组合键: ctrl + shift + alt + / 或者 ctrl + alt + a
2.勾选自动编译
3).启动IDEA自动编译
2. SpringBoot整合Mybatis
2.1 导入数据库
2.1.1 安装SqlYog
1).解压安装包
2).运行sql文件
根据资料中的破解码 完成破解 输入用户名和密码 点解链接
3).正确的展现
2.1.2 导入数据库
1).新建数据库
2).导入数据
2.2 创建SpringBoot项目
2.2.1 创建项目
2.2.2 导入POM.xml文件
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jt</groupId> <artifactId>springboot_demo_2</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot_demo_2</name> <description>Demo project for Spring Boot</description>
<span class="token generics function"><span class="token punctuation"><</span>parent<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>parent<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>version<span class="token punctuation">></span></span><span class="token number">2.4</span><span class="token number">.5</span><span class="token operator"><</span><span class="token operator">/</span>version<span class="token operator">></span> <span class="token operator"><</span>relativePath<span class="token operator">/</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span> lookup parent from repository <span class="token operator">--</span><span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>parent<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>properties<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>java<span class="token punctuation">.</span>version<span class="token punctuation">></span></span><span class="token number">1.8</span><span class="token operator"><</span><span class="token operator">/</span>java<span class="token punctuation">.</span>version<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>properties<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependencies<span class="token punctuation">></span></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter 启动项 只要导入jar包 则可以完成自动的配置 暂时没有数据库的链接 <span class="token operator">--</span><span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>jdbc<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">!</span><span class="token operator">--</span>引入数据库驱动 <span class="token operator">--</span><span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>mysql<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>mysql<span class="token operator">-</span>connector<span class="token operator">-</span>java<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>scope<span class="token punctuation">></span></span>runtime<span class="token operator"><</span><span class="token operator">/</span>scope<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>web<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>mybatis<span class="token punctuation">.</span>spring<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>mybatis<span class="token operator">-</span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>version<span class="token punctuation">></span></span><span class="token number">2.1</span><span class="token number">.4</span><span class="token operator"><</span><span class="token operator">/</span>version<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>devtools<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>scope<span class="token punctuation">></span></span>runtime<span class="token operator"><</span><span class="token operator">/</span>scope<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>optional<span class="token punctuation">></span></span><span class="token boolean">true</span><span class="token operator"><</span><span class="token operator">/</span>optional<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>projectlombok<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>lombok<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>optional<span class="token punctuation">></span></span><span class="token boolean">true</span><span class="token operator"><</span><span class="token operator">/</span>optional<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>dependency<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>starter<span class="token operator">-</span>test<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>scope<span class="token punctuation">></span></span>test<span class="token operator"><</span><span class="token operator">/</span>scope<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependency<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>dependencies<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>build<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>plugins<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>plugin<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>springframework<span class="token punctuation">.</span>boot<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>spring<span class="token operator">-</span>boot<span class="token operator">-</span>maven<span class="token operator">-</span>plugin<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>configuration<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>excludes<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>exclude<span class="token punctuation">></span></span> <span class="token generics function"><span class="token punctuation"><</span>groupId<span class="token punctuation">></span></span>org<span class="token punctuation">.</span>projectlombok<span class="token operator"><</span><span class="token operator">/</span>groupId<span class="token operator">></span> <span class="token generics function"><span class="token punctuation"><</span>artifactId<span class="token punctuation">></span></span>lombok<span class="token operator"><</span><span class="token operator">/</span>artifactId<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>exclude<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>excludes<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>configuration<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>plugin<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>plugins<span class="token operator">></span> <span class="token operator"><</span><span class="token operator">/</span>build<span class="token operator">></span>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
2.2.3 Mavenjar包作用范围
1.test范围是指测试范围有效,在编译和打包时都不会使用这个依赖
2.compile范围是指编译范围内有效,在编译和打包时都会将依赖存储进去
3.provided依赖,在编译和测试过程中有效,最后生成的war包时不会加入 例如:
servlet-api,因为servlet-api tomcat服务器已经存在了,如果再打包会冲突
4.runtime在运行时候依赖,在编译时候不依赖
默认依赖范围是compile
2.2.4 数据源配置
#SpringBoot 开箱即用
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/jtadmin?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
- 1
- 2
- 3
- 4
- 5
- 6
关于参数说明:
1.serverTimezone=GMT%2B8 %2B “+” 号 新版本的驱动要求必须配置时区
2.&useUnicode=true&characterEncoding=utf8 使用Unicode编码 要求字符UTF-8编码
3.&autoReconnect=true 是否自动重连.
4.&allowMultiQueries=true 是否允许批量操作 同时执行多个sql!
2.2.5 Mybatis相关配置
#SpringBoot整合Mybatis配置
mybatis:
#定义别名包
type-aliases-package: com.jt.pojo
mapper-locations: classpath:/mybatis/mappers/*.xml
#开启驼峰映射
configuration:
map-underscore-to-camel-case: true
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2.2.6 编辑Mapper接口/映射文件
2.2.7 将Mapper接口交给容器管理
2.2.8 Mybatis测试
编辑测试类,注意包路径的写法.
@SpringBootTest public class TestSpringBoot {
<span class="token annotation punctuation">@Autowired</span> <span class="token keyword">private</span> DemoUserMapper userMapper<span class="token punctuation">;</span> <span class="token annotation punctuation">@Test</span> <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testFindAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">{<!-- --></span> System<span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span>userMapper<span class="token punctuation">.</span><span class="token function">findAll</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span>
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
2.3 关于绑定异常
2.3.1 报错说明
绑定异常: mapper的接口与xml的映射文件之间绑定异常.
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): com.jt.mapper.DemoUserMapper.findAll
- 1
代码排查:
1).检查Mapper接口的路径
2).检查mapper.xml引入过程
3).检查mapper.xml映射文件
4).检查方法名称
检查mapper接口方法名称,与标签的ID是否一致.
5). 清空/重新编译项目
1).clean项目
2).build项目
6).将我的代码全部复制 测试一下. 只粘贴pom.xml 和src文件.
7).检查目录层级,是否使用/进行分割.
2.3.2 关于数据库密码问题
默认的数据库密码 root/root
创建数据库密码时 以数字0开头 012345
问题解释: 数据密码在进行编译时. 如果遇到首字母为0则自动删除.
解决方案: 如果以特殊数字开头 则 “012345” 引号包裹
2.4 关于Mapper接口代理对象创建流程
说明: 当springBoot启动时,加载pom.xml文件.实例化对象的流程图.
2.5 Mybatis入库练习
2.5.1 编辑Mapper接口
2.5.2 编辑Mapper.xml 文件
<!--
完成用户入库操作 id="与方法名称保持一致"
sql结束时不需要添加;号 在Mysql数据库中执行;号没有问题,
但是如果在Oracle中执行则必然报错.
-->
<insert id="insertUser">
insert into demo_user(id,name,age,sex)
values(null,#{name},#{age},#{sex})
</insert>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
2.5.3 业务调用
//新增用户
@Test
public void testInsert(){
DemoUser user = new DemoUser();
user.setId(null).setName("mybatis信息").setAge(18).setSex("男");
userMapper.insertUser(user);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
2.6 Mybatis更新练习
2.6.1 Mapper接口
2.6.2 编辑Mapper.xml映射文件
在mapper.xml文件中 实现更新操作
2.6.3 编辑测试类
标签:gt,Springboot,spring,boot,lt,mybatis,artifactId,groupId,搭建 来源: https://blog.csdn.net/yanguicehn/article/details/116257404