Maven整合MyBatis

MyBatis整合到Spring框架需要做的事情主要包括:

在pom文件中导入MyBatis依赖

配置文件SqlMapConfig.xml(名字可以任意取)在这个配置文件中要配好数据源,以及要扫描的mapper文件

mapper文件,也是xml,它里面的内容是操作数据库的代码

最后在测试程序中读取配置文件,根据这个配置文件生成SqlSessionFactory对象,然后由这个对象拿到mapper接口的代理对象

项目的结构:

Maven整合MyBatis_第1张图片

建表语句:

/*
 Navicat Premium Data Transfer

 Source Server         : local
 Source Server Type    : MySQL
 Source Server Version : 50726
 Source Host           : localhost:3306
 Source Schema         : mybatis

 Target Server Type    : MySQL
 Target Server Version : 50726
 File Encoding         : 65001

 Date: 19/11/2019 10:57:15
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户ID',
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '用户名',
  `sex` char(1) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '性别',
  `birthday` date NULL DEFAULT NULL COMMENT '生日',
  `height` double NULL DEFAULT NULL COMMENT '身高',
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '张三', '男', '1997-10-10', 180);
INSERT INTO `user` VALUES (2, '李四', '男', '1998-10-19', 180.5);
INSERT INTO `user` VALUES (3, '小红', '女', '2000-10-10', 165.6);

SET FOREIGN_KEY_CHECKS = 1;

SqlMapper.xml文件:





    
    
    
    
    
        
            
            
                
                
                
                
                
            
        
    
    
    
        
    

User实体类:

package com.zhuzb.cn.domain;

import lombok.Data;

import java.sql.Date;

/**
 * @project: com.zhuzb.cn.domain
 * @class: User
 * @author: zhuzb
 * @date: 2019/11/19 11:10
 * @Version 0.0.1
 * @description: 略。
 */
@Data
public class User {
    private Integer id;
    private String name;
    private String sex;
    private Date birthday;
    private Double height;
}

UserMapper接口:

package com.zhuzb.cn.mapper;

import com.zhuzb.cn.domain.User;

public interface UserMapper {
    User findById(int id);
}

MyBatisTest测试类:

public class MyBatisTest {
    public static void main(String[] args) throws Exception {
        // 读取MyBatis配置文件
        InputStream is = Resources.getResourceAsStream("SqlMapConfig.xml");
        // 创建Builder对象
        SqlSessionFactoryBuilder factoryBuilder = new SqlSessionFactoryBuilder();
        // 从配置文件中加载factory对象
        SqlSessionFactory factory = factoryBuilder.build(is);
        // 生成Session
        SqlSession session = factory.openSession();
        // 使用SqlSession创建mapper接口的代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        // 使用代理对象执行查询方法
        User user = mapper.findById(1);
        System.out.println(user);
    }
}

UserMapper.xml文件:






    

db.properties:

jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username = root
jdbc.password = root

 

你可能感兴趣的:(Maven整合MyBatis)