博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring boot 初识实例及问题小结
阅读量:4983 次
发布时间:2019-06-12

本文共 7363 字,大约阅读时间需要 24 分钟。

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 {    List
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
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;@Servicepublic class PhoneServiceImpl implements  PhoneService{    @Autowired    private PhoneDao phoneDao;    @Override    public List
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;@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 List
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

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参数携带指定编码都无效,有那位大神赐教.

转载于:https://www.cnblogs.com/jiujianyaoge/p/11018219.html

你可能感兴趣的文章
java.lang.outofmemoryerror android
查看>>
coding
查看>>
省市联级(DataReader绑定)
查看>>
20165219 课上内容补做
查看>>
Tomcat7.0与Oracle10数据库连接池配置
查看>>
解决webpack和gulp打包js时ES6转译ES5时Object.assign()方法没转译成功的问题
查看>>
字节流与字符流的区别详解(转)
查看>>
类操作数据库
查看>>
找球号(一)
查看>>
oracle ebs 笔记
查看>>
Android studio使用git-android学习之旅(79)
查看>>
eclipse中去掉Js/javsscript报错信息
查看>>
网络中,FIFO、LRU、OPT这三种置换算法的缺页次数
查看>>
随机森林算法参数调优
查看>>
read命令读取用户输入
查看>>
Mysql编写定时任务事件
查看>>
路由器/交换机/集线器的区别收集(转)
查看>>
今日头条面试题汇总
查看>>
hdu 1305 Immediate Decodability
查看>>
基本数据类型
查看>>