Spring 中使用 Mybatis generator 自动生成代码
2020-12-13 06:29
标签:har LLC core googl mode span ict ext image Mybatis generator 是一个 Mybatis 插件,可以用于自动生成项目中需要的 pojo 类、对应的 Mapper 接口和 mapper.xml 文件。使用该插件,一方面可以节省开发时间,另一方面也避免手写时打错字的问题。 由于工作需要,我打算在原有的 Spring 项目中使用 Mybatis generator 自动生成代码,以便简化 DAO 层的开发。 闲言少叙,just do it! 1. 引入依赖 a. 父模块配置 b. 子模块配置 我们在子模块中引入插件 mybatis-generator-maven-plugin。 2. 引入配置文件 由于上文在 mybatis-generator-maven-plugin 插件中指定了所需的配置文件:generatorConfig.xml,所以在子模块 resources 目录下引入该配置文件。 3. 自动生成 Mapper 接口、XML 文件和 pojo 类 点击 IDEA 右侧的 Maven 控件栏,找到对应模块下面的 mybatis-generator 插件,双击即可自动生成 Mapper 接口、mapper.xml 文件和 pojo 类。 4. Spring 中使用 Mapper 接口 a. 配置 spring-meta.xml 文件 b. config.properties 配置 注意上述的 spring-meta.xml、config.properteis 文件都放在 resources 目录下。 子模块整体结构如下: c. 接口中新增方法 此时 Mapper 接口已经具备部分 CRUD 的能力,再新增一个自定义的 getNumberWeNeed 方法。 Mapper 接口: 相应的 XML 文件: 5. 编写测试类验证 以上。 Spring 中使用 Mybatis generator 自动生成代码 标签:har LLC core googl mode span ict ext image 原文地址:https://www.cnblogs.com/hurricaneyang/p/11180847.htmlproperties>
spring_version>4.3.9.RELEASEspring_version>
mysql_connector_version>5.1.21mysql_connector_version>
gson_version>2.2.2gson_version>
commons_beanutils_version>1.8.3commons_beanutils_version>
commons_io_version>2.0.1commons_io_version>
junit_version>4.12junit_version>
skip_maven_deploy>trueskip_maven_deploy>
lombok.version>1.18.2lombok.version>
project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
project.build.sourceOptimize>trueproject.build.sourceOptimize>
project.build.sourceDebug>trueproject.build.sourceDebug>
project.build.sourceTarget>1.8project.build.sourceTarget>
properties>
dependencyManagement>
dependencies>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-coreartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-beansartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-aopartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-contextartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-context-supportartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-txartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-ormartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-webmvcartifactId>
version>${spring_version}version>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-testartifactId>
version>${spring_version}version>
scope>testscope>
dependency>
dependency>
groupId>commons-beanutilsgroupId>
artifactId>commons-beanutilsartifactId>
version>${commons_beanutils_version}version>
dependency>
dependency>
groupId>commons-iogroupId>
artifactId>commons-ioartifactId>
version>${commons_io_version}version>
dependency>
dependency>
groupId>com.google.code.gsongroupId>
artifactId>gsonartifactId>
version>${gson_version}version>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>${mysql_connector_version}version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
version>3.2.8version>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatis-springartifactId>
version>1.2.2version>
dependency>
dependency>
groupId>org.apache.ibatisgroupId>
artifactId>ibatis-sqlmapartifactId>
version>${ibatis_version}version>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
version>1.0.12version>
dependency>
dependency>
groupId>org.aspectjgroupId>
artifactId>aspectjweaverartifactId>
version>1.8.4version>
dependency>
dependency>
groupId>junitgroupId>
artifactId>junitartifactId>
version>${junit_version}version>
scope>testscope>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>fastjsonartifactId>
version>1.2.58version>
dependency>
dependency>
groupId>org.projectlombokgroupId>
artifactId>lombokartifactId>
version>${lombok.version}version>
scope>providedscope>
dependency>
dependencies>
dependencyManagement>
dependencies>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatisartifactId>
dependency>
dependency>
groupId>org.mybatisgroupId>
artifactId>mybatis-springartifactId>
dependency>
dependency>
groupId>com.alibabagroupId>
artifactId>druidartifactId>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-jdbcartifactId>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-ormartifactId>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-aopartifactId>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-txartifactId>
dependency>
dependency>
groupId>org.springframeworkgroupId>
artifactId>spring-testartifactId>
dependency>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
dependency>
dependencies>
build>
plugins>
plugin>
groupId>org.mybatis.generatorgroupId>
artifactId>mybatis-generator-maven-pluginartifactId>
version>1.3.5version>
configuration>
configurationFile>${basedir}/src/main/resources/generatorConfig.xmlconfigurationFile>
overwrite>trueoverwrite>
verbose>trueverbose>
configuration>
dependencies>
dependency>
groupId>mysqlgroupId>
artifactId>mysql-connector-javaartifactId>
version>${mysql_connector_version}version>
dependency>
dependencies>
plugin>
plugins>
build>
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="baseset" targetRuntime="MyBatis3Simple" defaultModelType="flat">
commentGenerator>
property name="suppressAllComments" value="false"/>
property name="suppressDate" value="true"/>
commentGenerator>
jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://ip:port/db_name"
userId="username"
password="password">
jdbcConnection>
javaModelGenerator targetPackage="cn.com.meta.pojo" targetProject="src/main/java">
property name="enableSubPackages" value="true"/>
property name="trimStrings" value="true"/>
javaModelGenerator>
sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
property name="enableSubPackages" value="true"/>
sqlMapGenerator>
javaClientGenerator targetPackage="cn.com.meta.mapper" targetProject="src/main/java" type="XMLMAPPER">
property name="enableSubPackages" value="true"/>
javaClientGenerator>
table tableName="t_table_name" domainObjectName="TableNamePojo" enableCountByExample="false"
enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
generatedKey column="id" sqlStatement="Mysql" identity="true"/>
table>
context>
generatorConfiguration>
xml version="1.0" encoding="UTF-8"?>
beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
tx:annotation-driven transaction-manager="transactionManager"/>
bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
property name="dataSource" ref="dataSource"/>
bean>
context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true"/>
bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
property name="url" value="${jdbc.url}"/>
property name="username" value="${jdbc.username}"/>
property name="password" value="${jdbc.password}"/>
property name="driverClassName" value="${jdbc.driver}"/>
property name="initialSize" value="10"/>
property name="minIdle" value="10"/>
property name="maxActive" value="200"/>
property name="maxWait" value="60000"/>
property name="timeBetweenEvictionRunsMillis" value="60000"/>
property name="minEvictableIdleTimeMillis" value="300000"/>
property name="validationQuery" value="SELECT ‘x‘"/>
property name="testWhileIdle" value="true"/>
property name="testOnBorrow" value="false"/>
property name="testOnReturn" value="false"/>
property name="poolPreparedStatements" value="true"/>
property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
property name="filters" value="stat"/>
bean>
bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
property name="dataSource" ref="dataSource"/>
property name="mapperLocations">
list>
value>classpath:mapper/*Mapper.xmlvalue>
list>
property>
property name="typeAliasesPackage" value="cn.com.meta.pojo"/>
bean>
bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
property name="basePackage" value="cn.com.meta.mapper"/>
property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
bean>
beans>
jdbc.url=jdbc:mysql://ip:port/db_name?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
jdbc.username=username
jdbc.password=password
jdbc.driver=com.mysql.jdbc.Driver
public interface HelloMapper {
int deleteByPrimaryKey(String tjId);
int insert(Hello record);
Hello selectByPrimaryKey(String tjId);
ListHello> selectAll();
// 根据月份、地区查找所需的数值型字段
BigDecimal getNumberWeNeed(@Param("column") String column,
@Param("month") String month,
@Param("area") String area);
}
select id="getNumberWeNeed" resultType="java.math.BigDecimal">
select ${column} from t_table_name where month = #{month}
and area= #{area}
select>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:spring-meta.xml"})
public class HelloMapperTest {
@Autowired
HelloMapper helloMapper;
@Test
public void testGetWeNeedNumber(){
// 比较两个 BigDecimal 类型的数必须用 compareTo
Assert.assertTrue(new BigDecimal("2").compareTo(helloMapper.getNumberWeNeed("total", "209905", "BeiJing")) == 0);
}
}
文章标题:Spring 中使用 Mybatis generator 自动生成代码
文章链接:http://soscw.com/essay/33108.html