This action will force synchronization from 票牛/pndao, which will overwrite any changes that you have made since you forked the repository, and can not be recovered!!!
Synchronous operation will process in the background and will refresh the page when finishing processing. Please be patient.
一个非常简单的MyBatis辅助工具,可以基于DAO的命名约定帮你生成并维护SQL语句
pndao是票牛Java团队实践一年演化出来的工具。在实际生产中减少了80%以上的重复SQL编写工作,从而把关注力转移到模型本身的制定上。结合建表语句生成插件pngen,大部分场景只需编写一个模型类即可完成DAO层工作。
以下是一个常见的DAO功能:
public class UserDaoTest extends AbstractTest{
public static final int USER_ID = 1;
@Autowired
private UserDao userDao;
@Test
public void testInsertUser() throws Exception {
User user = initUser();
assertThat(userDao.insert(user)).isEqualTo(1);
}
@Test
public void testFindUserById() throws Exception {
User user = userDao.findById(USER_ID);
assertThat(user).isNotNull();
}
@Test
public void testUpdateUserName() throws Exception {
assertThat(userDao.updateForUserName("用户13700000001",USER_ID)).isEqualTo(1);
}
}
基于pndao,所有需要开发的DAO只有这些:
@DaoGen
public interface UserDao {
int updateForUserName(@Param("userName") String userName, @Param("id") int id);
int insert(User t);
User findById(int id);
}
<artifactId>pndao</artifactId>
<groupId>com.piaoniu</groupId>
<version>0.2.0</version>
使用前建议先阅读pndao-example中的样例代码。
支持的方法:
方法 | 说明 | 样例 | 备注 |
insert | 插入记录 | insert(User user) | ID会回写入user的主键 |
batchInsert | 批量插入记录 | batchInsert(List user) | 返回插入记录数 |
findBy | 按某列查询单个元素 | findByUserName(String userName) | 返回单条记录 |
queryBy | 按某列查询一些元素 | queryByUserName(String userName) | 返回多条记录 |
countBy | 按某列查询总数 | countBy(String userName) | 返回总数 |
update | 按照主键其他所有列 | update(User user) | 返回更新记录数 |
updateFor | 按照主键更新一列 | updateForUserName(String userName,int id) | 返回更新记录数 |
如果你的团队命名规范与pndao不同
可以下载源码,修改DaoGen的prefix的default值并重新编译。
可以通过指定编译期的 processor options
: -AtablePrefix=PN_
的方式修改默认表名前缀, 示例如下:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<compilerArgument>-AtablePrefix=PNTS_</compilerArgument>
</configuration>
</plugin>
pndao基于jsr269的注解编译生成,与lombok的原理是类似的,所以理论上可以为任意框架的生成配置型的代码。
项目目前仍在beta阶段,对于不同版本覆盖不全,欢迎建议、使用和提交代码。
票牛技术团队博客地址:https://piaoniu.io/。
Sign in for post a comment
Comments ( 0 )