时间:2021-07-01 10:21:17 帮助过:6人阅读
注意:手动修改一下mysql驱动的版本,默认版本比较高,应用时可能会有错误;我这里手动指定为5.1.46版本。
5、修改原application.properties为application.yml,并做如下配置
spring: datasource: driverClassName: com.mysql.jdbc.Driver url: jdbc:mysql://localhost:3306/scott?useUnicode=true&autoReconnect=true&allowMultiQueries=true&useSSL=false username: root password: **** server: port: 8686 mybatis: config-location: classpath:mybatis-config.xml
6、resources目录下新增mybatis-config.xml配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.lock"/> </typeAliases> <mappers> <mapper resource="mybatis/mapper/DeptMapper.xml"/> <mapper resource="mybatis/mapper/EmpMapper.xml"/> <mapper resource="mybatis/mapper/SalgradeMapper.xml"/> <mapper resource="mybatis/mapper/BonusMapper.xml"/> </mappers> </configuration>
对应DeptMapper.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.lock.smartmis.business.vo.DeptVo"> <select id="getAllDepts" resultType="com.lock.smartmis.business.vo.DeptVo"> select * from dept </select> <select id="getDeptByNo" resultType="com.lock.smartmis.business.vo.DeptVo"> select * from dept where deptno = #{deptNo} </select> </mapper>
7、建立bo类(以Dept类为例),并以此为基础建立vo类
package com.lock.smartmis.business.bo; import java.io.Serializable; public class Dept implements Serializable { private static final long serialVersionUID = 1L; private long deptNo; private String dName; private String loc; public long getDeptNo() { return deptNo; } public void setDeptNo(long deptNo) { this.deptNo = deptNo; } public String getdName() { return dName; } public void setdName(String dName) { this.dName = dName; } public String getLoc() { return loc; } public void setLoc(String loc) { this.loc = loc; } @Override public String toString(){ return getDeptNo() + "," + getdName() + "," + getLoc(); } }
package com.lock.smartmis.business.vo; import com.lock.smartmis.business.bo.Dept; public class DeptVo extends Dept{ private static final long serialVersionUID = 1L; }
8、Dao层接口及其实现
package com.lock.smartmis.business.dao; import com.lock.smartmis.business.vo.DeptVo; import java.util.List; public interface DeptDao { public List<DeptVo> getAllDepts(); public DeptVo getDeptByNo(Long id); }
package com.lock.smartmis.business.dao.impl; import com.lock.smartmis.business.dao.DeptDao; import com.lock.smartmis.business.vo.DeptVo; import org.mybatis.spring.SqlSessionTemplate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.List; @Component public class DeptDaoimpl implements DeptDao { @Autowired public SqlSessionTemplate sqlSessionTemplate; @Override public List<DeptVo> getAllDepts() { return this.sqlSessionTemplate.selectList("getAllDepts"); } @Override public DeptVo getDeptByNo(Long deptNo) { return this.sqlSessionTemplate.selectOne("getDeptByNo", deptNo); } }
9、Controller层
package com.lock.smartmis.business.controller; import com.lock.smartmis.business.service.DeptService; import com.lock.smartmis.business.util.JsonResult; import com.lock.smartmis.business.util.ResultCode; import com.lock.smartmis.business.vo.DeptVo; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController @RequestMapping("/restapi") public class ScottController { protected static Logger logger = LoggerFactory.getLogger(ScottController.class); @Autowired private DeptService deptService; @GetMapping("/test") public String test() { return "API接口测试"; } @GetMapping("/depts") public JsonResult getAllDepts(){ List<DeptVo> list = this.deptService.getAllDepts(); return new JsonResult(ResultCode.SUCCESS, list); } @GetMapping("/dept/{deptNo}") public JsonResult getDeptByNo(@PathVariable("deptNo") Long deptNo){ DeptVo deptVo = this.deptService.getDeptByNo(deptNo); return new JsonResult(ResultCode.SUCCESS, deptVo); } }
10、项目运行起来,测试一下接口
~
SpringBoot示例教程(一)MySQL与Mybatis基础用法
标签:template alibaba port import lan false 注意 创建 http