spring-boot加mybatis-plus基础配置

整个包的路径

spring-boot加mybatis-plus基础配置_第1张图片

spring-boot加mybatis-plus代码自动配置(GenerateTest)

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

/**
 * 自动生成代码(mybatis-plus)
 */
public class GenerateTest {
    public static void main(String[] args) {
        //创建generator对象
        AutoGenerator autoGenerator = new AutoGenerator();
        //数据源
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        dataSourceConfig.setUsername("");
        dataSourceConfig.setPassword("");
        dataSourceConfig.setUrl("jdbc:mysql://localhost:3306/name");
        autoGenerator.setDataSource(dataSourceConfig);
        //全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        globalConfig.setOutputDir(System.getProperty("user.dir")+"/src/main/java");
        globalConfig.setAuthor("admin");
        globalConfig.setOpen(false);
        globalConfig.setServiceName("%sService");
        autoGenerator.setGlobalConfig(globalConfig);
        //包信息
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setParent("com.practice.practice");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setServiceImpl("service.impl");
        packageConfig.setController("controller");
        autoGenerator.setPackageInfo(packageConfig);
        //策略配置
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig.setInclude("my_fruit");
        //数据库中表的名称,它会根据这个表的名称生成相应的类和接口。可以往后多加
        //数据库表的名称可以用下划线,这样后面会驼峰命名法转换
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
        strategyConfig.setColumnNaming(NamingStrategy.underline_to_camel);
        //下划线转驼峰命名法
        strategyConfig.setEntityLombokModel(true);
        autoGenerator.setStrategy(strategyConfig);
        //运行
        autoGenerator.execute();
    }
}

跨域访问处理(Configuration.CrosConfiguration)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class CrosConfiguration implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }

}

entity(实体类)注意事项

要用@TableId配置主键

@Data
  @EqualsAndHashCode(callSuper = false)
    public class MyFruit implements Serializable {

    private static final long serialVersionUID=1L;

    @TableId(type = IdType.AUTO)
    private byte[] name;

    private Long size;

    private Long age;


}

controller注意事项

要写上

@ResponseBody

在当前controller类的上面

@Controller
@ResponseBody
@RequestMapping("/myFruit")
public class MyFruitController {
    @Autowired
    private MyFruitService myFruitService;

    @GetMapping("/list")
    public List<MyFruit> list(){
        List<MyFruit> myFruits=this.myFruitService.list();
        //System.out.println(myFruits);
        return myFruits;
    }
}

总入口注意事项

要写上MapperScan

@SpringBootApplication
@MapperScan("com.practice.practice.mapper")
public class PracticeApplication {

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

}

yml要配置数据库信息(前面那个是代码补全的信息)

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/name
    username:
    password:
    driver-class-name: com.mysql.cj.jdbc.Driver
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath:com/practice/practice/mapper/xml/*.xml
server:
  port: 8181


maven依赖添加

<dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
            <version>3.4.2version>
        dependency>

        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-generatorartifactId>
            <version>3.3.2version>
        dependency>

        <dependency>
            <groupId>org.apache.velocitygroupId>
            <artifactId>velocityartifactId>
            <version>1.7version>
        dependency>

你可能感兴趣的:(spring-boot加mybatis-plus基础配置)