springboot整合mybatis对数据库进行增删改查(注解配置方式)
1.引入依赖可以在创建springboot引用的时候选择好需要开发的模块也可以直接在pom文件添加依赖<?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-instanc
·
1.引入依赖
可以在创建springboot引用的时候选择好需要开发的模块
也可以直接在pom文件添加依赖
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>springboot-data</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-data</name>
<description>springboot-data</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.7.RELEASE</version>
<configuration>
<mainClass>com.example.springbootdata.SpringbootDataApplication</mainClass>
</configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.准备数据库
这里我仅仅用到topic一张表
配置数据库连接池注意如果mysql数据库在8.0+的要在drive加cj
# 应用名称
spring.application.name=springboot-data
# 数据库驱动:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# 数据源名称
spring.datasource.name=defaultDataSource
# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/newsmanagersystem?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
# 数据库用户名&密码:
spring.datasource.username=root
spring.datasource.password=123456
# 应用服务 WEB 访问端口
server.port=8080
3.创建实体类Topic
package com.example.springbootdata.bean;
public class Topic {
private Integer tid;
private String tname;
public Integer getTid() {
return tid;
}
public void setTid(Integer tid) {
this.tid = tid;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
}
4.创建mapper层,对数据库进行操作
package com.example.springbootdata.mapper;
import com.example.springbootdata.bean.Topic;
import org.apache.ibatis.annotations.*;
//@Mapper
public interface TopicMapper {
@Select("select * from topic where tid=#{id}")
public Topic gettopicById(Integer id);
@Delete("delete from topic where tid=#{id}")
public int delete(Integer id);
@Insert("insert into topic(tname) values(#{tname})")
public int insertTopic(Topic topic);
@Update("update topic set tname=#{tname} where tid=#{tid}")
public void updateTopic(Topic topic);
}
这次我们用的方式是直接在方法上通过注解sql语句的方式进行操作,因为id是自增所以只需要插入tname的值就可以了
除了在每个接口类中加上@Mapper
也可以在主类上添加包扫描这样在mapper所有的接口类都会等同于加上了@Mapper注解
@MapperScan(value = "com.example.springbootdata.mapper")
注意!
sql语句中inset update delete是不需要返回值类型的,
将mapper中这些方法的返回值类型设为int或void。
但是也不要忘记接口和实现类的返回类型也要改
5.创建控制层
实现我们在网页进行操作修改数据库(这里我们简单写)
package com.example.springbootdata.controller;
import com.example.springbootdata.bean.Topic;
import com.example.springbootdata.mapper.TopicMapper;
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.RestController;
@RestController
public class TopicController {
@Autowired
TopicMapper topicMapper;
@GetMapping("/intopic")
public Topic inserDept(Topic topic){
topicMapper.insertTopic(topic);
return topic;
}
@GetMapping("/delete")
public void delete(Integer id){
topicMapper.delete(id);
}
@GetMapping("/gettopicById")
public Topic gettopicById(Integer id){
Topic tp;
tp=topicMapper.gettopicById(id);
return tp;
}
@GetMapping("/updateTopic")
public void updateTopic(Topic topic){
topicMapper.updateTopic(topic);
}
}
我们要注入TopicMapper 才能引入方法
6.测试
插入
查询
删除
修改
更多推荐
已为社区贡献1条内容
所有评论(0)