Administrator
发布于 2025-01-23 / 2 阅读
0
0

JAVA-JDBC和MyBatis配置问题

JAVA-JDBC和MyBatis配置问题

JDBC

Maven依赖

<!--        JDBC-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jdbc</artifactId>
</dependency>
<!--  添加MySQL驱动器的依赖  -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.33</version>
</dependency>
<!--        数据库连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>

properties配置

#JDBC
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/blog?useSSL=true&characterEncoding=utf8&useUnicode=true&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=
​

MyBatis

依赖

<!--        MyBatis-->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>org.mybatis</groupId>
    <artifactId>mybatis-spring</artifactId>
    <version>3.0.3</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus-boot-starter</artifactId>
    <version>3.2.0</version>
</dependency>
<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>mybatis-plus</artifactId>
    <version>3.5.5</version>
</dependency>

配置

#Mybatis
mybatis.type-aliases-package=com.test.blogadmin.pojo
mybatis.mapper-locations=classpath:/mapper/*.xml
#开启驼峰转换
mybatis.configuration.map-underscore-to-camel-case=true

使用

Application中新增扫描目录

@SpringBootApplication
// 在application中新增Mapper扫描目录
@MapperScan({"com.test.blogadmin.mapper"}) // 本行为新增配置 其他为原有配置 目录位置为Mapper位置
public class BlogAdminApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(BlogAdminApplication.class, args);
    }
​
}

新增DAO层,Mapper,Service,Impl及resource下mapper中的xml目录

DAO层

package com.test.blogadmin.pojo;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import org.springframework.stereotype.Repository;

/**
 * @author test
 * @TableName role
 */

@TableName(value ="role")
@Data
public class Role implements Serializable {
    /**
     * ID
     */
    @TableId
    private String id;

    /**
     * 角色名称
     */
    private String roleName;

    /**
     * 角色标识
     */
    private String roleAuth;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 
     */
    private Date createdAt;

    /**
     * 
     */
    private Date updatedAt;

    /**
     * 角色备注
     */
    private String remark;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;

    @Override
    public boolean equals(Object that) {
        if (this == that) {
            return true;
        }
        if (that == null) {
            return false;
        }
        if (getClass() != that.getClass()) {
            return false;
        }
        Role other = (Role) that;
        return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
            && (this.getRoleName() == null ? other.getRoleName() == null : this.getRoleName().equals(other.getRoleName()))
            && (this.getRoleAuth() == null ? other.getRoleAuth() == null : this.getRoleAuth().equals(other.getRoleAuth()))
            && (this.getStatus() == null ? other.getStatus() == null : this.getStatus().equals(other.getStatus()))
            && (this.getCreatedAt() == null ? other.getCreatedAt() == null : this.getCreatedAt().equals(other.getCreatedAt()))
            && (this.getUpdatedAt() == null ? other.getUpdatedAt() == null : this.getUpdatedAt().equals(other.getUpdatedAt()))
            && (this.getRemark() == null ? other.getRemark() == null : this.getRemark().equals(other.getRemark()));
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((getId() == null) ? 0 : getId().hashCode());
        result = prime * result + ((getRoleName() == null) ? 0 : getRoleName().hashCode());
        result = prime * result + ((getRoleAuth() == null) ? 0 : getRoleAuth().hashCode());
        result = prime * result + ((getStatus() == null) ? 0 : getStatus().hashCode());
        result = prime * result + ((getCreatedAt() == null) ? 0 : getCreatedAt().hashCode());
        result = prime * result + ((getUpdatedAt() == null) ? 0 : getUpdatedAt().hashCode());
        result = prime * result + ((getRemark() == null) ? 0 : getRemark().hashCode());
        return result;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(getClass().getSimpleName());
        sb.append(" [");
        sb.append("Hash = ").append(hashCode());
        sb.append(", id=").append(id);
        sb.append(", roleName=").append(roleName);
        sb.append(", roleAuth=").append(roleAuth);
        sb.append(", status=").append(status);
        sb.append(", createdAt=").append(createdAt);
        sb.append(", updatedAt=").append(updatedAt);
        sb.append(", remark=").append(remark);
        sb.append(", serialVersionUID=").append(serialVersionUID);
        sb.append("]");
        return sb.toString();
    }
}

Mapper

package com.test.blogadmin.mapper;

import com.test.blogadmin.pojo.Role;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
* @author test
* @description 针对表【role】的数据库操作Mapper
* @createDate 2024-04-10 13:53:50
* @Entity com.test.blogadmin.pojo.Role
*/
@Mapper
@Repository
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * @description 获取所有信息
     * @author test
     * @time 2024/4/10 13:56
     * @return List<Role> list
     */
    List<Role> findAll();

    /**
     * @description 根据id获取角色
     * @author test
     * @time 2024/4/10 14:02
     * @param id id
     * @return Role role
     */
    Role selectById(Integer id);
}

Service

package com.test.blogadmin.service;

import com.test.blogadmin.pojo.Role;
import com.baomidou.mybatisplus.extension.service.IService;

import java.util.List;

/**
* @author test
* @description 针对表【role】的数据库操作Service
* @createDate 2024-04-10 13:53:50
*/
public interface RoleService extends IService<Role> {
    
    /**
     * @description 获取所有角色列表
     * @author test
     * @time 2024/4/10 14:05
     * @return List<Role> list
     */
    List<Role> getAllRole(); 
}

Impl

package com.test.blogadmin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.test.blogadmin.pojo.Role;
import com.test.blogadmin.service.RoleService;
import com.test.blogadmin.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author test
* @description 针对表【role】的数据库操作Service实现
* @createDate 2024-04-10 13:53:50
*/
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role>
    implements RoleService{

    @Autowired
    private RoleMapper roleMapper;

    /**
     * @description 获取所有角色列表
     * @author test
     * @time 2024/4/10 14:07
     * @return List<Role> list
     */
    @Override
    public List<Role> getAllRole() {
        System.out.println("This is RoleServiceImpl, getAllRole function");
        return roleMapper.findAll();
    }
}

resource下的Mapper的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.test.blogadmin.mapper.RoleMapper">

    <resultMap id="BaseResultMap" type="com.test.blogadmin.pojo.Role">
            <id property="id" column="id" jdbcType="CHAR"/>
            <result property="roleName" column="role_name" jdbcType="VARCHAR"/>
            <result property="roleAuth" column="role_auth" jdbcType="VARCHAR"/>
            <result property="status" column="status" jdbcType="INTEGER"/>
            <result property="createdAt" column="created_at" jdbcType="TIMESTAMP"/>
            <result property="updatedAt" column="updated_at" jdbcType="TIMESTAMP"/>
            <result property="remark" column="remark" jdbcType="VARCHAR"/>
    </resultMap>

    <sql id="Base_Column_List">
        id,role_name,role_auth,
        status,created_at,updated_at,
        remark
    </sql>
    <select id="findAll" resultType="com.test.blogadmin.pojo.Role">
            select * from role
    </select>

    <select id="selectById" resultType="com.test.blogadmin.pojo.Role">
            select * from role where id = #{id}
    </select>

</mapper>

目录结构

补充

上述中的DAO层,Mapper,Service,Impl及resource下mapper中的xml目录可以使用IDEA中的插件MyBatisX生成,MyBatisX的使用和配置将在《IDEA插件MyBatisX的使用》文章中说明


评论