文章目录
- 1.Mybatis
- (1)配置文件
- (2)实体类
- (3)Mapper
- (4)mybatis-config.xml
- 2.TKMybatis
- (1)配置文件
- (2)实体类
- (3)Mapper
- (4)mybatis-config.xml
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())")@Options(useGeneratedKeys = true, keyColumn="id", keyProperty="id")int save(Coffee coffee);@Select("select * from t_coffee where id = #{id}")@Results({@Result(id = true, column = "id", property = "id"),@Result(column = "create_time", property = "createTime"),@Result(column = "update_time", property = "updateTime")})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><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) 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><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>