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

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

 

sql自定义mapper 与SB整合

数据源student准备

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for student
-- ----------------------------
DROP TABLE IF EXISTS `student`;
CREATE TABLE `student`  (
  `id` bigint(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `age` int(11) NULL DEFAULT NULL,
  `email` varchar(80) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL,
  `status` int(11) NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci ROW_FORMAT = Dynamic;

-- ----------------------------
-- Records of student
-- ----------------------------
INSERT INTO `student` VALUES (1, 'wanson', 18, 'wanson@163.com', 1);
INSERT INTO `student` VALUES (2, 'wanson', 38, 'wanson@163.com', 0);
INSERT INTO `student` VALUES (3, 'lisi', 28, 'lisi@163.com', 0);

SET FOREIGN_KEY_CHECKS = 1;

Student类文件

package com.uos.handleag.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import lombok.Data;

@Data
public class Student {
    @TableId(value = "id", type = IdType.AUTO)
    private Long id;
    private String name;
    private Integer age;
    private String email;
    private Integer status;
}

StudentMapper类文件

package com.uos.handleag.mapper;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.uos.handleag.entity.Student;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;
@Mapper
public interface StudentMapper extends BaseMapper<Student> {
    //    自定义方法
    public int insertStudent(Student student);

    public Student selectStudentById(Long id);

    public List<Student> selectStudentByName(String name);
}

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.uos.handleag.mapper.StudentMapper">

    <insert id="insertStudent">
        insert into student(name, age, email, status)
        values (#{name}, #{age}, #{email}, #{status})
    </insert>
    <select id="selectStudentById" resultType="com.uos.handleag.entity.Student">
        select id, name, age, email, status
        from student
        where id = #{studentId}
    </select>
    <select id="selectStudentByName" resultType="com.uos.handleag.entity.Student">
        select id, name, age, email, status
        from student
        where name = #{name}
    </select>
</mapper>

application-dev.yml配置文件

server:
  port: 80



spring:
  datasource:
    url: jdbc:mysql://192.168.85.136:3306/handlemybatis?useUnicode=true&zeroDateTimeBehavior=convertToNull&autoReconnect=true&characterEncoding=utf-8
    username: root
    password: 1
    driver-class-name: com.mysql.cj.jdbc.Driver
#    日志配置
mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  mapper-locations: classpath*:xml/*Mapper.xml

单元测试

package com.uos.handleag;

import com.uos.handleag.entity.Student;
import com.uos.handleag.mapper.StudentMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
class HandleAgApplicationTests {

    @Autowired
    private StudentMapper studentDao;

    @Test
    void contextLoads() {
    }

    @Test
    void contextLoadsinsertStudent() {
        Student student = new Student();
        student.setName("lisi");
        student.setEmail("lisi@163.com");
        student.setAge(28);
        student.setStatus(0);
        int rows = studentDao.insertStudent(student);
        System.out.println("insertStudent:\t" + rows);
    }

    @Test
    void contextLoadsselectStudentById() {
        Student student = studentDao.selectStudentById(2L);
        if (student == null) {
            // 其他业务操作
        }
        System.out.println("selectStudentById:\t" + student);
    }

    @Test
    void contextLoadsselectStudentByName() {
        List<Student> studentList = studentDao.selectStudentByName("wanson");
        studentList.forEach(student -> System.out.println(student));
    }

}


附件图

posted on 2023-04-07 14:49  Indian_Mysore  阅读(32)  评论(0)    收藏  举报

导航