008、使用Mybatis

一、使用mybatis

1. 添加依赖

		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.0.1</version>
		</dependency>

2. 配置

# 自定义 类型转换处理器的包
mybatis.type-handlers-package=com.example.mybatis.typehandler
# 驼峰转换 比如 updateTime 对应数据库 update_time
mybatis.configuration.map-underscore-to-camel-case=true
# 打印sql com.example.mybatis.mapper是maper类的包
logging.level.com.example.mybatis.mapper=trace

3. 扫描Mapper类

@MapperScan(basePackages = { "com.example.mybatis.mapper" })

 

4. 自定义mybatis类型转换类

如 2.配置中,第1条  com.example.mybatis.typehandler 包里放着所有转换类型的处理器。

继承 org.apache.ibatis.type.BaseTypeHandler<T> 抽象类, T 类实体的类型,主要需要实现1个set和3个get方法:

  public abstract void setNonNullParameter(PreparedStatement ps, int i, T parameter, JdbcType jdbcType) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, String columnName) throws SQLException;

  public abstract T getNullableResult(ResultSet rs, int columnIndex) throws SQLException;

  public abstract T getNullableResult(CallableStatement cs, int columnIndex) throws SQLException;

 

二、使用Mybatis插件生成Mapper

参考官网 http://www.mybatis.org/generator/

1. 引入依赖

        <dependency>
			<groupId>org.mybatis.generator</groupId>
			<artifactId>mybatis-generator-core</artifactId>
			<version>1.3.7</version>
		</dependency>

 

2. 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="H2Tables" targetRuntime="MyBatis3Simple">
		<plugin
			type="org.mybatis.generator.plugins.FluentBuilderMethodsPlugin" />
		<plugin type="org.mybatis.generator.plugins.ToStringPlugin" />
		<plugin type="org.mybatis.generator.plugins.SerializablePlugin" />
		<plugin type="org.mybatis.generator.plugins.RowBoundsPlugin" />

		<jdbcConnection driverClass="org.h2.Driver"
			connectionURL="jdbc:h2:mem:testdb" userId="sa" password="">
		</jdbcConnection>

		<javaModelGenerator
			targetPackage="com.example.mybatis.generatedmodel"
			targetProject="./src/main/java">
			<property name="enableSubPackages" value="true" />
			<property name="trimStrings" value="true" />
		</javaModelGenerator>

		<sqlMapGenerator
			targetPackage="com.example.mybatis.generatedmapper"
			targetProject="./src/main/resources/generatedmapper">
			<property name="enableSubPackages" value="true" />
		</sqlMapGenerator>

		<javaClientGenerator type="ANNOTATEDMAPPER"
			targetPackage="com.example.mybatis.generatedmapper"
			targetProject="./src/main/java">
			<property name="enableSubPackages" value="true" />
		</javaClientGenerator>

		<table tableName="generatedstudent"
			domainObjectName="GeneratedStudent">
			<generatedKey column="id" sqlStatement="CALL IDENTITY()"
				identity="true" />
			<columnOverride column="money"
				javaType="org.joda.money.Money" jdbcType="BIGINT"
				typeHandler="com.example.mybatis.typehandler.MoneyTypeHandler" />
		</table>
	</context>
</generatorConfiguration>

 

3. java运行

	private void generateArtifacts() throws Exception {
		List<String> warnings = new ArrayList<>();
		ConfigurationParser cp = new ConfigurationParser(warnings);
		Configuration config = cp.parseConfiguration(this.getClass().getResourceAsStream("/generatorConfig.xml"));
		DefaultShellCallback callback = new DefaultShellCallback(true);
		MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
		myBatisGenerator.generate(null);
	}

 

4. 主要参数

targetRuntime,常用的有下面两个,其他的请参见官网

  • MyBatis3
  • MyBatis3Simple

 

三、分页工具

PageHelper  参考官网  https://pagehelper.github.io/

1. 引入依赖

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.12</version>
</dependency>

 

2. 使用

		Page<GeneratedStudent> startPage = PageHelper.startPage(1, 1);
		generatedStudentMapper.selectAll();
		log.info("selectAll: {}, page: {}", startPage.getResult(), startPage);

源码

Fork me on Gitee

转载于:https://my.oschina.net/tita/blog/3059573

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值