Mybatis、TKMybatis 对比

1.Mybatis

(1)配置文件

  • mybatis.config-location=classpath:mybatis-config.xml,配置mybatis-config.xml路径,mybatis-config.xml中配置MyBatis基础属性,如果项目中配置了mybatis-config.xml文件需要设置该参数。
  • mybatis.mapper-locations=classpath*:mapper/**/*.xml,指定mapper文件夹
  • mybatis.type-handlers-package=geektime.spring.data.mybatisdemo.handler,不用再指定TypeHandler
  • mybatis.configuration.map-underscore-to-camel-case = true,将带有下划线的表字段映射为驼峰格式的实体类属性,省去了在mapper.xml文件中编写表字段列表与表实体类属性的映射关系,即resultMap。

(2)实体类

  • 使用 Mybatis 时,数据库表对应的实体类中,并没有使用任何注解
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class Coffee {
    private Long id;
    private String name;
    private Money price;
    private Date createTime;
    private Date updateTime;
}

(3)Mapper

@Mapper
public interface CoffeeMapper {
    @Insert("insert into t_coffee (name, price, create_time, update_time)"
            + "values (#{name}, #{price}, now(), now())")
    // 因为配置了mybatis.type-handlers-package,所以不用指定typeHandler
    // #{price, typeHandler=geektime.spring.data.mybatisdemo.handler.MoneyTypeHandler}
    @Options(useGeneratedKeys = true, keyColumn="id", keyProperty="id")
    int save(Coffee coffee);
	
    @Select("select * from t_coffee where id = #{id}")
    @Results({
            // 默认会根据名字映射
            // map-underscore-to-camel-case = true 可以实现一样的效果
            // 因为配置了mybatis.type-handlers-package,所以不用指定typeHandler
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "create_time", property = "createTime"),
            @Result(column = "update_time", property = "updateTime")
            // 因为配置了mybatis.type-handlers-package,所以不用指定typeHandler
            // @Result(column = "price", property = "price", typeHandler = geektime.spring.data.mybatisdemo.handler.MoneyTypeHandler.class),
    })
    Coffee findById(@Param("id") Long id);
}

(4)mybatis-config.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 配置 -->
	<!-- 配置 -->
	<settings>
		<!-- 一级缓存默认开启,只在同一个sqlSession之间共享,方法必须在同一个事务内,才会支持,执行commit()方法时,会清空本地缓存 -->
		<!-- 二级缓存默认开启,下面的配置可关闭,支持多个SqlSession之间共享缓存,如果两个SqlSession之间发生了更新操作,就不会去缓存拿数据了。如果在执行过程中,执行了多表操作,即如果A表和B表相关联,若对A表执行了更新操作,B表并不能够感知到,从而会拿到脏数据 -->
		<setting name="cacheEnabled" value="false"/>
		<!-- 日志实现 -->
		<setting name="logImpl" value="LOG4J2"/>
	</settings>

	<typeAliases>
	</typeAliases>
	
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"> 
			<property name="name" value="mybatis"/> 
		</plugin> 
	</plugins>
</configuration>

2.TKMybatis

(1)配置文件

  • mybatis.config-location=classpath:mybatis-config.xml,配置mybatis-config.xml路径,mybatis-config.xml中配置MyBatis基础属性,如果项目中配置了mybatis-config.xml文件需要设置该参数。
  • mybatis.mapper-locations=classpath*:mapper/**/*.xml,指定mapper文件夹
  • mybatis.type-handlers-package=geektime.spring.data.mybatisdemo.handler,不用再指定TypeHandler

(2)实体类

  • TkMybatis默认使用继承Mapper接口中传入的实体类对象去数据库寻找对应的表,因此如果表名与实体类名不满足对应规则时会报错。这时使用@Table为实体类指定表(这种对应规则为驼峰命名规则)。
@Data
@Table(name = "anc_company")  // 建立实体类和数据库表之间的对应关系
public class Company {
   @Id // 标记和数据库表中主键字段对应的实体类字段
    @Column(name = "company_name")  // 建立实体类字段和数据库表字段之间的对应关系
    @GeneratedValue(strategy = GenerationType.IDENTITY) // 通用Mapper在执行insert操作之后将数据库自动生成的主键值回写到实体类对象中
    private String companyName;

    @Column(name = "company_logo")
    private String companyLogo;

    @Column(name = "company_dec")
    @Transient  // 用于标记不与数据库表字段对应的实体类字段。
    private String companyDec;
}

(3)Mapper

public interface CompanyMapper extends BaseMapper<Company> {
}

(4)mybatis-config.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 配置 -->
	<!-- 配置 -->
	<settings>
		<!-- 一级缓存默认开启,只在同一个sqlSession之间共享,方法必须在同一个事务内,才会支持,执行commit()方法时,会清空本地缓存 -->
		<!-- 二级缓存默认开启,下面的配置可关闭,支持多个SqlSession之间共享缓存,如果两个SqlSession之间发生了更新操作,就不会去缓存拿数据了。如果在执行过程中,执行了多表操作,即如果A表和B表相关联,若对A表执行了更新操作,B表并不能够感知到,从而会拿到脏数据 -->
		<setting name="cacheEnabled" value="false"/>
		<!-- 日志实现 -->
		<setting name="logImpl" value="LOG4J2"/>
		<!-- 驼峰配置 -->
		<setting name="mapUnderscoreToCamelCase" value="true"/>
	</settings>

	<typeAliases>
	</typeAliases>
	
	<plugins>
		<plugin interceptor="com.github.pagehelper.PageInterceptor"> 
			<property name="name" value="mybatis"/> 
		</plugin> 
	</plugins>
</configuration>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值