美文网首页
SpringBoot整合MyBatis

SpringBoot整合MyBatis

作者: 凌康ACG | 来源:发表于2019-08-20 13:42 被阅读0次

SpringBoot整合MyBatis

2019年8月20日 13:25:27

创建一个demo项目:

image.png

导入maven依赖:

maven依赖.png
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
        <!--数据库连接用-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>

项目如下

image.png

application配置文件如下:

#mysql
spring.datasource.url=jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
#spring.datasource.driverClassName = com.mysql.jdbc.Driver

#配置扫描xml路径
mybatis.mapper-locations=classpath:/mapper/*.xml

数据库名为demo,表car如下

DROP TABLE IF EXISTS `car`;
CREATE TABLE `car` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) DEFAULT NULL,
  `price` decimal(10,2) DEFAULT NULL,
  `createed` datetime DEFAULT NULL,
  `updateed` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=30 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of car
-- ----------------------------
INSERT INTO `car` VALUES ('26', '奔驰', '420000.00', '2019-06-23 20:08:11', null);
INSERT INTO `car` VALUES ('27', '五菱宏光xc288.cn', '90000.00', '2019-06-23 20:08:11', '2019-06-23 20:53:53');
INSERT INTO `car` VALUES ('28', '宝马', '666666.00', '2019-08-03 13:31:36', null);
INSERT INTO `car` VALUES ('29', '路虎', '555555.00', '2019-08-03 13:32:04', null);

持久层dao

package com.example.mybatisdemo.dao;

import com.example.mybatisdemo.entity.Car;
import java.util.List;

public interface CarMapper {
    List<Car> getAll();
}

dao对应的xml

在resources文件夹下创建mapper/CarMapper.xml文件,用来执行SQL

<?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">
<!--这里指向dao-->
<mapper namespace="com.example.mybatisdemo.dao.CarMapper">

    <!--执行查询   把结果用实体返回去 更多类型自行查阅文档-->
    <select id="getAll" resultType="com.example.mybatisdemo.entity.Car">
        select id,name,price,createed,updateed from car
    </select>
</mapper>

实体类entity

public class Car {
    private int id;
    private String name;
    private double price;
    private Date createed;
    private Date updateed;
    //Get和Set方法

控制层controller

package com.example.mybatisdemo.controller;

import com.example.mybatisdemo.dao.CarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {

    @Autowired
    private CarMapper carMapper;

    @GetMapping("")
    @ResponseBody
    private Object index() {
        return carMapper.getAll();
    }
}

启动入口需要加扫描的dao包

@SpringBootApplication
//扫描dao包
@MapperScan(basePackages = {"com.example.mybatisdemo.dao"})
public class MybatisDemoApplication {

运行结果如下:http://localhost:8080/

image.png

项目代码:https://gitee.com/lingkang_top/mybatis-demo

SpringBoot整合Hibernate https://www.jianshu.com/p/0d282d66fc4b

相关文章

网友评论

      本文标题:SpringBoot整合MyBatis

      本文链接:https://www.haomeiwen.com/subject/ptfpsctx.html