1.项目结构
2.pom.xml
org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-webflux org.mybatis.spring.boot mybatis-spring-boot-starter 2.0.1 mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test io.projectreactor reactor-test test org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-thymeleaf org.apache.tomcat.embed tomcat-embed-jasper provided
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;@Repositorypublic interface PhoneDao { ListfinAll(); 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 { ListfinAll(); 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;@Servicepublic class PhoneServiceImpl implements PhoneService{ @Autowired private PhoneDao phoneDao; @Override public ListfinAll() { 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;@Controllerpublic 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 ListfindAll(){ 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
id,name,price,color,production_date insert into phone (name,price,color,production_date) values (#{name},#{price},#{color},#{productionDate}) update phone set name=#{name},price=#{price},color=#{color},production_date=#{productionDate} where id=#{id} delete from phone where id=#{id}
9.index.html
Title
10.applicatiion.properties
spring.datasource.url=jdbc:mysql://localhost:3306/phonespring.datasource.username=rootspring.datasource.password=940521spring.datasource.driver-class-name=com.mysql.cj.jdbc.Drivermybatis.type-aliases-package=com.laola.phone2.pojomybatis.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参数携带指定编码都无效,有那位大神赐教.