1.通过maven创建springboot项目
maven --> org.apache.maven.archetypes:maven-archetype-quickstart
若不使用springboot,则选择maven-archetype-webapp,以war包形式部署。
2. 引入springboot依赖包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.测试简单运行
@EnableAutoConfiguration //SpringBoot自动化配置
public class App
{
@RequestMapping("/")
public String home(){
return "Hello World!";
}
public static void main( String[] args )
{
System.out.println( "Hello World!" );
SpringApplication.run(App.class,args); //启动SpringBoot
}
}
4.Mybatis 接入SpringBoot
引入依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.3</version>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
添加插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!--允许移动文件-->
<verbose>true</verbose>
<!--允许自动覆盖文件-->
<overwrite>true</overwrite>
<!--mybatis配置文件-->
<configurationFile>
src/main/resources/mybatisConfig-generator.xml
</configurationFile>
</configuration>
<executions>
<execution>
<id>mybatis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.13</version>
</dependency>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
</dependencies>
</plugin>
创建mybatisConfig-generator.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>
<context id="DB2Tables" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/***?useInformationSchema=true"
userId="***"
password="***">
</jdbcConnection>
<!-- 生成DataObject类存放位置 -->
<javaModelGenerator targetPackage="com.***.dataobject" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- 生成映射文件存放位置 -->
<sqlMapGenerator targetPackage="mapping" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- 生成Dao类存放位置 -->
<!-- 客户端代码,生成易于使用的针对Model对象和XML配置文件的代码
type="ANNOTATIONDMAPPER",生成Java Model和基于注解的Mapper 对象
type="MIXEDMAPPER",生成基于注解的Java Model和相应的Mapper对象
type="XMLMAPPER",生成SQLMap XML 文件和独立的Mapper接口
-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.***.dao" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--生成对应表及类名-->
<table schema="***" tableName="user_info" domainObjectName="UserDO" enableCountByExample = "false"
enableUpdateByExample = "false" enableDeleteByExample = "false"
enableSelectByExample = "false" selectByExampleQueryId = "false"></table>
</context>
</generatorConfiguration>
自动生成数据库Mybatis映射文件
网友评论