1.jar包地址:https://mvnrepository.com/artifact/com.baomidou/mybatis-plus/3.0.5
注意:引入 MyBatis-Plus 之后请不要再次引入 MyBatis 以及 MyBatis-Spring,以避免因版本差异导致的问题。
2.配置:
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="扫描你映射接口的包"/>
</bean>
<bean id="sessionFactoryBean" class="com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 这里是你分页插件需要的配置,如果不使用分页可以去掉-->
<property name="plugins">
<array>
<bean class="com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor">
</bean>
</array>
</property>
</bean>
以上就是spring集成mybatis-plus的配置了
Mybatis-plus会自动帮你操作单表的sql语句,所以单表的sql语句可以不用写了
1.接口类:
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ps.entity.Emp;
public interface EmpMapper extends BaseMapper<Emp> {
}
2.实体类:
// 当类名和表名不一致是,可以用来确定表
@TableName("emp")
public class Emp {
//@NotBlank(message = "编号不能为空")
/*主键*/
@TableId(type=IdType.AUTO)
private String empno;
private String ename;
private String job;
// 表格字段和类的属性字段不一致时可以确定表格字段
@TableField("sal")
private String mySal;
private String hiredate;
public String getEmpno() {
return empno;
}
public void setEmpno(String empno) {
this.empno = empno;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public String getJob() {
return job;
}
public void setJob(String job) {
this.job = job;
}
public String getMySal() {
return mySal;
}
public void setMySal(String mySal) {
this.mySal = mySal;
}
public String getHiredate() {
return hiredate;
}
public void setHiredate(String hiredate) {
this.hiredate = hiredate;
}
@Override
public String toString() {
return "Emp{" +
"empno='" + empno + '\'' +
", ename='" + ename + '\'' +
", job='" + job + '\'' +
", mysal='" + mySal + '\'' +
", hiredate='" + hiredate + '\'' +
'}';
}
service层实现类
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ps.entity.Emp;
import com.ps.entity.Result;
import com.ps.mapper.EmpMapper;
import com.ps.service.EmpService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmpServiceImpl implements EmpService {
// 自动装配映射的接口类
@Autowired
private EmpMapper empMapper;
@SuppressWarnings({ "rawtypes", "unchecked" })
public Result queryEmps(String checkName, String curPage, String pageNum) throws Exception {
int curPageIn = Integer.parseInt(curPage);// 页数
int pageNumIn = Integer.parseInt(pageNum);//每页的数据
// 条件构造器
QueryWrapper wrapper = new QueryWrapper();
// 相当于模糊查询
wrapper.like("ename", checkName);
// 这里是分页传入的参数:页数和数据量 // 条件构造器
IPage<Emp> selectPage = empMapper.selectPage(new Page(curPageIn,pageNumIn), wrapper);
Result result = new Result();
result.setCode(0);
result.setCount((int)selectPage.getTotal());
result.setData(selectPage.getRecords());
return result;
}
@SuppressWarnings("unchecked")
public void addEmp(Emp empValue) {
// 增加实体类中所有的字段
empMapper.insert(empValue);
}
@SuppressWarnings("unchecked")
public void updateEmp(Emp empValue,String userId) {
empValue.setEmpno(userId);
// 根据主键修改
empMapper.updateById(empValue);
}
public void delete(String userId) {
// 自动根据id删除
empMapper.deleteById(userId);
}
}