提醒:如果实体类中没有一个标记 @Id 的字段,当你使用带有 ByPrimaryKey 的方法时,所有的字段会作为联合主键来使用,也就会出现类似 where id = ? and countryname = ? and countrycode = ?的情况。
第四章会介绍代码生成器,可以自动生成上面的实体和下面的接口代码
通用 Mapper 提供了大量的通用接口,这里以最常用的 Mapper 接口为例
该实体类对应的数据库操作接口如下:
import tk.mybatis.mapper.common.Mapper;
public interface CountryMapper extends Mapper {
}
只要配置 MyBatis 时能注册或者扫描到该接口,该接口提供的方法就都可以使用。
该接口默认继承的方法如下:
selectOne
select
selectAll
selectCount
selectByPrimaryKey
方法太多,省略其他…
从 MyBatis 中获取该接口后就可以直接使用:
//从 MyBatis 或者 Spring 中获取 countryMapper,然后调用 selectAll 方法
List countries = countryMapper.selectAll();
//根据主键查询
Country country = countryMapper.selectByPrimaryKey(1);
//或者使用对象传参,适用于1个字段或者多个字段联合主键使用
Country query = new Country();
query.setId(1);
country = countryMapper.selectByPrimaryKey(query);
如果想要增加自己写的方法,可以直接在 CountryMapper 中增加。
使用纯接口注解方式时
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;
public interface CountryMapper extends Mapper {
@Select("select * from country where countryname = #{countryname}")
Country selectByCountryName(String countryname);
}
这里只是举了个简单的例子,可以是很复杂的查询。
如果使用 XML 方式,需要提供接口对应的 XML 文件
例如提供了 CountryMapper.xml 文件,内容如下:
在接口中添加对应的方法:
import tk.mybatis.mapper.common.Mapper;
public interface CountryMapper extends Mapper {
Country selectByCountryName(String countryname);
}
在接口中添加其他方法的时候和只用 MyBatis 是完全一样的,但是需要注意,在对应的 XML 中,不能出现和继承接口中同名的方法!
List selectByExample(Object example);
int updateByExampleSelective(@Param("record") T record, @Param("example") Object example);
所有 Example 方法中的 example 类型都是 Object 类型,这是因为通用 Mapper 支持所有符合 Example 结构的参数,例如通过 MBG 生成的 CountryExample、UserExample 类。还有通用 Mapper 中提供的通用 Example,以及支持 Java8 方法引用的 Weekend 类型。
配置中有一个和 Example 有关的参数,点击查看 3.14 checkExampleEntityClass。
5.1 MBG 生成的 Example
用法如下:
CountryExample example = new CountryExample();
example.createCriteria().andCountrynameLike("A%");
example.or().andIdGreaterThan(100);
example.setDistinct(true);
int count = mapper.deleteByExample(example);
对于的 SQL 日志如下:
DEBUG [main] - ==> Preparing: DELETE FROM country WHERE ( countryname like ? ) or ( Id > ? )
DEBUG [main] - ==> Parameters: A%(String), 100(Integer)
DEBUG [main] - <== Updates: 95
Example example = new Example(Country.class);
example.setForUpdate(true);
example.createCriteria().andGreaterThan("id", 100).andLessThan("id",151);
example.or().andLessThan("id", 41);
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT id,countryname,countrycode FROM country WHERE ( id > ? and id < ? ) or ( id < ? ) ORDER BY id desc FOR UPDATE
DEBUG [main] - ==> Parameters: 100(Integer), 151(Integer), 41(Integer)
DEBUG [main] - <== Total: 90
5.2.2 动态 SQL
示例:
Example example = new Example(Country.class);
Example.Criteria criteria = example.createCriteria();
if(query.getCountryname() != null){
criteria.andLike("countryname", query.getCountryname() + "%");
}
if(query.getId() != null){
criteria.andGreaterThan("id", query.getId());
}
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT id,countryname,countrycode FROM country WHERE ( countryname like ? ) ORDER BY id desc
DEBUG [main] - ==> Parameters: China%(String)
DEBUG [main] - <== Total: 1
5.2.3 排序
示例:
Example example = new Example(Country.class);
example.orderBy("id").desc().orderBy("countryname").orderBy("countrycode").asc();
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT id,countryname,countrycode FROM country order by id DESC,countryname,countrycode ASC
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 183
5.2.4 去重
示例:
CountryExample example = new CountryExample();
//设置 distinct
example.setDistinct(true);
example.createCriteria().andCountrynameLike("A%");
example.or().andIdGreaterThan(100);
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT distinct id,countryname,countrycode FROM country WHERE ( countryname like ? ) or ( Id > ? ) ORDER BY id desc
DEBUG [main] - ==> Parameters: A%(String), 100(Integer)
DEBUG [main] - <== Total: 95
5.2.5 设置查询列
示例:
Example example = new Example(Country.class);
example.selectProperties("id", "countryname");
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT id , countryname FROM country ORDER BY id desc
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 183
除了这里提到的方法外,还有很多其他的方法,可以查看 Example 源码进行了解。
5.3 Example.builder 方式
示例:
Example example = Example.builder(Country.class)
.select("countryname")
.where(Sqls.custom().andGreaterThan("id", 100))
.orderByAsc("countrycode")
.forUpdate()
.build();
List countries = mapper.selectByExample(example);
日志:
DEBUG [main] - ==> Preparing: SELECT countryname FROM country WHERE ( id > ? ) order by countrycode Asc FOR UPDATE
DEBUG [main] - ==> Parameters: 100(Integer)
DEBUG [main] - <== Total: 83
官方参考文档:
https://github.com/abel533/Mapper
四、通用IService&ServiceImpl
package cn.nyse.test.service;
import cn.nyse.entity.User;
import org.apache.ibatis.session.RowBounds;
import java.util.List;
public interface IService {
int deleteByPrimaryKey(Object o);
int delete(T t);
int insert(T t);
int insertSelective(T t);
boolean existsWithPrimaryKey(Object o);
List selectAll();
User selectByPrimaryKey(Object o);
int selectCount(T t);
List select(T t);
User selectOne(T t);
int updateByPrimaryKey(T t);
int updateByPrimaryKeySelective(T t);
int deleteByExample(Object o);
List selectByExample(Object o);
int selectCountByExample(Object o);
User selectOneByExample(Object o);
int updateByExample(T t, Object o);
int updateByExampleSelective(T t, Object o);
List selectByExampleAndRowBounds(Object o, RowBounds rowBounds);
List selectByRowBounds(T t, RowBounds rowBounds);
}
package cn.nyse.test.service.impl;
import cn.nyse.test.service.IService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
public class ServiceImpl implements IService {
@Autowired
Mapper mapper;
@Override
public int deleteByPrimaryKey(Object o) {
return mapper.deleteByPrimaryKey(o);
}
@Override
public int delete(T t) {
return mapper.delete(t);
}
@Override
public int insert(T t) {
return mapper.insert(t);
}
@Override
public int insertSelective(T t) {
return mapper.insertSelective(t);
}
@Override
public boolean existsWithPrimaryKey(Object o) {
return mapper.existsWithPrimaryKey(o);
}
@Override
public List selectAll() {
return mapper.selectAll();
}
@Override
public T selectByPrimaryKey(Object o) {
return mapper.selectByPrimaryKey(o);
}
@Override
public int selectCount(T t) {
return mapper.selectCount(t);
}
@Override
public List select(T t) {
return mapper.select(t);
}
@Override
public T selectOne(T t) {
return mapper.selectOne(t);
}
@Override
public int updateByPrimaryKey(T t) {
return mapper.updateByPrimaryKey(t);
}
@Override
public int updateByPrimaryKeySelective(T t) {
return mapper.updateByPrimaryKeySelective(t);
}
@Override
public int deleteByExample(Object o) {
return mapper.deleteByExample(o);
}
@Override
public List selectByExample(Object o) {
return mapper.selectByExample(o);
}
@Override
public int selectCountByExample(Object o) {
return mapper.selectCountByExample(o);
}
@Override
public T selectOneByExample(Object o) {
return mapper.selectOneByExample(o);
}
@Override
public int updateByExample(T t, Object o) {
return mapper.updateByExample(t,o);
}
@Override
public int updateByExampleSelective(T t, Object o) {
return mapper.updateByExampleSelective(t,o);
}
@Override
public List selectByExampleAndRowBounds(Object o, RowBounds rowBounds) {
return mapper.selectByExampleAndRowBounds(o,rowBounds);
}
@Override
public List selectByRowBounds(T t, RowBounds rowBounds) {
return mapper.selectByRowBounds(t,rowBounds);
}
}
java的多态性是指main方法在调用属性的时候类可以对这一属性做出反应的情况
//package 1;
class A{
public void test(){
System.out.println("A");
}
}
class D extends A{
public void test(){
S
参考了网上的思路,写了个Java版的:
public class Fibonacci {
final static int[] A={1,1,1,0};
public static void main(String[] args) {
int n=7;
for(int i=0;i<=n;i++){
int f=fibonac
1、查看系统客户端,数据库,连接层的编码
查看方法: http://daizj.iteye.com/blog/2174993
进入mysql,通过如下命令查看数据库编码方式: mysql> show variables like 'character_set_%'; +--------------------------+------
public class MyQueue {
private long[] arr;
private int front;
private int end;
// 有效数据的大小
private int elements;
public MyQueue() {
arr = new long[10];
elements = 0;
front
A binary search tree (BST) is a binary tree where each node has a Comparable key (and an associated value) and satisfies the restriction that the key in any node is larger than the keys in all