Mybatis的逆向工程功能还是很强大的,可以自动生成实体类以及Mapper文件和对象文件,这样可以大大减少开发人员的工作量。
-
首先我们要在pom.xml引入依赖
org.mybatis.generator mybatis-generator-maven-plugin 1.3.6 mysql mysql-connector-java GeneratorMapper.xml true true mysql mysql-connector-java -
创建generatorConfig.xml文件
配置完后执行逆向工程
自动生成这三个文件
数据库的配置
Mybatis集合及运用**展示:**我们可以利用Mybatis实现从数据库中查询功能
具体实现方法如下:-
配置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; } } -
配置Service接口
package com.nanjing.springboot.service; import com.nanjing.springboot.model.Student; public interface StudentService { Student queryStudentById(Integer id); } -
配置实现类
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); } } -
配置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); } -
配置对应的xml类
id, name, age -
注意:需要再pom中指定文件夹为resources,否则mapper层无法被调用
src/main/java **/*.xml



