将会使用两种方式搭建SpringBoot框架,其中也整合了mybatis框架
刚开始自学的时候踩过很多奇奇怪怪的坑,本来想列出来的,但是却忘了什么问题会报什么错误,所以大家如果有什么问题就留言把,看到了就会回复,希望能帮到大家
项目搭建
首先我们来到idea页面,点击新建项目。 这种方式idea是默认使用https://start.spring.io提供的在线模板,所以需要联网。
1、搭建一个SpringBoot项目
2、开始为项目命名
3、选择我们需要的依赖,不选或者漏选也没事,后面要用到的话自己手动加就好了
4、选择项目存放的位置,然后finish
5、创建好项目之后,可以看到目录结构是这样的(这里有个错,demo.iml文件不可删除)
6、来到pom.xml文件里,可以看到需要的依赖都已经在里面了
7、这时候我们需要去配置文件增加配置
7.1 第一种配置mybatis
server:
#服务端口
port: 8080
#mybatis连接数据库的四个参数
spring:
datasource:
#用户名
username:
#密码
password:
#链接地址
url: jdbc:mysql://IP地址/数据库名称?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#配置驱动
driver-class-name: com.mysql.cj.jdbc.Driver
mybatis:
mapper-locations: classpath*:mapper/**/*.xml
type-aliases-package: com.detail.model.**.domain

7.2 第二种配置mybatis
aplication.yml
server:
#服务端口
port: 8080
#mybatis连接数据库的四个参数
spring:
datasource:
#用户名
username:
#密码
password:
#链接地址
url: jdbc:mysql://IP地址/数据库名称?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
#配置驱动
driver-class-name: com.mysql.cj.jdbc.Driver
#mybatis的配置
mybatis-plus:
mapper-locations: classpath*:mapper/*.xml #mapper映射文件位置
config-location: classpath:mybatis-config.xml #mybatis-config.xml文件所在位置

mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置全局属性-->
<settings>
<!--显示当前执行的sql语句-->
<setting name="logImpl" value="info"/>
<!--使用jdbc的getGeneratedKeys获取数据库自增主键值-->
<setting name="useGeneratedKeys" value="true"/>
<!--使用列标签替换列别名 默认未true-->
<setting name="useColumnLabel" value="true"/>
<!--开启驼峰式命名转换:Table{create_time} -> Entity{createTime}-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<typeAliases>
<!--对应实体类所在包 该包下的实体类都会注册别名,并且类名就是别名且不区分大小写-->
<package name="org.wel.entity"/>
</typeAliases>
</configuration>

到此就完成了整个配置,然我们启动一下吧