一.搭建mybatis
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.easy</groupId><artifactId>EasySpringMybatis</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-parent</artifactId><version>2.6.6</version></parent><properties><spring-boot-version>2.6.6</spring-boot-version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-aspects</artifactId><version>5.3.16</version></dependency><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.2.2</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><version>${spring-boot-version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-api</artifactId><version>1.7.14</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.26</version></dependency></dependencies></project>
3、配置mybatis的数据源 指定映射配置文件的位置
#注释 Springboot主配置文件
#yml基本语法
#student:
# name: zhangsan
# sex: nan
#配置mybatis的数据源
spring:datasource:username: rootpassword: rootdriver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/easydata#设置MyBatis xml保存路径,xml 文件中保存是对数据库的具体操作SQL
mybatis:mapper-locations: classpath:mapper/*.xml#设置日志级别
logging:level:com.easy.dao: debug
二.
3.具体操作
package com.easy.bean;public class Department {private int id;private String code;private String name;public int getId() {return id;}public void setId(int id) {this.id = id;}public String getCode() {return code;}public void setCode(String code) {this.code = code;}public String getName() {return name;}public void setName(String name) {this.name = name;}}
package com.easy.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;import com.easy.bean.Department;
import com.easy.dao.IDepartmentDao;@RestController
public class DepartmentController {@AutowiredIDepartmentDao dao;@PostMapping("department")public String addDepartment(Department department) {dao.addDepartment(department);return "success";}@DeleteMapping("department/{id}")public String delDepartment(@PathVariable int id) {dao.delDepartment(id);return "success";}@PutMapping("department")public String editDepartment(Department department) {dao.editDepartment(department);return "success";}@GetMapping("department/{id}")public String getName(@PathVariable int id) {String name= dao.getName(id);return name;}
}
package com.easy.dao;import org.apache.ibatis.annotations.Mapper;import com.easy.bean.Department;@Mapper
public interface IDepartmentDao {int addDepartment(Department department);int delDepartment(int id);int editDepartment(Department department);String getName(int id);
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.easy.dao.IDepartmentDao"><insert id="addDepartment">insert into department(id,code,name)value(#{id},#{code},#{name})</insert><delete id="delDepartment">delete from department where id=#{id}</delete><update id="editDepartment"> update department set name=#{name},code=#{code}where id=#{id}</update><select id="getName" resultType="string">select name from department where id=#{id}</select>
</mapper>
今日内容:
今日内容:搭建mybatis ORM 配置数据源 $#的区别 增删改