栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot学习:逆向工程-集合MyBatis

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot学习:逆向工程-集合MyBatis

SpringBoot学习4:逆向工程&集合MyBatis 逆向工程:

Mybatis的逆向工程功能还是很强大的,可以自动生成实体类以及Mapper文件和对象文件,这样可以大大减少开发人员的工作量。

  1. 首先我们要在pom.xml引入依赖

    
                    org.mybatis.generator
                    mybatis-generator-maven-plugin
                    1.3.6
                    
                        
                            mysql
                            mysql-connector-java
    
                        
                    
                    
                        
                        GeneratorMapper.xml
                        true
                        true
                    
                
    
            
                mysql
                mysql-connector-java
    
            
    
  2. 创建generatorConfig.xml文件

    
    
    
        
        
        
        
            
            
                
            
            
            
            
            
            
                
                
            
            
            
                
            
            
            
                
            
            
            

    配置完后执行逆向工程

    自动生成这三个文件

    数据库的配置

    Mybatis集合及运用

    **展示:**我们可以利用Mybatis实现从数据库中查询功能

    具体实现方法如下:
    1. 配置Controller文件

      package com.nanjing.springboot.web;
      
      
      import com.nanjing.springboot.model.Student;
      import com.nanjing.springboot.service.StudentService;
      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;
      
      @Controller
      public class StudentController {
      
          @Autowired
          private StudentService studentService;
      
          @RequestMapping(value = "/student")
          @ResponseBody
          public Object student(Integer id){
              Student student=studentService.queryStudentById(id);
              return student;
          }
      }
      
    2. 配置Service接口

      package com.nanjing.springboot.service;
      
      import com.nanjing.springboot.model.Student;
      
      public interface StudentService {
          
          Student queryStudentById(Integer id);
      }
      
    3. 配置实现类

      package com.nanjing.springboot.service.impl;
      
      import com.nanjing.springboot.mapper.StudentMapper;
      import com.nanjing.springboot.model.Student;
      import com.nanjing.springboot.service.StudentService;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      
      @Service
      public class StudentServiceImpl implements StudentService {
      
          @Autowired
          private StudentMapper studentMapper;
          @Override
          public Student queryStudentById(Integer id) {
              return studentMapper.selectByPrimaryKey(id);
          }
      }
      
      
    4. 配置Mapper类

      package com.nanjing.springboot.mapper;
      
      import com.nanjing.springboot.model.Student;
      import org.apache.ibatis.annotations.Mapper;
      
      @Mapper//扫描DAO接口到spring容器
      public interface StudentMapper {
          int deleteByPrimaryKey(Integer id);
      
          int insert(Student record);
      
          int insertSelective(Student record);
      
          Student selectByPrimaryKey(Integer id);
      
          int updateByPrimaryKeySelective(Student record);
      
          int updateByPrimaryKey(Student record);
      }
      
    5. 配置对应的xml类

      
      
      
        
      
      
      
      
          
          
          
        
      
        
          id, name, age