自动生成mapper和domain,可以减少写sql语句的时间,一般的逻辑都可以使用,复杂的逻辑当然还是需要我们自己来写sql语句的,只是一些基本的增删改查就不需要浪费时间去写了,能够大大的提高效率,把重心放在代码逻辑上。
先来看结果
//实体类
@ApiModel(value="com.example.springboot_shiro.domain.test.User")
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
public class User extends UserExt {
/**
*
*/
@ApiModelProperty(value="id")
private Integer id;
/**
*
*/
@ApiModelProperty(value="username")
private String username;
/**
*
*/
@ApiModelProperty(value="password")
private String password;
/**
*
*/
@ApiModelProperty(value="salt")
private String salt;
}
//userExample 即使用Criteria增加查询条件所用的
public class UserExample {
protected String orderByClause;
protected boolean distinct;
protected List<Criteria> oredCriteria;
public UserExample() {
oredCriteria = new ArrayList<Criteria>();
}
public void setOrderByClause(String orderByClause) {
this.orderByClause = orderByClause;
}
public Criteria createCriteria() {
Criteria criteria = createCriteriaInternal();
if (oredCriteria.size() == 0) {
oredCriteria.add(criteria);
}
return criteria;
}
public Criteria andIdBetween(Integer value1, Integer value2) {
addCriterion("id between", value1, value2, "id");
return (Criteria) this;
}
public Criteria andIdNotBetween(Integer value1, Integer value2) {
addCriterion("id not between", value1, value2, "id");
return (Criteria) this;
}
········等等,由于文件太长就不全部展示了
}
//UserMapper
@Mapper
@Repository
public interface UserMapper {
long countByExample(UserExample example);
int deleteByExample(UserExample example);
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
List<User> selectByExample(UserExample example);
User selectByPrimaryKey(Integer id);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
}
UserMapper.xml
下面记录一下如何实现
1.添加依赖
//pom.xml
<!--mysql依赖 -->
<dependency>