generatorConfig.xml生成代码后续完善思路

95 阅读3分钟

前端访问数据 后端提供对应接口

Controller监听,Service处理数据,Mapper操作数据库

在 MyBatis 中,mapper.xml 文件用于编写SQL 映射语句,将 Java 对象和数据库表之间进行映射。

上一篇文章:generatorConfig.xml - 掘金 (juejin.cn)

使用generatorConfig.xml的重点

  1. 配置文件的地址
<!-- 引入配置文件 -->
<properties url="file:///D:/A/musicWebsite/src/main/resources/generator.properties"/>
  1. 生成实体类的地址
<!-- 生成实体类地址 -->
<javaModelGenerator targetPackage="com.yuze.musicwebsite.model"
                    targetProject="${project}" >
    <!-- 是否在当前路径下新加一层schema,eg:fase路径com.packetXXX.model, true:com.packetXXX.model.[schemaName] -->
    <property name="enableSubPackages" value="false"/>
    <!-- 是否针对string类型的字段在set的时候进行trim调用 -->
    <property name="trimStrings" value="true"/>
</javaModelGenerator>
  1. 生成mapxml的地址
<sqlMapGenerator targetPackage="mapper"
                 targetProject="${project}" >
    <!-- 是否在当前路径下新加一层schema,eg:fase路径mapper, true:mapper.[schemaName] -->
    <property name="enableSubPackages" value="false" />
</sqlMapGenerator>
  1. 生成client的接口dao的地址
<!-- 生成mapxml对应client,也就是接口dao -->
<javaClientGenerator targetPackage="com.yuze.musicwebsite.mapper"
                     targetProject="${project}" type="XMLMAPPER" >
    <!-- 是否在当前路径下新加一层schema,eg:fase路径com.packetXXX.mapper, true:com.packetXXX.mapper.[schemaName] -->
    <property name="enableSubPackages" value="false" />
</javaClientGenerator>

书写完成后检查各个类的所在包名是否正确 xml文件内的(如下图)是否是全称

1693063969142.png

正确位置放置如图

image.png

展开后(未展示即为里面是空的)(以下两张图)

image.png

image.png

完善阶段:如何运行该程序

笔者现在正在学习一个新项目 以该项目为例 我们想要运行localhost:8888/allUser1后得到数据

image.png

  1. 确定Mapper.xml有对应指令操作数据库
<select id="allUser1" resultMap="BaseResultMap">
    select *
    from consumer
  </select>
  1. Mapper接口里面写入
@Repository
public interface ConsumerMapper extends BaseMapper<Consumer> {

    //Test
    List<Consumer> allUser1();
}
  1. Service接口写入
public interface ConsumerService extends IService<Consumer> {
    //Test
    List<Consumer> allUser1();
}
  1. Service实现类里写
package com.yuze.musicwebsite.service.impl;
@Service
public class ConsumerServiceImpl extends ServiceImpl<ConsumerMapper,Consumer>
        implements  ConsumerService{

    @Autowired
    private ConsumerMapper consumerMapper;

    //Test
    @Override
    public List<Consumer> allUser1() {
        return consumerMapper.allUser1();
    }
}
  1. Controller写入
package com.yuze.musicwebsite.controller;

@RestController
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;

    //test
    @RequestMapping(value = "/allUser1", method = RequestMethod.GET)
    public Object allUser1(){
        return consumerService.allUser1();
    }
}
  1. 项目运行主程序
@SpringBootApplication
@MapperScan("com.yuze.musicwebsite.mapper")
public class MusicWebsiteApplication {

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

}

用户视角的顺序

  1. 输入网站localhost:8888/allUser1
  2. 访问的接口交给Controller层处理,Controller将 命令传给Service层
  3. Service层调用Mapper/Dao层的对应方法返回数据,注意:Mapper接口操作数据库依赖Mapper.xml文件

注意点

运行的Application文件运行后只对所在包下的所有包、类进行扫描

另一种不需要在xml文件中书写指令的代码模式

上面是编写代码从底层model到控制台思路,这篇从用户角度思路来写 用户输入网址localhost:8888/singer/sex/detail?sex=0 代表歌手中性别为女的信息 得出下图效果

image.png

  1. Controller层接收用户指令,进入对应方法
@RestController
public class SingerController {

    @Autowired
    private SingerService singerService;

    // 根据歌手性别查找歌手
    @GetMapping("/singer/sex/detail")
    public R singerOfSex(@RequestParam int sex) {
        return singerService.singerOfSex(sex);
    }
  1. Controller将对应命令传给Service

接口:

public interface SingerService extends IService<Singer> {
        R singerOfSex(Integer sex);
}

实现类:

@Service
public class SingerServiceImpl extends ServiceImpl<SingerMapper, Singer> implements SingerService {

    @Autowired
    private SingerMapper singerMapper;
    
    @Override
    public R singerOfSex(Integer sex) {
        QueryWrapper<Singer> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("sex", sex);
        return R.success(null, singerMapper.selectList(queryWrapper));
    }
}
  1. Service调用Mapper操作数据库
@Repository
public interface SingerMapper extends BaseMapper<Singer> {
}
  1. Mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.yuze.musicwebsite.mapper.SingerMapper">
  <resultMap id="BaseResultMap" type="com.yuze.musicwebsite.model.Singer">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="name" jdbcType="VARCHAR" property="name" />
    <result column="sex" jdbcType="TINYINT" property="sex" />
    <result column="pic" jdbcType="VARCHAR" property="pic" />
    <result column="birth" jdbcType="TIMESTAMP" property="birth" />
    <result column="location" jdbcType="VARCHAR" property="location" />
    <result column="introduction" jdbcType="VARCHAR" property="introduction" />
  </resultMap>
  <sql id="Base_Column_List">
    id, name, sex, pic, birth, location, introduction
  </sql>
</mapper>
  1. Request作为传递的参数
@Data
public class SingerRequest {
    private Integer id;

    private String name;

    private Byte sex;

    private String pic;

    private Date birth;

    private String location;

    private String introduction;
}
  1. 与数据库表对应的实体类

@TableName(value = "singer")
@Data
public class Singer {

    @TableId(type = IdType.AUTO)
    private Integer id;

    private String name;

    private Byte sex;

    private String pic;

    private Date birth;

    private String location;

    private String introduction;

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this);
    }
}

参考:Vue + SpringBoot + MyBatis 音乐网站 | Yin-Hongwei