依赖pom.xml:
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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
resource包下的Application.yml:
Application.yml
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/work #自己数据库名
username: root
password: 123456
mybatis:
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.demo.pojo
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
pojo包下实体类User:
User
package com.demo.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class User {
private Long id;
private String name;
private Integer age;
private String email;
}
mapper包下的接口UserMapper:
UserMapper
package com.demo.mapper;
import com.demo.pojo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
List<User> getAll();
int insert(User user);
int delete(Long id);
int update(User user);
}
resource包下mapper映射UserMapper:
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.demo.mapper.UserMapper">
<select id="getAll" resultType = "User">
select * from t_user
</select>
<insert id="insert">
insert into t_user(id,name,age,email)
values(#{id}, #{name}, #{age}, #{email})
</insert>
<delete id="delete">
delete from t_user where id=#{id}
</delete>
<update id="update">
update t_user set
id=#{id},
name=#{name},
age=#{age},
email=#{email}
where id=#{id}
</update>
</mapper>
主启动类DemoApplication:
DemoApplication
package com.demo;
import org.apache.ibatis.annotations.Mapper;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.demo.mapper") // 注意自己路径
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
测试DemoApplicationTest:
DemoApplicationTest
package com.demo;
import com.demo.mapper.UserMapper;
import com.demo.pojo.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.sql.SQLOutput;
import java.util.List;
@SpringBootTest
class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Autowired
private DataSource dataSource;
private int count;
@Test
void dataSource() throws SQLException{
System.out.println(dataSource.getConnection());
}
@Test
void testGetAll() {
List<User> list = userMapper.getAll();
System.out.println(list);
}
@Test
void testInsert(){
User user = new User(666L, "qimiao", 22, "[email protected]");
int count = userMapper.insert(user);
System.out.println((count == 1 ? "插入成功" : "插入失败"));
}
@Test
void deleteTest() {
int count = userMapper.delete(345L);
System.out.println((count == 1 ? "删除成功" : "删除失败"));
}
@Test
void updateTest() {
User user = new User(666L, "kuazhang", 53, "[email protected]");
int count = userMapper.update(user);
System.out.println((count == 1 ? "更新成功" : "更新失败"));
}
}
测试结果:
检验数据库连接:
添加信息
删除信息
查询信息
标签:SpringBoot,demo,boot,SpringBoot3,org,Mybatis,import,com,User From: https://www.cnblogs.com/pine1203/p/18084593