步骤1:在mybatis-generator中添加要生成的数据库表名
在genratorConfig.xml内容:
<!-- 数据库表 -->
<table tableName="stu"></table>
步骤2:StuMapper.xml和StuMapper拷贝到对应的mapper模块下
步骤3:pojo对应Stu.java 拷贝到pojo模块下
步骤4:创建service接口和对应的实现类
StuService接口:
package com.one.service;
import com.one.pojo.Stu;
public interface StuService {
public Stu getStuInfo(int id);
}
StuServiceImpl实现类:
package com.one.service.impl;
import com.one.mapper.StuMapper;
import com.one.pojo.Stu;
import com.one.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
public class StuServiceImpl implements StuService {
@Autowired
public StuMapper stuMapper;
@Transactional(propagation = Propagation.SUPPORTS)
@Override
public Stu getStuInfo(int id) {
return stuMapper.selectByPrimaryKey(id);
}
}
步骤5:创建controll控制层
package com.one.controller;
import com.one.service.StuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class StuFooController {
@Autowired
private StuService stuService;
@GetMapping("/getStu")
public Object getStu(int id) {
return stuService.getStuInfo(id);
}
}
步骤6:启动类上添加,扫描mybatis通用 mapper所在的包
package com.one;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
/**
* 启动类
* @date: 2023年06月17日 15:39
*/
@SpringBootApplication
@MapperScan(basePackages="com.one.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
步骤7:访问结果