tk.mybatis 简单的使用方法

一 pom.xml 导入依赖 (这里低版本会报错)


            tk.mybatis
            mapper-spring-boot-starter
            2.1.5

二 书写PersonZhuJie类

package com.example.demo.bean;


import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Table(name = "per")
public class PersonZhuJie {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer id;

    @NotNull(message = "用户名不能为空")
    private String name;
    @NotNull(message = "密码不能为空")
    private String password;

    @Column(name = "age")
    private int age;
    @Column(name = "sex")
    private String sex;
    @Column(name = "address")
    public String address;

    public Integer getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }


}

三 mapper 包书写PersonZhuJieMapper接口

package com.example.demo.mapper;

import com.example.demo.bean.PersonZhuJie;
import tk.mybatis.mapper.common.Mapper;

public interface PersonZhuJieMapper extends Mapper {
}

四 service包书写接口和工具类

目录结构如下
tk.mybatis 简单的使用方法_第1张图片
BaseService 书写所有的接口在里面

package com.example.demo.service;

import java.util.List;

public interface BaseService {
    List selectAll(T record);
    List selectAll();
}

PersonZhujieService 继承BaseService ,有利于接口的管理

package com.example.demo.service;

import com.example.demo.bean.PersonZhuJie;

import java.util.List;

public interface PersonZhujieService extends BaseService{

}

BaseImpl实现BaseService接口里面的方法,这里可以调用tk.mybatis里封装的sql语句。

package com.example.demo.service.impl;

import com.example.demo.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;    //这里注意不要导错包

import java.util.List;

public abstract class BaseImpl implements BaseService {
    @Autowired
    protected Mapper mapper;

    public Mapper getMapper() {
        return mapper;
    }

    @Override
    public List selectAll(T record) {
        return mapper.select(record);
    }

    @Override
    public List selectAll() {
        return mapper.selectAll();
    }
}

PersonZhujieImpl 继承BaseImpl,有利于管理接口

package com.example.demo.service.impl;

import com.example.demo.bean.PersonZhuJie;
import com.example.demo.service.PersonZhujieService;
import org.springframework.stereotype.Service;

@Service
public  class PersonZhujieImpl extends BaseImpl implements PersonZhujieService{
}

五 resource 包下的目录

tk.mybatis 简单的使用方法_第2张图片
personzhujie.xml





    
        
        
        
        
        
        
    

application.properties

server.port=6785
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/booksystem?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
server.tomcat.uri-encoding=UTF-8
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource

#指定全局配置文件
mybatis.config= classpath:mybatis/mybatis-config
#指定sql文件
mybatis.mapperLocations=classpath:mybatis/mapper/*.xml
mybatis.typeAliasesPackage=com.lgp.SpringBoot.bean

最后一步就是程序入口,这里很重要

package com.example.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
//@MapperScan(value = "com.example.demo.mapper")
@tk.mybatis.spring.annotation.MapperScan("com.example.demo.mapper")   //指向你的mapper路径
@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

}

sql语句

/*
 Navicat Premium Data Transfer

 Source Server         : root
 Source Server Type    : MySQL
 Source Server Version : 50559
 Source Host           : localhost:3306
 Source Schema         : booksystem

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

 Date: 25/07/2019 18:07:40
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for per
-- ----------------------------
DROP TABLE IF EXISTS `per`;
CREATE TABLE `per`  (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  `age` varchar(11) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT '0',
  `address` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `sex` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  `password` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 16 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of per
-- ----------------------------
INSERT INTO `per` VALUES (1, 'Ewan McGregor', '34', 'AM', '', '123');
INSERT INTO `per` VALUES (2, 'Saoirse Ronan', '18', 'AM', '', '123');
INSERT INTO `per` VALUES (3, 'Quentin Tarantino', '39', 'AM', '', '234');
INSERT INTO `per` VALUES (4, 'muyuan', '0', NULL, NULL, '456');

SET FOREIGN_KEY_CHECKS = 1;

六 PersonController

package com.example.demo.controller;

import com.example.demo.bean.Person;
import com.example.demo.bean.PersonZhuJie;
import com.example.demo.mapper.PersonMapper;
import com.example.demo.service.PersonZhujieService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.security.sasl.SaslServer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class PersonController {
    

    @Autowired
    PersonZhujieService personZhujieService;
   

    @GetMapping("/findAll")
    public Map findAll(){

        Map map = new HashMap();
        map.put("people",personZhujieService.selectAll());
        return map;
    }

}

结果

tk.mybatis 简单的使用方法_第3张图片

你可能感兴趣的:(后端)