pom.xml 文件 配置
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.5 com.itheima tlias-web-management 0.0.1-SNAPSHOT tlias-web-management Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-web org.mybatis.spring.boot mybatis-spring-boot-starter 2.2.2 com.mysql mysql-connector-j runtime org.projectlombok lombok true org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin org.projectlombok lombok
application.propertoen 文件配置
#驱动类名称 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver #数据库连接的url spring.datasource.url=jdbc:mysql://localhost:3306/mybatis #连接数据库的用户名 spring.datasource.username=root #连接数据库的密码 spring.datasource.password=123456 #配置mybatis的日志, 指定输出到控制台 mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl #开启mybatis的驼峰命名自动映射开关 a_column ------> aCloumn mybatis.configuration.map-underscore-to-camel-case=true
处理前端请求的数据
package com.itheima.controller; import com.itheima.pojo.Dept; import com.itheima.pojo.Result; import com.itheima.service.DeptService; import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * 部门管理Controller */ @Slf4j @RestController public class DeptController { //固定代码 也可也用注解 @Slf4j private static Logger log=LoggerFactory.getLogger(DeptController.class); // private static Logger log=LoggerFactory.getLogger(DeptController.class); // @RequestMapping(value = "/depts",method = RequestMethod.GET) //指定请求方式get @Autowired private DeptService deptService; @GetMapping("/depts") public Result list(){ log.info("查询全部部门数据"); //调用Service ListdeptList= deptService.list(); return Result.success(deptList); } }
服务处理逻辑调用操作
服务层接口
package com.itheima.service; import com.itheima.pojo.Dept; import java.util.List; /** * 部门管理 */ public interface DeptService { /* *查询全部员工数据 * @return * */ Listlist(); }
package com.itheima.service.impl; import com.itheima.mapper.DeptMapper; import com.itheima.pojo.Dept; import com.itheima.service.DeptService; import org.apache.ibatis.annotations.Mapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class DeptServiceImpl implements DeptService { @Autowired private DeptMapper deptMapper; @Override public Listlist() { return deptMapper.lidt(); } //部门管理 }
数据库调用(mybatis)跟数据库链接层
package com.itheima.mapper; import com.itheima.pojo.Dept; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select; import java.util.List; /** * 部门管理 */ @Mapper public interface DeptMapper { //查询前部部门数据 @Select("select * from tlias.dept") Listlidt(); }
实体类
package com.itheima.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import java.time.LocalDateTime; /** * 部门实体类 */ @Data @NoArgsConstructor @AllArgsConstructor public class Dept { private Integer id; //ID private String name; //部门名称 private LocalDateTime createTime; //创建时间 private LocalDateTime updateTime; //修改时间 }
package com.itheima.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @NoArgsConstructor @AllArgsConstructor public class Result { private Integer code;//响应码,1 代表成功; 0 代表失败 private String msg; //响应信息 描述字符串 private Object data; //返回的数据 //增删改 成功响应 public static Result success(){ return new Result(1,"success",null); } //查询 成功响应 public static Result success(Object data){ return new Result(1,"success",data); } //失败响应 public static Result error(String msg){ return new Result(0,msg,null); } }
报错
报错代码
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.binding.BindingException: Parameter 'start' not found. Available parameters are [arg0, pageSize, param1, param2]] with root cause
@Select("select * from emp limit #{start},#{pageSize}")
public List page(@Param("start") Integer start,@Param("pageSize")Integer pageSize );
com.github.pagehelper
pagehelper-spring-boot-starter
1.4.6
@Override
public PageBean page(Integer page, Integer pageSize) {
// 1 分页查询
PageHelper.startPage(page, pageSize);
// 2 执行查询
List empList = empMaqqer.list();
Page p= (Page)empList;
// 3 封装pageBean 对象
PageBean pageBean=new PageBean(p.getTotal(),p.getResult());
return pageBean;
}