1.新建springboot项目 创建时可以勾上必须的依赖
如spring web组件 其他的依赖也可后续pom慢慢增加
2.pom文件引入对应数据库驱动和mybatis依赖
检查pom.xml,缺少对应依赖补上
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.2</version>
</dependency>
3.yml文件增加数据库连接,mybatis 配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://xxxx/cloudpes?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: xxx
password: xxx123
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
#配置mapper的映射文件的位置
mybatis:
mapper-locations: classpath:mappers/*Mapper.xml
src/main/resources下增加mappers文件夹
4.PesappserviceApplication.java配置mybatis扫描路径
package com.wying.myspringbootframework;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.wying.myspringbootframework.dao")
public class MyspringbootframeworkApplication {
public static void main(String[] args) {
SpringApplication.run(MyspringbootframeworkApplication.class, args);
}
}
5.新建controller service dao dto entiry mapper.xml测试
我使用的mybatis generator工具生成的mapper.xml dao entity,手动写也行
5.1 entity层 新增YyptTestEntity.java文件
com.wying.myspringbootframework.entity.YyptTestEntity.java
package com.wying.myspringbootframework.entity;
import java.util.Date;
/**
* description:测试entity
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
public class YyptTestEntity {
private Integer csid;
private Integer xtsb;
private String csmc;
private String csz;
private String mrz;
private String csms;
private String cskxz;
private String jgid;
private String tjjgcsh;
private String yyjgcsh;
private String createid;
private Date createtime;
private String modifyid;
private Date modifytime;
public Integer getCsid() {
return csid;
}
public void setCsid(Integer csid) {
this.csid = csid;
}
public Integer getXtsb() {
return xtsb;
}
public void setXtsb(Integer xtsb) {
this.xtsb = xtsb;
}
public String getCsmc() {
return csmc;
}
public void setCsmc(String csmc) {
this.csmc = csmc == null ? null : csmc.trim();
}
public String getCsz() {
return csz;
}
public void setCsz(String csz) {
this.csz = csz == null ? null : csz.trim();
}
public String getMrz() {
return mrz;
}
public void setMrz(String mrz) {
this.mrz = mrz == null ? null : mrz.trim();
}
public String getCsms() {
return csms;
}
public void setCsms(String csms) {
this.csms = csms == null ? null : csms.trim();
}
public String getCskxz() {
return cskxz;
}
public void setCskxz(String cskxz) {
this.cskxz = cskxz == null ? null : cskxz.trim();
}
public String getJgid() {
return jgid;
}
public void setJgid(String jgid) {
this.jgid = jgid == null ? null : jgid.trim();
}
public String getTjjgcsh() {
return tjjgcsh;
}
public void setTjjgcsh(String tjjgcsh) {
this.tjjgcsh = tjjgcsh == null ? null : tjjgcsh.trim();
}
public String getYyjgcsh() {
return yyjgcsh;
}
public void setYyjgcsh(String yyjgcsh) {
this.yyjgcsh = yyjgcsh == null ? null : yyjgcsh.trim();
}
public String getCreateid() {
return createid;
}
public void setCreateid(String createid) {
this.createid = createid == null ? null : createid.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getModifyid() {
return modifyid;
}
public void setModifyid(String modifyid) {
this.modifyid = modifyid == null ? null : modifyid.trim();
}
public Date getModifytime() {
return modifytime;
}
public void setModifytime(Date modifytime) {
this.modifytime = modifytime;
}
}
5.2 DTO层新增YyptTestDTO.java文件
com.wying.myspringbootframework.dto.YyptTestDTO.java
package com.wying.myspringbootframework.dto;
import java.util.Date;
/**
* description:YyptTestDTO
* date: 2022/5/22
* author: gaom
* version: 1.0
*/
public class YyptTestDTO {
private Integer csid;
private Integer xtsb;
private String csmc;
private String csz;
private String mrz;
private String csms;
private String cskxz;
private String jgid;
private String tjjgcsh;
private String yyjgcsh;
private String createid;
private Date createtime;
private String modifyid;
private Date modifytime;
public Integer getCsid() {
return csid;
}
public void setCsid(Integer csid) {
this.csid = csid;
}
public Integer getXtsb() {
return xtsb;
}
public void setXtsb(Integer xtsb) {
this.xtsb = xtsb;
}
public String getCsmc() {
return csmc;
}
public void setCsmc(String csmc) {
this.csmc = csmc == null ? null : csmc.trim();
}
public String getCsz() {
return csz;
}
public void setCsz(String csz) {
this.csz = csz == null ? null : csz.trim();
}
public String getMrz() {
return mrz;
}
public void setMrz(String mrz) {
this.mrz = mrz == null ? null : mrz.trim();
}
public String getCsms() {
return csms;
}
public void setCsms(String csms) {
this.csms = csms == null ? null : csms.trim();
}
public String getCskxz() {
return cskxz;
}
public void setCskxz(String cskxz) {
this.cskxz = cskxz == null ? null : cskxz.trim();
}
public String getJgid() {
return jgid;
}
public void setJgid(String jgid) {
this.jgid = jgid == null ? null : jgid.trim();
}
public String getTjjgcsh() {
return tjjgcsh;
}
public void setTjjgcsh(String tjjgcsh) {
this.tjjgcsh = tjjgcsh == null ? null : tjjgcsh.trim();
}
public String getYyjgcsh() {
return yyjgcsh;
}
public void setYyjgcsh(String yyjgcsh) {
this.yyjgcsh = yyjgcsh == null ? null : yyjgcsh.trim();
}
public String getCreateid() {
return createid;
}
public void setCreateid(String createid) {
this.createid = createid == null ? null : createid.trim();
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getModifyid() {
return modifyid;
}
public void setModifyid(String modifyid) {
this.modifyid = modifyid == null ? null : modifyid.trim();
}
public Date getModifytime() {
return modifytime;
}
public void setModifytime(Date modifytime) {
this.modifytime = modifytime;
}
}
5.3 dao层 新增TestDao.java
com.wying.myspringbootframework.dao.TestDao.java
package com.wying.myspringbootframework.dao;
import com.wying.myspringbootframework.entity.YyptTestEntity;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
/**
* description:
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
@Mapper
public interface TestDao {
int deleteByPrimaryKey(Integer csid);
int insert(YyptTestEntity record);
YyptTestEntity selectByPrimaryKey(Integer csid);
List<YyptTestEntity> selectAll();
int updateByPrimaryKey(YyptTestEntity record);
}
5.4 service层 新增TestService.java,TestServiceImpl.java
com.wying.myspringbootframework.service.TestService.java
package com.wying.myspringbootframework.service;
import java.util.List;
/**
* description:测试服务接口
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
public interface TestService {
/**
*
* @return
*/
public List findAll();
}
com.wying.myspringbootframework.service.impl.TestServiceImpl.java
package com.wying.myspringbootframework.service.impl;
import com.wying.myspringbootframework.dao.TestDao;
import com.wying.myspringbootframework.entity.YyptTestEntity;
import com.wying.myspringbootframework.service.TestService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
/**
* description:
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
@Service
public class TestServiceImpl implements TestService {
@Resource
public TestDao testDao;
@Override
public List<YyptTestDTO> findAll() {
List<YyptTestEntity> list= testDao.selectAll();
List<YyptTestDTO> yyptTestDTOList=new ArrayList<>();
/**
* 转为DTO返回到前端
* list不能直接转,需要遍历转
* 使用的org.springframework.beans.BeanUtils 转换
*/
list.forEach(yyptTestEntity -> {
YyptTestDTO yyptTestDTO=new YyptTestDTO();
BeanUtils.copyProperties(yyptTestEntity,yyptTestDTO);
yyptTestDTOList.add(yyptTestDTO);
});
return yyptTestDTOList;
}
}
5.5 controller层 新增TestController.java
com.wying.myspringbootframework.controller.TestController.java
package com.wying.myspringbootframework.controller;
import com.wying.myspringbootframework.entity.YyptTestEntity;
import com.wying.myspringbootframework.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* description:测试请求
* date: 2022/3/31
* author: gaom
* version: 1.0
*/
@RestController
@RequestMapping("/testController")
public class TestController {
@Autowired
public TestService testService;
@GetMapping("/findAll")
public List<YyptTestEntity> findAll(){
List<YyptTestEntity> list=testService.findAll();
return list;
}
}
5.6 mapper层 新增YyptTestMapper.xml
src/main/resources/mappers/YyptTestMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.wying.myspringbootframework.dao.TestDao">
<resultMap id="BaseResultMap" type="com.wying.myspringbootframework.entity.YyptTestEntity">
<id column="CSID" jdbcType="DECIMAL" property="csid" />
<result column="XTSB" jdbcType="INTEGER" property="xtsb" />
<result column="CSMC" jdbcType="VARCHAR" property="csmc" />
<result column="CSZ" jdbcType="VARCHAR" property="csz" />
<result column="MRZ" jdbcType="VARCHAR" property="mrz" />
<result column="CSMS" jdbcType="VARCHAR" property="csms" />
<result column="CSKXZ" jdbcType="VARCHAR" property="cskxz" />
<result column="JGID" jdbcType="VARCHAR" property="jgid" />
<result column="TJJGCSH" jdbcType="VARCHAR" property="tjjgcsh" />
<result column="YYJGCSH" jdbcType="VARCHAR" property="yyjgcsh" />
<result column="CREATEID" jdbcType="VARCHAR" property="createid" />
<result column="CREATETIME" jdbcType="TIMESTAMP" property="createtime" />
<result column="MODIFYID" jdbcType="VARCHAR" property="modifyid" />
<result column="MODIFYTIME" jdbcType="TIMESTAMP" property="modifytime" />
</resultMap>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from yypt_test
where CSID = #{csid,jdbcType=DECIMAL}
</delete>
<insert id="insert" parameterType="com.wying.myspringbootframework.entity.YyptTestEntity">
insert into yypt_test (CSID, XTSB, CSMC,
CSZ, MRZ, CSMS, CSKXZ,
JGID, TJJGCSH, YYJGCSH,
CREATEID, CREATETIME, MODIFYID,
MODIFYTIME)
values (#{csid,jdbcType=DECIMAL}, #{xtsb,jdbcType=INTEGER}, #{csmc,jdbcType=VARCHAR},
#{csz,jdbcType=VARCHAR}, #{mrz,jdbcType=VARCHAR}, #{csms,jdbcType=VARCHAR}, #{cskxz,jdbcType=VARCHAR},
#{jgid,jdbcType=VARCHAR}, #{tjjgcsh,jdbcType=VARCHAR}, #{yyjgcsh,jdbcType=VARCHAR},
#{createid,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP}, #{modifyid,jdbcType=VARCHAR},
#{modifytime,jdbcType=TIMESTAMP})
</insert>
<update id="updateByPrimaryKey" parameterType="com.wying.myspringbootframework.entity.YyptTestEntity">
update yypt_test
set XTSB = #{xtsb,jdbcType=INTEGER},
CSMC = #{csmc,jdbcType=VARCHAR},
CSZ = #{csz,jdbcType=VARCHAR},
MRZ = #{mrz,jdbcType=VARCHAR},
CSMS = #{csms,jdbcType=VARCHAR},
CSKXZ = #{cskxz,jdbcType=VARCHAR},
JGID = #{jgid,jdbcType=VARCHAR},
TJJGCSH = #{tjjgcsh,jdbcType=VARCHAR},
YYJGCSH = #{yyjgcsh,jdbcType=VARCHAR},
CREATEID = #{createid,jdbcType=VARCHAR},
CREATETIME = #{createtime,jdbcType=TIMESTAMP},
MODIFYID = #{modifyid,jdbcType=VARCHAR},
MODIFYTIME = #{modifytime,jdbcType=TIMESTAMP}
where CSID = #{csid,jdbcType=DECIMAL}
</update>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select CSID, XTSB, CSMC, CSZ, MRZ, CSMS, CSKXZ, JGID, TJJGCSH, YYJGCSH, CREATEID,
CREATETIME, MODIFYID, MODIFYTIME
from yypt_test
where CSID = #{csid,jdbcType=DECIMAL}
</select>
<select id="selectAll" resultMap="BaseResultMap">
select CSID, XTSB, CSMC, CSZ, MRZ, CSMS, CSKXZ, JGID, TJJGCSH, YYJGCSH, CREATEID,
CREATETIME, MODIFYID, MODIFYTIME
from yypt_test
</select>
</mapper>
6.启动测试
http://127.0.0.1:7015/myspringbootframework/testController/findAll
成功返回数据