zoukankan      html  css  js  c++  java
  • 【mybatis】-- springboot快速整合mybatis

    1、添加依赖

    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.0</version>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
    </dependency>

    2、application配置文件

    spring.datasource.url=jdbc:mysql://localhost:3306/demo?serverTimezone=UTC&characterEncoding=UTF-8
    spring.datasource.username=root
    spring.datasource.password=xxxxx
    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

    关于更多更详细的配置请参考:https://blog.csdn.net/ajlzhu/article/details/81009845

    3、新建包类

    新建controller、dao、domain、service包,新建TestController、StudentDao、Student、StudentService类;

    创建后的目录结构(多余的包和类与本文内容无关,不用创建):

    4、创建student表

    CREATE TABLE `student` (
       `sno` int(10) unsigned NOT NULL AUTO_INCREMENT,
       `sname` varchar(20) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
       `sage` bigint(20) DEFAULT NULL,
       `sex` varchar(10) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
       PRIMARY KEY (`sno`)
     ) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8

    5、student类

    public class Student {
    private Long sno;
    private String sname;
    private int sage;
    private String sex;

    public Long getSno() {
    return sno;
    }

    public void setSno(Long sno) {
    this.sno = sno;
    }

    public String getSname() {
    return sname;
    }

    public void setSname(String sname) {
    this.sname = sname;
    }

    public int getSage() {
    return sage;
    }

    public void setSage(int sage) {
    this.sage = sage;
    }

    public String getSex() {
    return sex;
    }

    public void setSex(String sex) {
    this.sex = sex;
    }
    }

    6、StudentDao接口

    @Mapper
    public interface StudentDao {


    @Select("SELECT * FROM student where sno=#{id}")
    public Student getById(@Param("id")int id);

    }

    7、StudentService类

    @Service
    public class StudentService {

    @Autowired
    StudentDao studentDao;

    public Student getById(int id){
    return studentDao.getById(id);
    }
    }

    8、控制器

    @RestController
    public class Test {

    @Autowired
    StudentService studentService;

    @GetMapping("/demo")
    public String student(){
    Student student = studentService.getById(1);
    return student.getSname();
    }

    }

    9、测试

    数据库数据:

    浏览器访问:

    到这里就完全整合完成了,甚至都不需要进行任何多余的配置。

    -----------------------------------------------------------------------------------

    找到一篇讲的很好的文章:https://www.cnblogs.com/ityouknow/p/6037431.html

  • 相关阅读:
    java基础——标准输入输出重定向,数据流
    java基础——对象流,序列化机制Serializable
    java基础——包装流
    java基础——随机访问流
    java基础——流体系,字符流和字节流基本用法
    读取 xml 文件 获取其中保存的数据信息
    批处理 获取某个文件的,特定 两列,可以修改用来做相关的操作
    写一个最简单的 Server
    对 JDBC 做一个轻量封装,待完善。。。
    从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)
  • 原文地址:https://www.cnblogs.com/jsyllhb/p/10553821.html
Copyright © 2011-2022 走看看