springboot整合mybatis(配置文件)
程序员文章站
2022-07-12 22:42:57
...
结构目录
依赖`
<dependencies>
<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.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
数据库
写代码,1,配置文件application.yml
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/student?serverTimezone=UTC&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
mybatis:
type-aliases-package: com.wan.pojo
mapper-locations: classpath:mapper/*.xml
2,实体类
package com.wan.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
private int id;
private String name;
private int age;
private String sex;
}
dao层
package com.wan.dao;
import com.wan.pojo.Student;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
@Repository //加入ioc容器
@Mapper
public interface StudentMapper {
public int addStudent(Student student);
}
controller层
package com.wan.controller;
import com.alibaba.fastjson.JSON;
import com.wan.dao.StudentMapper;
import com.wan.pojo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class BmController {
@Autowired()
StudentMapper studentMapper;
@RequestMapping("/a")
public String allStudents(){
Student student= new Student();
student.setName("大哈");
student.setAge(6);
student.setSex("男");
int result = studentMapper.addStudent(student);
System.out.println(result);
return result>0?"插入成功!":"插入失败";
}
}
运行结果:
查看数据库是否插入(由于我多次操作,没改值)
上一篇: 贝塞尔曲线扫盲
下一篇: Cocos - 贝塞尔曲线 Bezier
推荐阅读
-
SpringBoot + Mybatis 增删改查实例教程详细解读
-
SpringBoot+Dubbo+Zookeeper整合搭建简单的分布式应用
-
springboot+mybatis日志显示SQL的最简单方法
-
Mybatis整合spring(适合小白)
-
SpringBoot整合MongoDB的步骤详解
-
springboot配置文件绑定实现解析
-
MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射
-
springboot~mybatis里localdatetime序列化问题
-
SpringBoot整合SpringCloud搭建分布式应用
-
带着新人学springboot的应用01(springboot+mybatis+缓存 上)