spring boot整合mybatisspring boot 学习笔记(第二天)

1.spring boot整合mybatis

1.在pom文件中添加mybatis依赖(如果在创建项目时已经勾选了mybatis选项,就不用添加mybatis-spring-boot-starter了)

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.2</version>

</dependency>

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.13</version>

</dependency>

2.在application.properties中添加mybatis配置项

#mybatis start

spring.datasource.url=jdbc:mysql://127.0.0.1:3306/peopleinfo?useUnicode=true&charsetEncoding=utf8

spring.datasource.name=root

spring.datasource.password=root

spring.datasource.driver-class-name=com.mysql.jdbc.Driver

#mybatis end

 

2.使用generator自动生成pojo类和数据库mapping

a. 自己创建数据库和数据表

b.在resources目录下创建genertorConfig.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">

<!-- 第二种mybatis逆向生成xml配置 -->

<generatorConfiguration>

<!-- 需要指明数据库连接器的绝对路径 -->

<classPathEntry

location="D:\AddFile\idea\ideaWork\TestDemo\lib\mysql-connector-java-5.1.39-bin.jar"/>

<context id="sqlserverTables" targetRuntime="MyBatis3">

<!-- 生成的pojo,将implements Serializable-->

<!-- <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>-->

<commentGenerator>

<!-- <property name="suppressDate" value="true"/>-->

<!-- 是否去除自动生成的注释 true:是 : false:否 -->

<property name="suppressAllComments" value="true" />

</commentGenerator>

<!-- 数据库链接URL、用户名、密码 -->

<jdbcConnection driverClass="com.mysql.jdbc.Driver"

connectionURL="jdbc:mysql://127.0.0.1:3306/peopleInfo"

userId="root"

password="root">

</jdbcConnection> <!--

默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer

true,把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal

-->

<javaTypeResolver>

<property name="forceBigDecimals" value="false" />

</javaTypeResolver>

<!--

生成model模型,对应的包路径,以及文件存放路径(targetProject),targetProject可以指定具体的路径,如./src/main/java,

也可以使用“MAVEN”来自动生成,这样生成的代码会在target/generatord-source目录下

--> <!--<javaModelGenerator targetPackage="com.forezp.entity" targetProject="MAVEN">-->

<javaModelGenerator targetPackage="com.htc.demo.entity" targetProject="src/main/java">

<property name="enableSubPackages" value="true"/>

<!-- 从数据库返回的值被清理前后的空格 -->

<property name="trimStrings" value="true" />

</javaModelGenerator>

<!--对应的mapper.xml文件 -->

<sqlMapGenerator targetPackage="mybatis" targetProject="src/main/resources">

<property name="enableSubPackages" value="true"/>

</sqlMapGenerator>

<!-- 对应的Mapper接口类文件 -->

<javaClientGenerator type="XMLMAPPER" targetPackage="com.htc.demo.mapper" targetProject="src/main/java">

<property name="enableSubPackages" value="true"/>

</javaClientGenerator>

<!-- 列出要生成代码的所有表,这里配置的是不生成Example文件 -->

<table tableName="uinfo"

domainObjectName="UserInfo"

enableCountByExample="false"

enableUpdateByExample="false"

enableDeleteByExample="false"

enableSelectByExample="false"

selectByExampleQueryId="false" >

<property name="useActualColumnNames" value="false"/>

</table>

<table tableName="password"

domainObjectName="Password"

enableCountByExample="false"

enableUpdateByExample="false"

enableDeleteByExample="false"

enableSelectByExample="false"

selectByExampleQueryId="false" >

<property name="useActualColumnNames" value="false"/>

</table>

</context>

</generatorConfiguration>

c.在pom文件中添加generator插件

<!--mybatis自动生成代码插件-->

<plugin>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<version>1.3.5</version>

<dependencies>

<dependency>

<groupId> mysql</groupId>

<artifactId> mysql-connector-java</artifactId>

<version> 5.1.39</version>

</dependency>

<dependency>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-core</artifactId>

<version>1.3.5</version>

</dependency>

</dependencies>

<executions>

<execution>

<id>Generate MyBatis Artifacts</id>

<phase>package</phase>

<goals>

<goal>generate</goal>

</goals>

</execution>

</executions>

<configuration>

<!--允许移动生成的文件 -->

<verbose>true</verbose>

<!-- 是否覆盖 -->

<overwrite>true</overwrite>

<!-- 自动生成的配置 -->

<configurationFile>src\main\resources\generator\genertorConfig.xml</configurationFile>

</configuration>

</plugin>

注意:自动生成的配置是刚刚写的generatorConfig.xml

点击mavenProject 选择到mybatis-generator双击执行。如果没错的话就会在相应的路径上添加文件了。

-------------------------------------------雷区警戒线--------------------------------------------

坑1:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver一直飘红报错

填坑:

注释掉pom文件中mysql-connector-java scope为 runtime的依赖,它与你添加的那个依赖有jar包冲突。


<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

坑2:

The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.

填坑:

时区错误,真的是大千世界无奇不有。。。。。。

是问题还是得解决的,本着对未知世界的敬畏,网上什么改mysql的时区就算了,直接来一个简单的,在你的地址后边添加这么一句:?serverTimezone=GMT%2B8 问题就解决了。

就像这样:

jdbc:mysql://127.0.0.1:3306/peopleInfo?serverTimezone=GMT%2B8&useUnicode=true&charsetEncoding=utf8

 

坑3:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mainController': Unsatisfied dependency expressed through field 'service'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'serviceImp': Unsatisfied dependency expressed through field 'passwordMapper'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'passwordMapper' defined in file [D:\AddFile\idea\ideaWork\TestDemo\target\classes\com\htc\demo\mapper\PasswordMapper.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Property 'sqlSessionFactory' or 'sqlSessionTemplate' are required

 

填坑:

此坑出处颇多,由于已经解决了许久,忘了,所以只找出这么一个原因:

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

在建立项目时,系统的启动项上,会默认添加这个,但是有时候可能会出点小乱子,把括号里的东西全删了,也许就好了。(原因大概是它会自动使用项目自带的数据源配置,再进去我也说不清了)

 

坑4:

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading of the driver class is generally unnecessary.

填坑:

其实这个算不上坑,因为前边把启动项的东西删掉了,所以这个驱动是不完整的,根据提示使用“com.mysql.cj.jdbc.Driver”就好了,有些人可能找不到这个驱动,应该是缺少这个jar包。(jar包版本要高点,我另一个项目的jar是5.1的,似乎没得这个驱动)

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<version>8.0.13</version>

</dependency>

坑5:

大概意思就是找不到mapper,然后报一大堆错。

填坑:

1.看看是不是配置了映射路径:

mybatis.mapper-locations=classpath:mybatis/*.xml

mybatis.type-aliases-package=com.htc.demo.mapper

2.看看启动项是不是有配置(其实可以在调用mapper的地方写这个,但是启动项就不用每一个都去配置了)

@MapperScan(value = "com.htc.demo.mapper")

3.mapper是不是添加了@component。

--------------------------------------------------------------我不是系统的生产者,我只是bug的搬运工------------------------

上篇文章:springboot学习笔记(第一天)spring boot的搭建

下一篇文章: SpringBoot 学习笔记(第三天)SpringBoot 整合dubbo之zookeeper的搭建

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值