Springbootspringboot中使用mybatis操作数据库
Posted to-red
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Springbootspringboot中使用mybatis操作数据库相关的知识,希望对你有一定的参考价值。
新建springboot项目的时候,选择好web,mybatis,JDBC
在application.properties或者application.yml中配置
spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/studentManagerBoot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC username: root password:
Student的实体类:
package com.hj.studentmanagerboot.user.entity; public class Student private int stuId; private int stuNum; private String stuName; private String stuBirthday; private int stuEnterYear; private int stiState; @Override public String toString() return "Student" + "stuId=" + stuId + ", stuNum=" + stuNum + ", stuName=" + stuName + ", stuBirthday=‘" + stuBirthday + ‘\\‘‘ + ", stuEnterYear=" + stuEnterYear + ", stiState=" + stiState + ‘‘; public String getStuName() return stuName; public void setStuName(String stuName) this.stuName = stuName; public int getStuId() return stuId; public void setStuId(int stuId) this.stuId = stuId; public int getStuNum() return stuNum; public void setStuNum(int stuNum) this.stuNum = stuNum; public String getStuBirthday() return stuBirthday; public void setStuBirthday(String stuBirthday) this.stuBirthday = stuBirthday; public int getStuEnterYear() return stuEnterYear; public void setStuEnterYear(int stuEnterYear) this.stuEnterYear = stuEnterYear; public int getStiState() return stiState; public void setStiState(int stiState) this.stiState = stiState;
写好在dao包中新建StudentMapper接口,添加getStudent(int stuId)方法
package com.hj.studentmanagerboot.user.dao; import com.hj.studentmanagerboot.user.entity.Student; import org.springframework.stereotype.Repository; @Repository public interface StudentMapper Student getStudent(int stuId);
在service包中新建getStudentService,并自动装配StudentMapper,添加getStudentService的方法
package com.hj.studentmanagerboot.user.service; import com.hj.studentmanagerboot.user.dao.StudentMapper; import com.hj.studentmanagerboot.user.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class StudentServiceTest @Autowired StudentMapper studentMapper; public Student getStudent(int stuId) return studentMapper.getStudent(stuId);
在controller中自动装配StudentService
package com.hj.studentmanagerboot.user.handler; import com.hj.studentmanagerboot.user.service.StudentServiceTest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("testStudent") public class StudentHandlerTest @Autowired private StudentServiceTest studentServiceTest; @RequestMapping("getUser/id") public String getUser(@PathVariable("id") int stuId) return studentServiceTest.getStudent(stuId).toString();
在SpringBoot的入口程序中增加mybatis的注解,扫描dao包
@MapperScan("com.hj.studentmanagerboot.user.dao") public class ...
在Resource文件下新建mapper文件夹存放Mapper.xml,并在application.yml或application.properties中增加存放mybatis的Mapper配置文件的信息:
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.hj.studentmanagerboot.user.entity
在StudentMapper.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.hj.studentmanagerboot.user.dao.StudentMapper"> <select id="getStudent" resultType="com.hj.studentmanagerboot.user.entity.Student"> select * from stuInfo where stuId = #stuId </select> </mapper>
以上是关于Springbootspringboot中使用mybatis操作数据库的主要内容,如果未能解决你的问题,请参考以下文章
SpringBootSpringBoot 之 OpenFeign服务调用