搭建springboot+mybatis环境
上次没学完,就没继续写_(:з」∠)_
话不多说,进入正题
修改并写入配置文件
首先将application.properties文件改为更加简洁的.yml文件。
然后写入配置文件
server:
port: 8080
spring:
datasource:
#数据库链接
url: jdbc:mysql://127.0.0.1:3306/practice
username: 你的数据库名字
password: 你的数据库密码
#数据库驱动
driver-class-name: com.mysql.cj.jdbc.Driver
创建目录
我们在com.example.项目名文件夹下创建五个包,分别为:
base:存放数据库的一些手动实现的接口
controller:存放控制器
mapper:存放数据库的一些基础接口
pojo:存放基础类
service:存放业务逻辑
测试
我们在controller包下创一个java类,并尝试输出hello springboot
首先我们在test类上增加两个注解
@Controller:表明这个类是个控制器
@RequestMapping("/test"):将这个类映射为test
然后我们新创一个方法并增加两个注解
@RequestMapping("/hello")
@ResponseBody
public String say(){
return "hello springboot";
}
@ResponseBody:使此方法不返回html页面,而是返回一个字符串
然后我们运行项目,打开浏览器,在地址栏输入http://localhost:8080/test/hello,来看一下效果。
如果出现了这个效果,说明之前搭建的就成功了。
整合Mybatis
首先我们在application.yml文件中添加Mybatis配置
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.example.register_practice.pojo
部署mybatis generator
首先我们在pom.xml文件中添加Mybatis generator自动生成代码插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.1</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
然后在右下角弹出的弹窗中点击Import Changes下载相关文件
然后我们在resource文件下创建generator文件夹,并在其下创建generatorConfig.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="C:\Program Files (x86)\MySQL\Connector.J 5.1\mysql-connector-java-5.1.36-bin.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="false"/>
</commentGenerator>
<!--数据库连接驱动类,URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/test" userId="root" password="123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成(实体)模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.springboottest1.entity" targetProject="src">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成XML映射文件的包名和位置-->
<sqlMapGenerator targetPackage="resources.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO接口的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.springboottest1.mapper" targetProject="src">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="user" domainObjectName="user" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
当然这里只是一个原始版本,我们需要修改其中的一些部分,慢慢填坑
①链接标红
如图添加url,应用后确定就行。
②选择数据库驱动包
首先我们打开项目结构(图中的右上角箭头),选中Libraries标签,找到mysql驱动包,复制其路径就好。
③修改数据库相关信息
将这里的信息改为自己的就好。
当然由于时区问题,我们要在url后面加上*?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8*,其中&需改为& a m p ;(删掉空格)。
然后在其下面插入一行代码:
<property name="nullCatalogMeansCurrent" value="true"/>
④修改路径
修改框出的两个路径为resources.pojo/mapper。
⑤修改数据库表名
实体类首字母记得大写。
进行Mybatis generator运行配置
打开此目录,然后创建一个maven,命名为generator,然后填写运行命令
运行generator
显示BUILD SUCCUSS即为成功。
然后我们将生成的三个文件放到对应的目录。
之后在启动类加入一条新注解
@MapperScan(“com.example.registry_practice.mapper”)
由于我们刚才修改了生成的文件的位置,所以我们需要去修改相应的路径。
打开UserMapper.java,修改相应路径。
同样修改UserMapper.xml中的路径。