欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

通用mapper使用Example代码实例(springboot)

程序员文章站 2024-03-02 13:09:46
...

通用mapper使用Example代码实例(springboot)

本文环境springboot,mysql整合mybatis:用通用mapper实现mysql数据库的增删改查;

1.jar包导入

在pom.xml文件加上如下配置:
其中有Mybatis依赖Jar包,Mapper依赖Jar包,pagehelper分页依赖Jar包(可选)

    <dependency>  
        <groupId>org.mybatis</groupId>  
        <artifactId>mybatis</artifactId>  
        <version>${mybatis.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.mybatis</groupId>  
        <artifactId>mybatis-spring</artifactId>  
        <version>${mybatis.spring.version}</version>  
    </dependency>  
    <dependency>  
        <groupId>org.mybatis.generator</groupId>  
        <artifactId>mybatis-generator-core</artifactId>  
        <version>${mybatis.generator.version}</version>  
        <scope>provided</scope>  
    </dependency>  
    <!-- mybatis分页插件 使用见:https://pagehelper.github.io/docs/howtouse/-->  
    <dependency>  
        <groupId>com.github.pagehelper</groupId>  
        <artifactId>pagehelper</artifactId>  
        <version>${pagehelper.version}</version>  
    </dependency>  
      
    <!--通用Mapper,见:http://www.mybatis.tk/-->  
    <dependency>  
        <groupId>tk.mybatis</groupId>  
        <artifactId>mapper</artifactId>  
        <version>${mybatis.mapper.version}</version>  
    </dependency>  

2.与spring整合

在yml配置文件加上扫描器,这个扫描器和Mybatis默认是不一样的:
使用的类是:tk.mybatis.spring.mapper.MapperScannerConfigurer,包名不一样。

<!--配置扫描器,将Mybatis接口的实现加入到ioc容器 -->  
 <!-- 使用通用Mapper,见:https://mapperhelper.github.io/docs/1.integration/ -->  
 <bean class="tk.mybatis.spring.mapper.MapperScannerConfigurer">  
    <property name="basePackage" value="com.lqy.ssm.dao"></property>  
</bean>  

3.自动生成代码文件的mybatis-mapper-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> 
    	<!-- <properties resource="application-dev.yml"/>  -->
    	<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
		<!--处理sql中的`符号--> 
		<property name="beginningDelimiter" value="`"/> 
		<property name="endingDelimiter" value="`"/> 
		
		<!--添加自定义的继承接口--> 
		<plugin type="tk.mybatis.mapper.generator.MapperPlugin"> 
			<property name="mappers" value="com.test.base.BaseMapper"/>
		</plugin>

		<!--数据源配置--> 
		<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
						connectionURL="jdbc:mysql://192.0.0.1:3306/test?useUnicode=true&amp;characterEncoding=UTF-8&amp;autoReconnect=true&amp;failOverReadOnly=false&amp;allowMultiQueries=true&amp;serverTimezone=Hongkong&amp;useSSL=true"
						userId="root" 
						password="root">
            <!--MySQL 8.x 需要指定服务器的时区-->
            <property name="serverTimezone" value="UTC"/>
            <!--防止生成别的库里的同名表-->
            <property name="nullCatalogMeansCurrent" value="true"/>
		</jdbcConnection> 
		
		<!--model包路径--> 
		<javaModelGenerator targetPackage="com.test.entity" targetProject="src/main/java"/>
		<!--mapper.xml包路径--> 
		<sqlMapGenerator targetPackage="com.test.mapper" targetProject="src/main/resources"/>
		<!--mapper包路径--> 
		<javaClientGenerator targetPackage="com.test.mapper" targetProject="src/main/java" type="XMLMAPPER"/>
		<!--表配置,tableName支持%,表示全部生成--> 
		<!--<table tableName="sys_permission_t" domainObjectName="SysPermission" >-->
            <!--<generatedKey column="id" sqlStatement="Mysql" identity="true"/>-->
        <!--</table>-->
        <table tableName="tb_test" domainObjectName="Test">
        </table>
	</context> 
</generatorConfiguration>

4.mapper文件的寫法

public interface BankCardBinMapper extends BaseMapper<TestBin> {}

baseMapper的类定义

public interface BaseMapper<T> extends Mapper<T>, InsertListMapper<T>{

}

5.整合example

Example example=new Example(Test.class);
Example.Criteria criteria = example.createCriteria();
criteria.andEqualTo("name", name);
List<Test> list = TestMapper.selectByExample(example);
相关标签: example mabatis