直接上干货:
例如一个叫xxx的项目,yml文件里加上这段
xml
xxxproject:
db:
xxxTable: xxx_dbname #自定义的数据库表名
创一个Configuration类放表名和Mapper
java
// XxxProjectAutoConfiguration.java
@Configuration
@MapperScan(basePackages = "cn.com.xxxproject.db.mapper")
public class XxxProjectAutoConfiguration {
@Value("${xxxproject.db.xxxTable}")
private String xxxTable;
@Autowired
private XxxMapper xxxMapper;
@Bean
public DatabaseManager databaseManager() {
return new DatabaseManager (xxxTable, xxxMapper);
}
}
看到这聪明的你应该已经知道后续该怎么做了
java
// DatabaseManager.java
public class DatabaseManager extends AbstractManager<XxxDO> {
private static final String DEFAULT_LOCK_TABLE_NAME = "xxx_dbname";
private final XxxMapper xxxMapper;
private final String localIp;
private final String xxxTable;
public DatabaseLockManager(String xxxTable, XxxMapper xxxMapper) {
this.xxxMapper = xxxMapper;
this.localIp = getLocalIp();
if (StringUtils.isNullOrEmpty(xxxTable)) {
xxxTable = DEFAULT_LOCK_TABLE_NAME;
}
this.xxxTable = xxxTable;
}
// 随便写个方法,根据自己需求来定制
@Override
protected boolean isExists(Long id) {
return xxxMapper.isExists(xxxTable, id);
}
@Override
protected XxxDO selectForUpdate(String xxxKey) {
return xxxMapper.selectForUpdate(xxxTable, xxxKey);
}
}
xml
<!-- LockMapper.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="cn.com.xxxproject.db.mapper.XxxMapper">
</mapper>
java
// LockMapper.java
@Mapper
public interface LockMapper {
@Select("select * from ${xxxTable} where id = #{id}")
@Results(id = "XxxMap",
value = { @Result(property = "id", column = "id"), @Result(property = "xxxKey", column = "xxx_key"),
@Result(property = "nodeInfo", column = "node_info"), @Result(property = "count", column = "count"),
@Result(property = "version", column = "version"),
@Result(property = "createTime", column = "create_time"),
@Result(property = "updateTime", column = "update_time") })
boolean isExists(String xxxTable, Long id);
@Select("select * from ${xxxTable} where xxx_key= #{xxxKey} for update")
@ResultMap("XxxMap")
XxxDO selectForUpdate(String xxxTable, String xxxKey);
}
完事!
顺便放两个关于Mybatis的好文章:
mybatis注解在方法直接编写SQL@Select@Insert@Update@Delete
第一篇是偏入门操作,第二篇的4.3多表查询真的适合反复细品
本篇用到:结果集映射相关注解
1、通过@Results、@Result注解定义结果集映射
如果实体字段的名称与数据库表字段名称不一致时,我们就需要显式的指定映射关系。这是通过@Results、@Result注解来指定的,例如为UserMapper的selectById指定映射关系:
java
@Select("SELECT id,name FROM user where id= #{id}")
@Results(id = "userMap", value = {
@Result(property = "id", column = "id", javaType = Integer.class,jdbcType = JdbcType.INTEGER,id = true),
@Result(property = "name", column = "name",javaType = String.class,jdbcType = JdbcType.VARCHAR)})
public User selectById(int id);
其中:
@Results注解:id属性用于给这个映射关系起一个名字(这里指定的为userMap),其内部还包含了一个@Result[]来表示实体属性和数据库表字段的映射关系
@Result注解:property属性是java实体属性的名称,column表示对应的数据库字段的名称。javaType和JdbcType属性可以不指定。
2、通过@ResultMap复用结果集映射
上述方法electById方法已经通过@Results注解指定了结果映射关系,可以通过@ResultMap来引用@Results的id属性值进行复用。如在UserMapper的selectAll方法进行复用:
java
@Select("SELECT id,name FROM user")
@ResultMap("userMap")
public List<User> selectAll();
THX~