Mybatis踩过的坑

Mybatis踩过的坑

一、Mybatis配置:
方法一:使用xml文件式config文件进行配置

1.我使用看起来比较标准的,先在 application.properties中将数据库的url/user/password/driver一系列设置好,再创建一个新的mybatis-config文件对mybatis初始化正式进行配置。

2.这里的mybaitis-config文件要放在resource文件夹下比较好,因为IDEA编译器不会编译src目录下的xml文件,所以在全局配置文件中加上以下配置语句就能编译到:

<build>
<resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
        </resource>
    </resources>
</build>

3.加上以上语句之后xml文件依然扫描不到,但在target的classes文件夹中能看到编译器编译了*.xml文件,

在mybatis-config配置文件中resource的问题

这是因为多级目录的问题,路径应使用com/demo/mapper/UserMapper.xml而不是com.demo.mapper.UserMapper.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>
    <!-- 配置文件信息 -->
    <properties resource="application.properties"></properties>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <!-- 从配置文件中加载属性 -->
                <property name="driver" value="${db.driver}"/>
                <property name="url" value="${db.url}"/>
                <property name="username" value="${db.user}"/>
                <property name="password" value="${db.password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <!-- 加载前面编写的SQL语句的文件 -->
        <mapper resource="com/learn/demo/mapper/StudentMapper.xml"/>
    </mappers>
</configuration>
方法二:在application.yml全局配置文件中进行配置
server:
  port: 6666
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bloginfo?useUnicode=true&amp;characterEncoding=utf-8&amp&useSSL=false
    username: root
    password: *******

mybatis:
  mapper-locations: classpath:/mapper/*.xml
  typeAliasesPackage: com.learn.demo.POJO
二、spring mvc

controller、dao、entity、service

  • controller层负责具体的业务模块流程的控制
  • entity层用于存放我们的实体类,与数据库中的属性值基本保持一致,实现set和get的方法
  • dao层主要是做数据持久层的工作,负责与数据库联络,封装了增删改查基本操作
  • service层主要负责业务模块的逻辑应用设计,具体要调用到已定义的DAO层的接口
  • 在 resource 目录下新建 mapper 目
  • 录。这个 mapper 目录是用来存放 SQL 语句的地方。

dao层加@Repository注解

service层加@Service(“xxxxService”)

controller层加@Controller或@RestController注解

报错:

有一大段报错找不到原因

将找错重点放在报错之前IDEA标出的红色报错

Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdb

查阅百度找到解决方法:

之前在yml中数据库的driver为:

spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/bloginfo?useUnicode=true&amp;characterEncoding=utf-8&amp&useSSL=false
    username: root
    password: *******

其中驱动为:driver-class-name: com.mysql.jdbc.Driver

改为:driver-class-name: com.mysql.cj.jdbc.Driver

然后报错:

Cannot load connection class because of underlying exception: com.mysql.cj.exceptions.WrongArgumentException: Malforme

继续查阅将datasource中的URL改变

url: jdbc:mysql://localhost:3306/bloginfo?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT

成功

这句话是排除spring的自动配置不要乱用

@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})

然后又发生这样的错误:

Description:

Multiple Spring Session store implementations are available on the classpath:
    - org.springframework.session.jdbc.JdbcIndexedSessionRepository
    - org.springframework.session.data.redis.RedisIndexedSessionRepository
Action:

Consider any of the following:
    - Define the spring.session.store-type property to the store you want to use
    - Review your classpath and remove the unwanted store implementation(s)`

这是因为pom.xml全局配置文件中之前根据别人的教程加了关于session的相关依赖

<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-jdbc</artifactId>
</dependency>

后面应该可以用Spring boot 中自带的事务进行操作,将相关依赖删除就能通过编译

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值