• springboot集成mybatis


     1. 新建Springboot项目,可以参考上一章. 

    spring-boot-starter-parent 的版本选2.0.1.RELEASE

    2. 添加Mybatis的起步依赖和据库驱动坐标

    <!--mybatis起步依赖-->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    
    <!-- MySQL连接驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

     

    3. 添加数据库连接信息

    在application.properties中添加数据量的连接信息

    #DB Configuration:
    spring.datasource.driverClassName=com.mysql.jdbc.Driver
    spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8
    spring.datasource.username=root
    spring.datasource.password=123456

     

    4. 创建user表

    在test数据库中创建user表

    -- ----------------------------
    -- Table structure for `user`
    -- ----------------------------
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `username` varchar(50) DEFAULT NULL,
    `password` varchar(50) DEFAULT NULL,
    `name` varchar(50) DEFAULT NULL,
    PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;
    -- ----------------------------
    -- Records of user
    -- ----------------------------
    INSERT INTO `user` VALUES ('1', 'zhangsan', '123', '张三');
    INSERT INTO `user` VALUES ('2', 'lisi', '123', '李四');

     

    5. 创建实体Bean

    新建 com.sui.domain.User  (包括get set tostrng)

    public class User {
        private Integer id;
        private String username;
        private String password;
        private String name;
    }

     

    6. 编写Mapper

    com.sui.mapper.UserMapper

    注意:@Mapper标记该类是一个mybatis的mapper接口,可以被spring boot自动扫描到spring上下文中

    package com.sui.mapper;
    
    import com.sui.domain.User;
    import org.apache.ibatis.annotations.Mapper;
    
    import java.util.List;
    
    @Mapper
    public interface UserMapper {
        public List<User> queryUserList();
    }

     

    7. 配置Mapper映射文件

    在srcmain esourcesmapper路径下加入UserMapper.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.sui.mapper.UserMapper">
        <select id="queryUserList" resultType="user">
    select * from user
    </select>
    </mapper>

     

    8. 在application.properties中添加mybatis的信息

    #spring集成Mybatis环境
    #pojo别名扫描包
    mybatis.type-aliases-package=com.sui.domain
    #加载Mybatis映射文件
    mybatis.mapper-locations=classpath:mapper/*Mapper.xml

     

    9. 编写测试controller

    com.sui.controller.MybatisController

    package com.sui.controller;
    
    import com.sui.domain.User;
    import com.sui.mapper.UserMapper;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import java.util.List;
    
    @Controller
    public class MybatisController {
    
        @Autowired
        private UserMapper userMapper;
    
        @RequestMapping("/query")
        @ResponseBody
        public List<User> queryList(){
            List<User> users = userMapper.queryUserList();
            return users;
        }
    }

     

    10. 结果

    http://127.0.0.1:8080/query

  • 相关阅读:
    Visual Studio 快捷键
    C#编程使用Managed Wifi API连接无线SSID
    C#两种创建快捷方式的方法
    COJ 1059
    [Unity3D]Unity3D游戏开发之鼠标滚轮实现放大缩小
    cloudstack4.4新增功能前瞻
    hdu 4635 Strongly connected (tarjan)
    freemarker声明变量
    Android 4.4 Kitkat Phone工作流程浅析(六)__InCallActivity显示更新流程
    评教,路上的风景更美
  • 原文地址:https://www.cnblogs.com/jec1999/p/11632964.html
Copyright © 2020-2023  润新知