Spring项目后端搭建
一、新建项目
工具:IDEA
1.create new project ->Spring Initializer->next
下一步,选择需要的依赖
下一步,选择项目名称和项目路径,然后next,创建成功后:
2.加入mybatis-plus、mybatis和boot的关联以及模板依赖
<!--mybatis代码快速生成--> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.2</version> </dependency>
3.创建数据库表对应的实体
创建TestGenerator.java文件,并执行,自动生成数据库表对应的实体类
package com.helq3; import com.baomidou.mybatisplus.annotation.IdType; 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; import org.junit.jupiter.api.Test; public class TestGenerator { @Test public void testGenerator() { AutoGenerator autoGenerator = new AutoGenerator(); //全局配置 GlobalConfig globalConfig = new GlobalConfig(); globalConfig.setAuthor("helq3") .setOutputDir("D:\\work\\family_service_platform\\src\\main\\java")//设置输出路径 .setFileOverride(true)//设置文件覆盖 .setIdType(IdType.AUTO)//设置主键生成策略 .setServiceName("%sService")//service接口的名称 .setBaseResultMap(true)//基本结果集合 .setBaseColumnList(true)//设置基本的列 .setControllerName("%sController"); //配置数据源 DataSourceConfig dataSourceConfig = new DataSourceConfig(); dataSourceConfig.setDriverName("com.mysql.cj.jdbc.Driver").setUrl("jdbc:mysql://localhost:3306/family_service_platform?serverTimezone=UTC") .setUsername("root").setPassword("root"); //策略配置 StrategyConfig strategyConfig = new StrategyConfig(); strategyConfig.setCapitalMode(true)//设置全局大写命名 .setNaming(NamingStrategy.underline_to_camel)//数据库表映射到实体的命名策略 //.setTablePrefix("tbl_")//设置表名前缀 .setInclude(); //包名配置 PackageConfig packageConfig = new PackageConfig(); packageConfig.setParent("com.helq3").setMapper("mapper") .setService("service").setController("controller") .setEntity("bean").setXml("mapper"); autoGenerator.setGlobalConfig(globalConfig).setDataSource(dataSourceConfig) .setStrategy(strategyConfig).setPackageInfo(packageConfig); autoGenerator.execute(); } }
生成实体类后,启动程序报mapper对象无法自动装配,因此需要在启动的application类上添加@MapperScan注解或者在每一个mapper的接口上添加@Mapper注解
由于mapper.xml文件不会自动加载到class文件中,所以,需要配置pom.xml文件<build>标签中增加
<resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> </resources>
4.resources->新建application.yaml文件,并补充配置
#项目启动端口 server: port: 8080 #数据源配置 spring: datasource: url: jdbc:mysql://localhost:3306/family_service_platform?serverTimeZone=UTC&useSSL=false username: root password: * driver-class-name: com.mysql.cj.jdbc.Driver #配置mybatis mybatis: mapper-location: classpath:com/helq3/mapper/*.xml configuration: map-underscore-to-camel-case: true #sql语句日志打印 logging: level: com: helq3: mapper: debug
5.使用Json需要在pom.xml注入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.9</version> </dependency>