⼿把⼿教会Spring⾃定义XML配置扩展
实现功能:根据数据库中的表,⾃动⽣成model、dao和对应的xml⽂件。xml中实现通⽤mapper中CURD功能1、引⼊依赖
<properties>
&ator.version>1.3.7</ator.version>    &batis.version>4.1.5</tk.mybatis.version>
</properties>
<dependencies>
<!--mybatis-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<!--通⽤mapper-->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper</artifactId>
<version>${tk.mybatis.version}</version>
</dependency>
<!--mybatis-generator-->
<dependency>
<groupId&ator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>${ator.version}</version>
</dependency>
<!--mysql数据库-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid数据源-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
<dependencies>
<build>
<plugins>
<!--配置Maven中 mybatis-generator 插件-->
<!--加⼊tk.mybatis的依赖-->
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>${ator.version}</version>
<configuration>
<!--可移动-->
<verbose>true</verbose>
<!--可覆盖-->
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId&batis</groupId>
<artifactId>mapper</artifactId>
<version>${tk.mybatis.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
2、配置通⽤mapper
⽅式1:配置要扫描的mapper
@SpringBootApplication
@MapperScan(basePackages = "com.liang.mapper")
public class MyappApplication {
public static void main(String[] args) {
SpringApplication.run(MyappApplication.class, args);
}
}
⽅式2:扫描的mapper
@Configuration
public class MybatisConfig {
@Bean(name = "mapperScannerConfigurer")
public MapperScannerConfigurer MapperScannerConfigurer1() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();    mapperScannerConfigurer.setBasePackage("com.liang.mapper");
Properties properties = new Properties();
properties.setProperty("notEmpty", "false");
properties.setProperty("IDENTITY", "MYSQL");
properties.setProperty("mappers", Name());
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactory");
mapperScannerConfigurer.setProperties(properties);
return mapperScannerConfigurer;
}
}
3、application.yaml中配置mybatis
#Mybatis(注意不是 fig-location 这个配置)
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.liang.pojo
configuration-properties: {
spring怎么读取xml文件notEmpty: false,
IDENTITY: 'MYSQL',
mappers: "tk.mybatis.mappermon.Mapper"
}
4、添加l
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-////DTD MyBatis Generator Configuration 1.0//EN"
"/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动-->
<classPathEntry location="E:\ideaMaven\MavenRepository\mysql\mysql-connector-java\8.0.22\mysql-connector-java-8.0.22.jar" />
<context id="DB2Tables" targetRuntime="MyBatis3Simple">
<!--beginningDelimiter和endingDelimiter:指明数据库的⽤于标记数据库对象名的符号,⽐如ORACLE就是双引号,MYSQL默认是`反引号; -->
<property name="beginningDelimiter" value="`"></property>
<property name="endingDelimiter" value="`"></property>
<!-- ⽣成的Java⽂件的编码 -->
<property name="javaFileEncoding" value="utf-8"/>
<!--添加Tk-mapper插件-->
<plugin type="tk.ator.MapperPlugin" >
<property name="mappers" value="tk.mybatis.mappermon.Mapper"/>
</plugin>
<jdbcConnection driverClass="sql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/temp_db?serverTimezone=Asia/Shanghai&characterEncoding=utf-8&useUnicode=true&am p;useSSL=false"
userId="root"
password="123456">
</jdbcConnection>
<!-- java类型处理器⽤于处理DB中的类型到Java中的类型,默认使⽤JavaTypeResolverDefaultImpl;注意⼀点,默认会先尝试使⽤Integer,Long,Short 等来对应DECIMAL和 NUMERIC数据类型; -->
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--⽣成entity类存放位置-->
<javaModelGenerator targetPackage="com.liang.pojo" targetProject="src/main/java">
<property name="enableSubPackages" value="false" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!--⽣成映射⽂件存放位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!--⽣成Dao类存放位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.liang.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!--要⽣成的Table,注意不同的数据库,sqlStatement-->
<!--sqlStatement 不同的数据库配置 /generator/configreference/generatedKey.html-->
<table domainObjectName="School2" tableName="t_school">
<!--<generatedKey column="id" sqlStatement="MySql" identity="true" />-->
<generatedKey column="id" sqlStatement="SELECT LAST_INSERT_ID() AS id" identity="true" />
</table>
</context>
</generatorConfiguration>
5、完成

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。