数据库层的准备

首先首先用到mysql和mybatis

1. 依赖

 <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 数据层 -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.33</version>
        </dependency>

2.配置yaml

image

注意注意 有坑
参考 https://blog.csdn.net/weixin_45812770/article/details/107996966 setTimezone 不识别
如果启动不成功在Database连接mysql中配置
image
image

注意你的xml文件和mapper映射位置就行

3.实体类

image
与表中对应
使用lombok简化

4.mapper映射

image

5.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.fang.mapper.EmployeeMapper">

    <resultMap id="EmployeeMap" type="Employee">
        <id property="id" column="eid"/>
        <result property="lastName" column="last_name"/>
        <result property="email" column="email"/>
        <result property="gender" column="gender"/>
        <result property="birth" column="birth"/>
        <association property="eDepartment"  javaType="Department">
            <id property="id" column="did"/>
            <result property="departmentName" column="dname"/>
        </association>
    </resultMap>

    <select id="getEmployees" resultMap="EmployeeMap">
        select e.id as eid,last_name,email,gender,birth,d.id as did,d.department_name as dname
        from department d,employee e
        where d.id = e.department
    </select>

    <insert id="save" parameterType="Employee">
        insert into employee (last_name,email,gender,department,birth)
        values (#{lastName},#{email},#{gender},#{department},#{birth});
    </insert>

    <update id="update" parameterType="Employee">
         update employee
         set last_name = #{lastName},email=#{email},gender=#{gender},department=#{department},birth=#{birth}
         where id = #{id} ;
    </update>

    <select id="get" resultType="Employee">
        select * from employee where id = #{id}
    </select>

    <delete id="delete" parameterType="int">
        delete from employee where id = #{id}
    </delete>
</mapper>

6.数据库数据表

哈哈蚊香一言 用好人工AI确实很强大
image
image


到这里就差不多了
剩下的就是显示效果 controller层调用增删改查 搭配html页面跳转显示

哦哦哦还有一个东西建议:启动项目无法加载找不到主类 并无问题 重新打开idea 或者rebuild项目

posted on 2024-04-08 09:32  蒸饺  阅读(15)  评论(0)    收藏  举报