1.新建项目 选择JDBC API / Mybatis Framework /MySQL Driver/Spring Boot Dev Tools /Spring Web/Thymeleaf
2.导入文件application.properties
server.port=8888
server.servlet.context-path=/exec200330
spring.datasource.name=ds
spring.datasource.url=jdbc:mysql://localhost:3306/project2?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.type=org.apache.commons.dbcp.BasicDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.mapper-locations=classpath:mapper/*.xml
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**
spring.servlet.multipart.max-file-size=10MB
spring.servlet.multipart.max-request-size=20MB
在这里插入代码片
3.导入pom.xml文件。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
com.neuedu
exec200330
0.0.1-SNAPSHOT
exec200330
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.apache.tomcat
tomcat-jdbc
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.2
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
commons-dbcp
commons-dbcp
1.4
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-maven-plugin
4.拷贝EmployeeMapper.xml文件
5.把要想写的对象,进行封装新建com.neuedu.po 包 新建EmployeePO.java类
package com.neuedu.po;
import java.sql.Date;
public class EmployeePO {
private Integer empid;
private String ename;
private Date hiredate;
private DeptPO dept;
public Integer getEmpid() {
return empid;
}
public void setEmpid(Integer empid) {
this.empid = empid;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
public DeptPO getDept() {
return dept;
}
public void setDept(DeptPO dept) {
this.dept = dept;
}
}
6.新建com.neuedu.mapper包 新建 EmployeeMapper.java 类
package com.neuedu.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.neuedu.po.EmployeePO;
@Mapper
public interface EmployeeMapper {
public List getEmployeeList();
}
7.新建com.neuedu.service包 新建EmployeeService.java类
package com.neuedu.service;
import java.util.List;
import com.neuedu.po.EmployeePO;
public interface EmployeeService {
public List getEmployeeList();
}
8.新建com.neuedu.service.impl 包 新建 EmployeeServiceImpl.java类
package com.neuedu.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.neuedu.mapper.EmployeeMapper;
import com.neuedu.po.EmployeePO;
import com.neuedu.service.EmployeeService;
@Service
public class EmployeeServiceImpl implements EmployeeService{
@Autowired
private EmployeeMapper mapper;
@Override
public List getEmployeeList() {
// TODO Auto-generated method stub
return mapper.getEmployeeList();
}
}
9.新建com.neuedu.controller 包 新建EmployeeHandler.java类
package com.neuedu.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.neuedu.po.EmployeePO;
import com.neuedu.service.EmployeeService;
@RestController
public class EmployeeHandler {
@Autowired
private EmployeeService es;
@RequestMapping("/emp/list")
public List getEmployeeList(){
return es.getEmployeeList();
}
}
10.最后打开spring boot APP 测试程序