TKmybatis的使⽤,MyBatis的Mapper接⼝、Example⽅法
⽂章⽬录
TKmybatis的使⽤
1. l导⼊依赖
<!-- /batis/mapper -->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper</artifactId>
<version>4.1.5</version>
</dependency>
<!-- /batis/mapper-spring-boot-starter -->
<dependency>
<groupId&batis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
【注意】如果在使⽤tkmybatis之前,你已经集成过mybatis,有mybatis的起步依赖mapper-spring-boot-starter,那么需要注释mybatis起步依赖,或者不添加tkmybatis的mapper-spring-boot-starter,否则会出报错****
2. 实体类的相关配置,@Id、@Table、@Column等
TKmybatis的常⽤注解
注解作⽤
炒鱿鱼的英文@Table指定该实体类对应的数据库表名 @Table(name = "tb_sy_company_info")
@Id表⽰该字段对应数据库表的主键id
@GeneratedValue strategy表⽰使⽤数据库⾃带的主键⽣成策略,generator配置为"JDBC",在数据插⼊完毕之后,会⾃动将主键id填充到实体类中。@GeneratedValue(strategy = GenerationType.IDENTITY):主键⾃增
@Column属性名和列名不⼀致进⾏匹配(数据库表以下划线,类属性是驼峰式)@Transient注解忽略作⽤,不与数据库表任何字段对应
@Table(name ="tb_sy_company_info")//数据库的表
@Data//lombok 不⽤写get、t、tostring、构造⽅法等代码
//供货商(公司)表
public class Company {
//@Id注解:主键
//@GeneratedValue(strategy = GenerationType.IDENTITY):主键⾃增
/
/@Column注解:属性名和列名不⼀致匹配
//@Transient注解:忽略,不与数据库表任何字段对应
@Id
@Column(name ="company_id")
private String companyId;//主键(UUID),供应商名称
@Column(name="company_name")
private String companyName;//供货商名称
@Column(name ="company_contact")
private String companyContact;//供货商联系⼈
汽车空滤
@Column(name ="company_phone")
private String companyPhone;//供货商联系电话
@Column(name ="company_address")
private String companyAddress;//供货商地址
@Column(name ="company_email")
private String companyEmail;//供货商邮箱
@Column(name ="business_licen")
private String businessLicen;//营业执照照⽚地址(src)
@Column(name ="reprentative_id")
private String reprentativeId;//法⼈代表⾝份证证件号
@Column(name ="reprentative_name")
private String reprentativeName;//法⼈代表姓名
private Integer weight;//权重(根据权重排名显⽰)
盈余公积转增资本
@Column(name ="company_status")
private Integer companyStatus;//激活状态 0:停⽤,1:启⽤
@Column(name ="company_size")
private Integer companySize;//供货商公司规模
@Column(name ="create_time")
private Date createTime;//创建时间(申请通过时间)
@Column(name ="update_time")
private Date updateTime;//最后更新时间
private String remark;//备注(供货商的介绍信息)
@Transient
private boolean check;//忽略,不与数据库表对应(判断是否选中)
}
3. dao层继承Mapper接⼝,dao extends Mapper<T>
ity.Example;
public interface CompanyMapper extends Mapper<Company>{
//update、delete、lect、inrt⽅法可以全部省略,由tkmybatis实现
}
Mapper中的⽅法(dao继承可⽤)
Example example =new Example(JavaBean.class);
⽅法功能说明
String/Integer inrt(Javabean bean) thorws SQLException整条插⼊数据(返回值为ID)int inrtSelective(Javabean bean);有选择性的插⼊数据
int deleteByPrimaryKey(Integer id) thorws SQLException按表的主键删除数据
int deleteByExample(UrExample example) thorws SQLException按指定条件删除
long countByExample(UrExample example) thorws SQLException按条件查询满⾜条件的个数
Object lectByPrimaryKey(Integer id) thorws SQLException按主键查询,返回⼀个javabean对象List lectByExample(UrExample example) thorws SQLException按指定条件查询
List lectByExampleWithBLOGs(UrExample example) thorws SQLException 按指定条件查询(包括BLOB字段)。只有当数据表中的字段类型有为⼆进制的才会产⽣。
int updateByPrimaryKey(Javabean bean) thorws SQLException按主键更新
pianistint updateByPrimaryKeySelective(Javabean bean) thorws SQLException按主键更新值不为null的字段
int updateByExample(Javabean bean, UrExample example) thorws
SQLException
按条件更新example
int updateByExampleSelective(Javabean bean, UrExample example) thorws SQLException 按条件更新值不为null的字段
⽅法功能说明
4. 在启动类Application上使⽤@MapperScan()扫描Mapper接⼝
@SpringBootApplication
@MapperScan(baPackages ="line_mall.system.mapper")//扫描mapper包
public class OnlineMallApplication {
澳际public static void main(String[] args){
SpringApplication.run(OnlineMallApplication.class, args);
}
}
5. application.properties配置⽂件配置数据库属性
# 数据库配置
a的写法spring:
datasource: # hikari
type: com.zaxxer.hikari.HikariDataSource # 数据库类型,默认就是hikari(如果不改的话可以不写)
driver-class-name: sql.cj.jdbc.Driver # mysql 8 需要添加时区
urname: root
tracy韩国组合password: 861221293
url: jdbc:mysql://localhost:3306/onlinemall?uUnicode=true&characterEncoding=utf8&rverTimezone=GMT%2B8 # rverTimezone=GMT%2B8表⽰东8区,不然报错time zone
# mybatis配置
mybatis:
type-alias-package: line_mall.system.domain # 别名
mapper-locations: classpath*:com/xgf/online_mall/mapper/*.xml # u xml 使⽤xml配置的时候需要
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 开启mybatis⽇志
6. Service调⽤
@Service
public class CompanyServiceImpl implements ICompanyService {
@Autowired
CompanyMapper companyMapper;
@Override
public List<Company>findAll(){
Example example =new Example(Company.class);//查询条件
Example.Criteria criteria = ateCriteria();
//criteria.andEqualTo(company); //根据值来拼接出where 条件
//where by urname=?
//where by name=?
List<Company> companyList = companyMapper.lectByExample(example);
return companyList;
}
@Override
public void saveCompany(Company company){
try{
东野圭吾推理系列04
companyMapper.inrt(company);//添加
}catch(Exception e){
e.printStackTrace();
}
}
}
Example⽅法设置查询条件
example ⽤于添加条件,相当与sql语句中的where后⾯的条件部分。
Example.Criteria criteria = ateCriteria();
【注意】
1. 没写ateCriteria();,然后执⾏lectByExample(example)会查询全部数据。
2. example没加条件,执⾏查询语句lectByExample(example);也是查询全部。example⽅法,criteria⽅法说明
example.tOrderByClau(“字段名 ASC”);添加升序(ASC)排列条件,DESC为降
序。example.tOrderByClau("AFTER_CHECK_TIME DESC");
example.tDistinct(boolean b)去除重复,boolean类型,true:去重(⽆重复)criteria.andXxxIsNull添加字段xxx为null的条件
criteria.andXxxIsNotNull添加字段xxx不为null的条件
criteria.andXxxEqualTo(value)添加字段xxx等于value条件
criteria.andXxxNotEqualTo(value)添加字段xxx不等于value条件
criteria.andXxxGreaterThan(value)添加字段xxx⼤于value条件
criteria.andXxxGreaterThanOrEqualTo(value)添加字段xxx⼤于等于value条件
criteria.andXxxLessThan(value)添加字段xxx⼩于value条件
criteria.andXxxLessThanOrEqualTo(value)添加字段xxx⼩于等于value条件
criteria.andXxxIn(List<?>)添加字段xxx值在List<?>列表条件
criteria.andXxxNotIn(List<?>)添加字段xxx值不在List<?>条件
example⽅法,criteria⽅法说明
criteria.andXxxLike(“%”+value+”%”)添加字段xxx值为value的模糊查询条件criteria.andXxxNotLike(“%”+value+”%”)添加字段xxx值不为value的模糊查询条件criteria.andXxxBetween(value1,value2)添加字段xxx值在value1和value2之间条件criteria.andXxxNotBetween(value1,value2)添加字段xxx值不在value1和value2之间条件
7. 创建测试类测试增加查询
@SpringBootTest
public class TestCompanyServiceImpl {
@Autowired
CompanyServiceImpl companyService;
//测试添加
@Test
public void test01(){
Company company =new Company();
company.tCompanyId(UUID.randomUUID().toString());
company.tCompanyName("供货商名称");
company.tCompanyEmail("email邮箱");
company.tCompanyContact("供货商联系⼈");
company.tCompanyAddress("供货商地址");
company.tBusinessLicen("c://"+CompanyName()+"/"+"licen.jpg");
company.tReprentativeId("123456789987654321");
company.tWeight(2);
companyService.saveCompany(company);//保存数据
}
//测试查询所有数据
@Test
public void test02(){
节日歌曲List<Company> companyServiceAll = companyService.findAll();
System.out.println(companyServiceAll);
}
}