近日做系统,由于选择了mybatis作为后端的ORM,所以在纠结到底用注解annotation的方式好呢,还是使用xml配置的方式。
为此,查询了很多资料。感觉大部分都在推荐xml配置方式,并且我是诚心的去用annotation的,毕竟想顺应时代嘛,结果死活就是找不到。
最后找到了一个方法,其实感觉这个方法是在走我很早以前的老路,在还没使用ORM的时代,我们通常自己来做非空判断并且拼接sql语句
请看代码
ContactMapper.java
@SelectProvider(type=ContactSqlProvider.class, method="selectByExample")@Results({@Result(column="idcontact", property="idcontact", jdbcType=JdbcType.INTEGER, id=true),@Result(column="contacttype_idcontacttype", property="contacttypeIdcontacttype", jdbcType=JdbcType.INTEGER),@Result(column="username", property="username", jdbcType=JdbcType.VARCHAR),@Result(column="password", property="password", jdbcType=JdbcType.VARCHAR),@Result(column="userlocale", property="userlocale", jdbcType=JdbcType.VARCHAR),@Result(column="usersetting", property="usersetting", jdbcType=JdbcType.VARCHAR),@Result(column="retired", property="retired", jdbcType=JdbcType.BIT),@Result(column="department_iddepartment", property="departmentIddepartment", jdbcType=JdbcType.INTEGER)})List<Contact> selectByExample(ContactExample example);
ContactSqlProvider
public String selectByExample(ContactExample example) {BEGIN();if (example != null && example.isDistinct()) {SELECT_DISTINCT("idcontact");} else {SELECT("idcontact");}SELECT("contacttype_idcontacttype");SELECT("username");SELECT("password");SELECT("userlocale");SELECT("usersetting");SELECT("retired");SELECT("department_iddepartment");FROM("contact");applyWhere(example, false);if (example != null && OrderByClause() != null) {ORDER_OrderByClause());}return SQL();}
ContactDao
public List<Contact> searchByExample(Contact contact) {System.out.println("searchByExampleContact");ContactExample example = new ContactExample();ContactExample.Criteria cri = ateCriteria();System.out.Username() + ":" + Password());if (!Username().equals("") && Username() != null)cri.Username());if (!Password().equals("") && Password() != null)cri.Password());ContactMapper vcontactMapper = Mapper(ContactMapper.class);List<Contact> returnList = vcontactMapper.selectByExample(example);return returnList;}
</pre>其实这个也并不说不好,就是觉得怪怪的。<p>而去访问了mybatis的官网,关于动态sql的guide还是推崇xml的方式的</p><p>.html</p><pre name="code" class="html"><select id="findActiveBlogLike"resultType="Blog">SELECT * FROM BLOG WHERE state = ‘ACTIVE’<if test="title != null">AND title like #{title}</if><if test="author != null and author.name != null">AND author_name like #{author.name}</if>
</select>
我持保留意见。我过去一直使用hibernate的,不知道是个人技艺不精还是别的原因。总觉得速度效率上有点滞后。
有愿意讨论了,欢迎过来拍砖。
本文发布于:2024-01-31 00:10:37,感谢您对本站的认可!
本文链接:https://www.4u4v.net/it/170663104023831.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |