山东大学暑期项目实训——云主机服务比价与预测系统

山东大学暑期项目实训——云主机服务比价与预测系统(七)

学习Spring Boot整合Mybatis

1、在springboot项目中的pom.xml中添加mybatis的依赖

<dependency>
  <groupId>org.mybatis.spring.boot</groupId>
  <artifactId>mybatis-spring-boot-starter</artifactId>
  <version>1.1.1</version>
</dependency>

2、在src/main/resources/application.yml中配置数据源信息

# DB Configation
spring:
 datasource:
  driver-class-name: com.mysql.jdbc.Driver
  url: jdbc:mysql://127.0.0.1:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT
  username: root
  password: root

# JAPConfigration
 jpa:
  database: mysql
  show-sql: true
  generate-ddl: true

在连接数据库的过程中可能会出现SQLException,可能是时区问题导致的,加上url路径后面的“serverTimezone = GMT”即可

3、在主启动类的同级创建po包和mapper包,在po包中创建实体类,编写pojo;在mapper包中创建mapper接口和mapper映射文件
mapper.java

package com.hxy.springbootdemo1.demo.mapper;

import com.hxy.springbootdemo1.demo.pojo.MUser;

import java.util.List;

public interface UserMapper {
  List<MUser> getUserList();
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.hxy.springbootdemo1.demo.mapper.UserMapper">
  <select id="getUserList" resultType="com.hxy.springbootdemo1.demo.pojo.MUser">
    select * from user
  </select>
</mapper>

4、手动配置mybatis的包扫描

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

//7月14日

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值