1.项目结构
2.pom.xml
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.0.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>io.projectreactor</groupId> <artifactId>reactor-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
3.pojo(Phone.java)
public class Phone { private Integer id; private String name; private Double price; private String color; private String productionDate; public Phone() { super(); } public Phone(Integer id, String name, Double price, String color, String productionDate) { this.id = id; this.name = name; this.price = price; this.color = color; this.productionDate = productionDate; } public Phone(String name, Double price, String color, String productionDate) { this.name = name; this.price = price; this.color = color; this.productionDate = productionDate; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } public String getProductionDate() { return productionDate; } public void setProductionDate(String productionDate) { this.productionDate = productionDate; } @Override public String toString() { return "Phone{" + "id=" + id + ", name='" + name + '\'' + ", price=" + price + ", color='" + color + '\'' + ", productionDate='" + productionDate + '\'' + '}'; } }
4.PhoneDao.java
import com.laola.phone2.pojo.Phone; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface PhoneDao { List<Phone> finAll(); void addPhone(Phone phone); void updatePhone(Phone phone); int deletePhone(Integer id); }
5.PhoneService.java
import com.laola.phone2.pojo.Phone; import java.util.List; public interface PhoneService { List<Phone> finAll(); void addPhone(Phone phone); void updatePhone(Phone phone); int deletePhone(Integer id); }
6.PhoneServiceImpl.java
import com.laola.phone2.dao.PhoneDao; import com.laola.phone2.pojo.Phone; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class PhoneServiceImpl implements PhoneService{ @Autowired private PhoneDao phoneDao; @Override public List<Phone> finAll() { return phoneDao.finAll(); } @Override public void addPhone(Phone phone) { phoneDao.addPhone(phone); } @Override public void updatePhone(Phone phone) { phoneDao.updatePhone(phone); } @Override public int deletePhone(Integer id) { return phoneDao.deletePhone(id); } }
7.PhoneController.java
import com.laola.phone2.pojo.Phone; import com.laola.phone2.service.PhoneService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; @Controller public class PhoneController { @Autowired private PhoneService phoneService; @PostMapping(value = "/addPhone") public String addPhone(Phone phone, HttpServletRequest request, HttpServletResponse response,ModelMap map)throws Exception{ request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8"); response.setCharacterEncoding("utf-8"); map.addAttribute("msg","你是个沙子"); phoneService.addPhone(phone); return "index"; } @GetMapping(value = "/finAll") @ResponseBody public List<Phone> findAll(){ return phoneService.finAll(); } @PutMapping(value = "updatePhone/{id}") @ResponseBody public void updatePhone(Phone phone){ phoneService.updatePhone(phone); } @DeleteMapping(value = "deletePhone/{id}") @ResponseBody public int deletePhone(@PathVariable("id") Integer id){ return phoneService.deletePhone(id); } }
8.PhoneMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.laola.phone2.dao.PhoneDao"> <sql id="column_list"> id,name,price,color,production_date </sql> <select id="finAll" resultType="com.laola.phone2.pojo.Phone"> select <include refid="column_list" /> from phone </select> <insert id="addPhone" > insert into phone (name,price,color,production_date) values (#{name},#{price},#{color},#{productionDate}) </insert> <update id="updatePhone"> update phone set name=#{name},price=#{price},color=#{color},production_date=#{productionDate} where id=#{id} </update> <delete id="deletePhone"> delete from phone where id=#{id} </delete> </mapper>
9.index.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <label th:text="${msg1}"></label> </body> </html>
10.applicatiion.properties
spring.datasource.url=jdbc:mysql://localhost:3306/phone
spring.datasource.username=root
spring.datasource.password=940521
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
mybatis.type-aliases-package=com.laola.phone2.pojo
mybatis.mapper-locations=classpath:mapper/*.xml
#启用驼峰命名
mybatis.configuration.map-underscore-to-camel-case=true
spring.http.encoding.charset=utf-8
11.说明:这里我只测试如何像jsp那样拿controller的信息,所以就用了一张index.html,其它业务测试用的postman,测试方法如下:
添加对应post请求:
查询对应get请求:
修改对应put请求:
删除对应delete请求:
12.遇到的问题:
a.springboot启动程序的文件位置最好按照图示放置,这是为了更好的扫描其下的dao,service,controller.
b.各层的注解如图上所示,尤其与@Autowired关联的@Service和@Repository注解.
c.注意启动入口的@MapperScan所扫描的dao的位置.
d.@Conrtoller可以用来跳转页面,结合@Responsebody使用,@RestController用来代替前面两个,但不能作跳转.
e.未解决的问题,前台url参数传入数据库后乱码,配置my.cnf,设置请求和响应编码,url参数携带指定编码都无效,有那位大神赐教.