昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

使用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 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.4</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.baidu</groupId>
	<artifactId>HelloMybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>HelloMybatis</name>
	<description>Mybatis使用入门</description>
	<properties>
		<java.version>21</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</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>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>3.0.3</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</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-test</artifactId>
			<version>3.0.3</version>
			<scope>test</scope>
		</dependency>



		<!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-launcher -->
		<dependency>
			<groupId>org.junit.platform</groupId>
			<artifactId>junit-platform-launcher</artifactId>
			<version>1.10.2</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/org.junit.vintage/junit-vintage-engine -->
		<dependency>
			<groupId>org.junit.vintage</groupId>
			<artifactId>junit-vintage-engine</artifactId>
			<version>5.10.2</version>
			<scope>test</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<excludes>
						<exclude>
							<groupId>org.projectlombok</groupId>
							<artifactId>lombok</artifactId>
						</exclude>
					</excludes>
				</configuration>
			</plugin>
		</plugins>
	</build>

</project>


application-dev.yml

Mybatis:
  type-aliases-package: com.baidu.HelloMybatis

Spring:
  #    ??????url+username+password+driver-class-name
  datasource:
    url: jdbc:mysql://192.168.103.209:3309/jdbcsb03?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
    username: root
    password: 1
    driver-class-name: com.mysql.cj.jdbc.Driver
  server:
    port: 80


修改启动类XXXApplication(@MapperScan)

package com.baidu.HelloMybatis;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@MapperScan("com.baidu.HelloMybatis") //定义需要扫描的Mapper
public class HelloMybatisApplication {

	public static void main(String[] args) {
		SpringApplication.run(HelloMybatisApplication.class, args);
	}

}


SQL脚本

DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NULL DEFAULT NULL,
  `sex` int(11) NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 12 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, 'uos', 1, 100);
INSERT INTO `student` VALUES (2, 'usa', 2, 200);
INSERT INTO `student` VALUES (3, 'russian', 1, 300);
INSERT INTO `student` VALUES (4, 'china', 1, 5000);
INSERT INTO `student` VALUES (5, 'china', 1, 5000);
INSERT INTO `student` VALUES (8, 'anhui', 1, 20);
INSERT INTO `student` VALUES (9, 'beijing', 1, 120);
INSERT INTO `student` VALUES (10, 'indian', 2, 3000);
INSERT INTO `student` VALUES (11, 'tianjin', 2, 210);

SET FOREIGN_KEY_CHECKS = 1;

Student.java

package com.baidu.HelloMybatis.model;

import lombok.Data;

@Data
public class Student {
    private Long id;
    private String name;
    private int sex;
    private int age;


    public Student() {
    }

    public Student(String name, int sex, int age) {
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}


StudentMapper.java

package com.baidu.HelloMybatis.services;

import com.baidu.HelloMybatis.model.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

//@Mapper
public interface StudentMapper {
    @Select("select * from student")
        //定义SQL查询语句即可实现查询所有学生列表功能
    List<Student> selectAll();

    @Select("select * from student where id=#{id,jdbcType=VARCHAR}")
    Student selectOne(Long id);

    @Insert({
            "insert into student (",
            "name, ",
            "age, ",
            "sex)",
            "values (",
            "#{name,jdbcType=VARCHAR},",
            "#{age,jdbcType=INTEGER},",
            "#{sex,jdbcType=INTEGER})"
    })
    int insert(Student record);


    @Select("delete from student where id=#{id,jdbcType=VARCHAR}")
    void delete(Long id);
}

业务功能进行单元测试

package com.baidu.HelloMybatis.services;

import com.baidu.HelloMybatis.model.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
class StudentMapperTest {

    @Autowired
    private StudentMapper studentMapper;


    @Test
    void testSelectAll() {
        //        查询
        List<Student> students = studentMapper.selectAll();
        for (Student student : students) {
            System.out.println(student.getId()+
                    "\t\tname:\t\t" + student.getName() +
                    "\t\tage:\t\t" + student.getAge());
        }
    }

    @Test
    void selectOne() {
        Student student=studentMapper.selectOne(3L);
        System.out.println("student:\t\t"+student.toString());
    }

    @Test
    void insert() {
        Student student=new Student("tianjin",2,210);
        studentMapper.insert(student);
    }

    @Test
    void delete() {
//删除前的数据
        System.out.println("删除前的数据:\t\t\n");
        List<Student> students = studentMapper.selectAll();
        for (Student student : students) {
            System.out.println(student.getId()+
                    "\t\tname:\t\t" + student.getName() +
                    "\t\tage:\t\t" + student.getAge());
        }
        studentMapper.delete(6L);



    }
}

posted on 2024-04-12 14:50  Indian_Mysore  阅读(24)  评论(1)    收藏  举报

导航