数据持久化(Dao---Entity---mapper)
配置(application.yml)
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/wiki?useUnicode=true&characterEncoding=utf8&autoReconnect=true&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC&useSSL=true
mapper-locations: classpath:/mapper/*.xml
写创建库表语句
drop table if exists `demo`;
`id` bigint not null comment 'id',
`name` varchar(50) comment '名称',
`other_name` vachar(50) comment '代替名',
) engine = innodb default charset utf8mb4 comment ='测试';
insert into `demo` (id, name,other_name)VALUES (1, '测试', 'text');
写相应的实体
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
public class DemoEntity {
private String otherName;
public class DemoEntity {
private String otherName;
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 getOtherName() { return otherName; }
public void setOtherName(String otherName) { this.otherName = otherName; }
public DemoEntity(Integer id, String name, String otherName) {
this.otherName = otherName;
写mapper
mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace= "com.jiawa.wiki.dao.DemoDao">
<resultMap id ="dateMap" type="com.jiawa.wiki.domain.DemoEntity">
<id column="id" property="id"/>
<result column="name" property="name"/>
<result column="password" property="password"/>
<select id="queryAllDemoDate" resultType="com.jiawa.wiki.domain.DemoEntity">
写dao接口
import com.jiawa.wiki.domain.DemoEntity;
import org.apache.ibatis.annotations.Mapper;
public interface DemoDao {
服务层对数据持久层的数据做处理
service
import com.jiawa.wiki.dao.DemoDao;
import com.jiawa.wiki.domain.DemoEntity;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
public class DemoService {
public List selectAllDateDemo(){
List DemoEntities = DemoDao.queryAllDemoDate();
控制层接收服务层提供的数据,或者向服务层传递前端的数据
controller
import com.jiawa.wiki.domain.DemoEntity;
import com.jiawa.wiki.service.DemoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
public class DemoController {
private DemoService DemoService;
@RequestMapping(value = "/hello", method = RequestMethod.GET)
@RequestMapping(value = "/hello/post", method = RequestMethod.POST)
public String HelloPost(String name) {
return "Hello world " +name;
@RequestMapping(value = "/hello/queryAll", method = RequestMethod.GET)
public List queryAllDateDemo() {
return DemoService.selectAllDateDemo();