您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot集成MyMatis-Generator的使用方法”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot集成MyMatis-Generator的使用方法”吧!
POM依賴
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> <version>5.1.40</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies>
根據實際數據源配置
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/spring_mybatis_dljd?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false username: root password: 123456
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.4.0</version> <dependencies> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.40</version> </dependency> </dependencies> <!--指定配置文件的路徑--> <configuration> <!-- 配置文件路徑 --> <configurationFile>${project.basedir}/src/main/resources/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> <!-- sql 映射文件 放在 代碼中時需要配置 此處掃描 src/main/java 中所有xml文件發布時導入resources中。 因為用了資源配置重寫了系統默認的配置,必須配置yml和properties文件 --> <resources> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.yml</include> <include>**/*.properties</include> </includes> </resource> </resources> </build>
注意:
1. 插件中使用的 mysql-connector-java 與application.yml 中保持一致
2.configurationFile 中指定了生成器配置文件 generatorConfig.xml的位置
3.發布時為了將mapper包中sql映射文件xml 導入資源文件中,加入resources。因此導致重新了默認的資源配置,需要額外導入*.yml,*.properties
路徑:
src\main\resources\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> <context id="context" targetRuntime="MyBatis3"> <commentGenerator> <property name="suppressAllComments" value="false"/> </commentGenerator> <!-- !!!! Database Configurations !!!! --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/spring_mybatis_dljd?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=GMT%2B8&useSSL=false&nullCatalogMeansCurrent=true" userId="root" password="123456"/> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!-- !!!! Model Configurations !!!! --> <javaModelGenerator targetPackage="com.zhl.springmybatis.pojo" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="true"/> </javaModelGenerator> <!-- !!!! Mapper XML Configurations !!!! --> <sqlMapGenerator targetPackage="com.zhl.springmybatis.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- !!!! Mapper Interface Configurations !!!! --> <javaClientGenerator targetPackage="com.zhl.springmybatis.mapper" targetProject="src/main/java" type="XMLMAPPER"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- !!!! Table Configurations !!!! --> <!-- <table tableName="users" enableCountByExample="false" enableDeleteByExample="false" enableSelectByExample="false" enableUpdateByExample="false"/>--> <table tableName="%"> <generatedKey column="id" sqlStatement="Mysql"/> </table> </context> </generatorConfiguration>
1. javaModelGenerator 實體和Example文件指定位置
2. sqlMapGenerator SQL映射文件*.Mapper.xml 所在位置
3. javaClientGenerator 接口文件所在位置
4. tableName="%" 為生成所有表
jdbcConnection driverClass :需與application.yml中保持一致
5.1 點擊
5.2 . 生成
@SpringBootApplication@MapperScan("com.zhl.springmybatis.mapper")public class SpringMybatisApplication {public static void main(String[] args) { SpringApplication.run(SpringMybatisApplication.class, args); } }
@Data @ToString public class Student {....}
StudentService:
package com.zhl.springmybatis.service; import com.zhl.springmybatis.pojo.Student; import java.util.List; public interface StudentService { List<Student> getList(); }
StudentServiceImpl
@Service public class StudentServiceImpl implements StudentService { @Resource private StudentMapper studentMapper; @Override public List<Student> getList() { StudentExample studentExample=new StudentExample(); List<Student> list= studentMapper.selectByExample(studentExample); for (Student s:list) { System.out.println(s); } return list; } }
這里引用 Mapper接口 使用 @Resource 避免出現警告紅線。
@SpringBootTest class SpringMybatisApplicationTests { @Resource private StudentMapper studentMapper; @Test void contextLoads() { } @Test public void GetList(){ StudentExample studentExample=new StudentExample(); List<Student> list= studentMapper.selectByExample(studentExample); for (Student s:list) { System.out.println(s); } } }
mybatis-3-mapper.dtd
mybatis-generator-config_1_0.dtd
路徑 File | Settings | Languages & Frameworks | Schemas and DTDs
URI 為 網絡地址
Location 為 本地文件地址
pom中引入
<!-- https://mvnrepository.com/artifact/org.apache.ibatis/ibatis-core --> <dependency> <groupId>org.apache.ibatis</groupId> <artifactId>ibatis-core</artifactId> <version>3.0</version> </dependency>
感謝各位的閱讀,以上就是“SpringBoot集成MyMatis-Generator的使用方法”的內容了,經過本文的學習后,相信大家對SpringBoot集成MyMatis-Generator的使用方法這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。