mybatis-ssm整合


目录
  • 环境
  • 测试

环境

/*
 Navicat Premium Data Transfer

 Source Server         : localhost
 Source Server Type    : MySQL
 Source Server Version : 80022
 Source Host           : localhost:3306
 Source Schema         : school

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

 Date: 20/11/2021 17:20:00
*/

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for teacher
-- ----------------------------
DROP TABLE IF EXISTS `teacher`;
CREATE TABLE `teacher`  (
  `id` int(0) NOT NULL AUTO_INCREMENT,
  `tea_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  `tea_age` int(0) NULL DEFAULT NULL,
  `tea_email` varchar(255) CHARACTER SET utf8 COLLATE utf8_bin NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 65 CHARACTER SET = utf8 COLLATE = utf8_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of teacher
-- ----------------------------
INSERT INTO `teacher` VALUES (1, 'set', 22, 'tom@qq.com');
INSERT INTO `teacher` VALUES (2, 'lili', 22, 'lili@qq.com');
INSERT INTO `teacher` VALUES (3, 'lucy', 22, 'lucy@qq.com');
INSERT INTO `teacher` VALUES (4, 'lisi', 22, 'lisi@qq.com');
INSERT INTO `teacher` VALUES (62, 'teacher1', 22, '1@qq.com');
INSERT INTO `teacher` VALUES (63, 'teacher2', 22, '2@qq.com');
INSERT INTO `teacher` VALUES (64, 'teacher3', 22, '3@qq.com');

SET FOREIGN_KEY_CHECKS = 1;

db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/school?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
db.username=root
db.password=123456
#dbconfig.properties
# Global logging configuration
log4j.rootLogger=debug, stdout
# MyBatis logging configuration...
log4j.logger.org.mybatis.example.BlogMapper=TRACE
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
#log4j.properties
<?xml version="1.0" encoding="UTF-8" ?>


	
	
		
	
	
	
		
		
		
	
	

<?xml version="1.0" encoding="UTF-8"?>


    
    
    
    
    
        
        
        
        
    

    
        
        
        
            
                classpath:mapper/*.xml
            
        
    

    
    
        
    

    
    
        
    

    
        
            
            
            
            
            
        
    

    
        
        
    

    



<?xml version="1.0" encoding="UTF-8"?>


    
    
    
    

    
        
        
        
        
    

    
        
            
            
                
                    
                        application/json;charset=UTF-8
                    
                
            
            
            
                
                    
                        text/html;charset=UTF-8
                        application/json;charset=UTF-8
                    
                
                
            
        
    
    

    
    
        
        
    




<?xml version="1.0" encoding="UTF-8"?>


    
    
        contextConfigLocation
        classpath:applicationContext.xml
    

    
    
        org.springframework.web.context.ContextLoaderListener
    

    
    
    
        spring
        org.springframework.web.servlet.DispatcherServlet
        1
    

    
    
        spring
        /
    


package com.fly.entity;

import java.io.Serializable;

/**
 * @author 26414
 */
public class Teacher implements Serializable {

  private Integer id;
  private String teaName;
  private Integer teaAge;
  private String teaEmail;

  @Override
  public String toString() {
    return "Teacher{" +
            "id=" + id +
            ", teaName='" + teaName + '\'' +
            ", teaAge=" + teaAge +
            ", teaEmail='" + teaEmail + '\'' +
            '}';
  }

  public Integer getId() {
    return id;
  }

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

  public String getTeaName() {
    return teaName;
  }

  public void setTeaName(String teaName) {
    this.teaName = teaName;
  }

  public Integer getTeaAge() {
    return teaAge;
  }

  public void setTeaAge(Integer teaAge) {
    this.teaAge = teaAge;
  }

  public String getTeaEmail() {
    return teaEmail;
  }

  public void setTeaEmail(String teaEmail) {
    this.teaEmail = teaEmail;
  }
}

package com.fly.mapper;

import com.fly.entity.Teacher;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author 26414
 */
@Mapper
public interface TeacherMapper {

  /**
   * 根据id查询老师
   * @param id id
   * @return 老师
   */
  Teacher getTeacherById(Integer id);

}

<?xml version="1.0" encoding="UTF-8" ?>



    

package com.fly.service;

import com.fly.entity.Teacher;

/**
 * @author 26414
 */
public interface TeacherService {

  /**
   * 根据id查询老师
   * @param id id
   * @return 老师
   */
  Teacher getTeacherById(Integer id);

}

package com.fly.service.impl;

import com.fly.entity.Teacher;
import com.fly.mapper.TeacherMapper;
import com.fly.service.TeacherService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author 26414
 */
@Service
public class TeacherMapperImpl implements TeacherService {

  @Resource
  private TeacherMapper teacherMapper;

  @Override
  public Teacher getTeacherById(Integer id) {
    return teacherMapper.getTeacherById(id);
  }
}

package com.fly.controller;

import com.alibaba.fastjson.JSON;
import com.fly.entity.Teacher;
import com.fly.service.TeacherService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 26414
 */
@RestController
public class TeacherController {

  @Resource
  private TeacherService teacherService;

  @RequestMapping("/get/{id}")
  public String getTeacherById(@PathVariable Integer id) {
    Teacher teacher = teacherService.getTeacherById(id);
    return JSON.toJSONString(teacher);
  }

}

测试