1、Springboot+Mybatis+SQL Server基础项目搭建:
参考博客:
Springboot+Mybatis+Mysql:https://blog.youkuaiyun.com/guobinhui/article/details/79289189
Springboot+Mybatis+SQL Server:https://blog.youkuaiyun.com/wangshuaiwsws95/article/details/85059207
2、配置Mybatis-generator代码自动生成器:
本项目配置的时候网上找了很多教程,试了各种方法,但是还是Plugins一直没办法出现mybatis-generator,也生成不了代码,直到使用了下面的配置文件,希望有用。
(1)在pom.xml文件中添加Maven插件Mybatis-generator:
注意:<dependensies>中mybatis和sqlserver相关的也得添加上。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--添加mybatis generator maven插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<dependencies>
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
</dependencies>
<executions>
<execution>
<id>my batis generator</id>
<phase>package</phase>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration> <!--配置文件的路径-->
<overwrite>true</overwrite>
<verbose>true</verbose>
<configurationFile>${basedir}/src/main/resources/mybatis_generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
(2)在resource文件夹建立mybatis_generator文件夹,新建generatorConfig.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="jdbc.properties"></properties>-->
<!--数据库驱动包位置-->
<classPathEntry location="C:\Users\18829\.m2\repository\com\microsoft\sqlserver\sqljdbc4\4.0\sqljdbc4-4.0.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressAllComments" value="true" />
</commentGenerator>
<!--数据库URL、用户名、密码-->
<jdbcConnection driverClass="com.microsoft.sqlserver.jdbc.SQLServerDriver"
connectionURL="jdbc:sqlserver://localhost:1433;DatabaseName=eggdemo" userId="sa" password="1q2w3e">
</jdbcConnection>
<!--生成模型包的位置 -->
<javaModelGenerator targetPackage="com.egg.eggdemo.Entity"
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="false" />
</sqlMapGenerator>
<!--生成映射dao(Mapper)的包名和位置-->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.egg.eggdemo.Mapper" targetProject="./src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--需要生成那些数据库(更改tableName和domainObjectName)-->
<table tableName="users" domainObjectName="Users" enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false" >
</table>
</context>
</generatorConfiguration>
(3)配置成功的话,点击IntelliJ IDEA右边菜单栏Maven,在插件Plugins下面会出现mybatis-genera