JavaWenb开发
pom.xml 文件 配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>tlias-web-management</artifactId><version>0.0.1-SNAPSHOT</version><name>tlias-web-management</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencies><!--web起步依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency> <!--mybatis起步依赖--><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency> <!--mysql驱动--><dependency><groupId>com.mysql</groupId><artifactId>mysql-connector-j</artifactId><scope>runtime</scope></dependency> <!--lombok--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency> <!-- <dependency>--> <!-- <groupId>org.slf4j</groupId>--> <!-- <artifactId>slf4j-log4j12</artifactId>--> <!-- <version>1.6.4</version>--> <!-- </dependency>--> <!--springboot单元测试--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies> <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build> </project>
链接数据库 和 驼峰命名
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
Controller 层
处理前端请求的数据
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@Autowiredprivate DeptService deptService; @GetMapping("/depts")public Result list(){log.info("查询全部部门数据");//调用ServiceList<Dept> deptList= deptService.list();return Result.success(deptList);} }
service 层
服务处理逻辑调用操作
服务层接口
package com.itheima.service; import com.itheima.pojo.Dept; import java.util.List; /*** 部门管理*/ public interface DeptService {/**查询全部员工数据* @return* */ List<Dept> list(); }
服务层实类
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 { @Autowiredprivate DeptMapper deptMapper; @Overridepublic List<Dept> list() {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")List<Dept> lidt(); }
实体类
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; //IDprivate 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 <Emp> page(@Param("start") Integer start,@Param("pageSize")Integer pageSize );
<!-- 分页查询 --><dependency><groupId>com.github.pagehelper</groupId><artifactId>pagehelper-spring-boot-starter</artifactId><version>1.4.6</version></dependency>
@Overridepublic PageBean page(Integer page, Integer pageSize) {// 1 分页查询PageHelper.startPage(page, pageSize);// 2 执行查询List<Emp> empList = empMaqqer.list();Page<Emp> p= (Page<Emp>)empList;// 3 封装pageBean 对象PageBean pageBean=new PageBean(p.getTotal(),p.getResult());return pageBean;}
文件上传
application.properties 文件名
阿里云oss