首先创建所需要的数据库,还所需要创建所需要的表单,然后对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 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 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 findBrands(String name) {
long t1 = System.currentTimeMillis();
List 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 list=brandService.findBrands(name);
model.addAttribute("list",list);
return "brand/brand";
}
}
客户端页面设计
id
name
createdTime
10
AAA
2020/10/11
其中:
1)${}为thymeleaf为中的EL表达式,用于从服务端model中获取数据
2)th:each为thymeleaf定义的自定义标签属性,用于迭代数据.
3)th:text为thymeleaf定义的自定义标签属性,用于设置文本内容.