首先创立所须要的数据库,还所须要创立所须要的表单,而后对 application.properties 进行配置
server.port=80
spring.datasource.url=jdbc:mysql:///dbbrand?serverTimezone=GMT%2B8&characterEncoding=utf8
spring.datasource.username=root
spring.datasource.password=root
mybatis.mapper-locations=classpath:/mapper/*/*.xml
spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
logging.level.com.cy=debug
实现一个 pojo 对象
brand 素来封装从数据库传回的信息。
package com.cy.pj.brand.pojo;
import java.util.Date;
public class Brand {
private Integer id;
private String name;
private String logo;
private String remark;
private Date createdTime;
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 String getLogo() {return logo;}
public void setLogo(String logo) {this.logo = logo;}
public String getRemark() {return remark;}
public void setRemark(String remark) {this.remark = remark;}
public Date getCreatedTime() {return createdTime;}
public void setCreatedTime(Date createdTime) {this.createdTime = createdTime;}
@Override
public String toString() {
return "Brand{" +
"id=" + id +
", name='" + name + '''+", logo='"+ logo +''' +
", remark='" + remark + '''+", createdTime="+ createdTime +'}';
}
}
创立数据逻辑对象(DAO)
设计用于拜访 Brand 数据的数据拜访对象及办法
定义一个(BrandDao)
package com.cy.pj.brand.dao;
import com.cy.pj.brand.pojo.Brand;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface BrandDao {//@Select("select * from tb_brand where name like concat('%',#{name},'%')")
List<Brand> findBrands(String name);
}
@Mapper 注解是把创立 impl 的权力交给 spring 框架治理,impl 能够提取数据库里的数据并将其返回
业务逻辑对象(Service)
业务逻辑对象负责模块的具体业务解决, 例如参数校验, 事务管制, 权限管制, 日志记录等.
创立接口
package com.cy.pj.brand.service;
import com.cy.pj.brand.pojo.Brand;
import java.util.List;
public interface BrandService {List<Brand> findBrands(String name);
}
实现接口
package com.cy.pj.brand.service.impl;
import com.cy.pj.brand.dao.BrandDao;
import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class BrandServiceImpl implements BrandService {private static final Logger log= LoggerFactory.getLogger(BrandServiceImpl.class);
@Autowired
private BrandDao brandDao;
@Override
public List<Brand> findBrands(String name) {long t1 = System.currentTimeMillis();
List<Brand> list = brandDao.findBrands(name);
long t2 = System.currentTimeMillis();
log.info("time:{}", t2 - t1);
return list;
}
}
管制逻辑对象(Controller)
在管制逻辑对象中次要是负责申请和响应逻辑管制, 例如申请 url 映射, 参数映射, 申请形式, 后果集的封装, 解析, 响应的设计等。
package com.cy.pj.brand.controller;
import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import java.util.List;
@Controller
public class BrandController {
@Autowired
private BrandService brandService;
//http://localhost/brand/doFindBrands?name=tcl 传统形式
//http://localhost/brand/doFindBrands?name=tcl rest 格调,更好跨平台
@GetMapping("/brand/doFindBrands/{name}")
public String doFindBrands(@PathVariable(required = false) String name, Model model){List<Brand> list=brandService.findBrands(name);
model.addAttribute("list",list);
return "brand/brand";
}
}
客户端页面设计
<table>
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>createdTime</th>
</tr>
</thead>
<tbody>
<tr th:each="brand: ${list}">
<td th:text="${brand.id}">10</td>
<td th:text="${brand.name}">AAA</td>
<td th:text="${#dates.format(brand.createdTime,'yyyy/MM/dd
HH:mm')}">2020/10/11</td>
</tr>
</tbody>
</table>
其中:
1)${}为 thymeleaf 为中的 EL 表达式, 用于从服务端 model 中获取数据
2)th:each 为 thymeleaf 定义的自定义标签属性, 用于迭代数据.
3)th:text 为 thymeleaf 定义的自定义标签属性, 用于设置文本内容.