首先创立所须要的数据库,还所须要创立所须要的表单,而后对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定义的自定义标签属性,用于设置文本内容.
发表回复