创建spring项目
创建spring项目:
file--->new---->(model)/project----->spring Initializr---->next




----->next---->finish
spring项目创建完成:运行main函数---->打开浏览器:localhost:8080
此时会有一个空白页报错,但是证明应用程序找到了
引入数据库:
在创建好的spring项目的pom.xml文件中加入依赖
<!-- =================要添加的部分开始================== -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!-- Mybatis代码生成工具 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<!-- mysql-connector-java: mysql数据库驱动包
在编译时没有直接使用,但是运行时需要,所以使用
scope runtime -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
<scope>runtime</scope>
</dependency>
<!-- druid-spring-boot-st arter: 阿里Druid数据库连接池,同样的运行时需要 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!-- =================要添加的部分结束================== -->
在java路径下添加代码Generator.java:
import org.mybatis.generator.api.MyBatisGenerator; import org.mybatis.generator.config.Configuration; import org.mybatis.generator.config.xml.ConfigurationParser; import org.mybatis.generator.internal.DefaultShellCallback; import java.io.File; import java.io.InputStream; import java.util.ArrayList; import java.util.List; public class Generator { private static final boolean OVERWRITE = true; private static final String CONFIG_PATH = "generator/config.xml"; public static void main(String[] args) throws Exception { System.out.println("--------------------start generator-------------------"); System.out.println(new File("").getAbsolutePath()); List<String> warnings = new ArrayList<>(); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream(CONFIG_PATH); ConfigurationParser cp = new ConfigurationParser(warnings); Configuration config = cp.parseConfiguration(is); DefaultShellCallback callback = new DefaultShellCallback(OVERWRITE); MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings); myBatisGenerator.generate(null); warnings.forEach(System.err::println); System.out.println("--------------------end generator-------------------"); } }
在resources下创建generator文件
然后加入config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"> <generatorConfiguration> <properties resource="application.properties" /> <context id="Mysql" targetRuntime="MyBatis3" defaultModelType="flat"> <property name="javaFileEncoding" value="UTF-8"/> <commentGenerator> <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true" /> <property name="addRemarkComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="${spring.datasource.driver-class-name}" connectionURL="${spring.datasource.url}" userId="${spring.datasource.username}" password="${spring.datasource.password}"> <property name="useInformationSchema" value="true" /> </jdbcConnection> <!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer, 为 true时把JDBC DECIMAL 和 NUMERIC 类型解析为java.math.BigDecimal --> <javaTypeResolver> <property name="forceBigDecimals" value="false" /> </javaTypeResolver> <!--MyBatis 生成器只需要生成 Model--> <javaModelGenerator targetProject="修改为自己的项目名/src/test/java" targetPackage="修改为实体类的包"> <!-- <property name="rootClass" value="修改为实体类的父类"/>--> </javaModelGenerator> <!--mybatis 的xml文件地址--> <sqlMapGenerator targetProject="修改为自己的项目名/src/test/resources" targetPackage="修改为xml的包"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--mybatis的mapper接口--> <javaClientGenerator type="XMLMAPPER" targetProject="修改为自己的项目名/src/test/java" targetPackage="修改为mapper的包"> <property name="enableSubPackages" value="true"/> <!-- <property name="rootInterface" value="修改为mapper的父接口"/>--> </javaClientGenerator> <!-- 需要生成的表,%表示模糊匹配,也可以指定具体的表名 --> <!-- <table tableName="%"--> <!-- enableCountByExample="false"--> <!-- enableDeleteByExample="false"--> <!-- enableSelectByExample="false"--> <!-- enableUpdateByExample="false"--> <!-- >--> <table tableName="%"> <!-- insert方法通过自增主键插入数据后,主键值是否设置到对象属性中 --> <!-- <generatedKey column="id" sqlStatement="JDBC"/>--> <generatedKey column="id" sqlStatement="Mysql" identity="true" /> </table> <!-- <table tableName="user">--> <!-- <generatedKey column="id" sqlStatement="Mysql" identity="true" />--> <!-- </table>--> </context> </generatorConfiguration>
检查项目路径:
// package tool; import java.io.File; public class Test { public static void main(String[] args) { System.out.println(new File("").getAbsolutePath()); // F:\编程\bite资料\javaee\javaee_study\springboot1 } }

浙公网安备 33010602011771号