数据库三剑客

该文章介绍了如何在SpringBoot项目中集成MySQL数据库、Druid连接池以及MyBatis框架,并展示了在application.yml中的配置。同时推荐了FreMyBatisTool工具,它能自动生成包括实体类、DAO接口及其XML文件在内的增删改查代码,提高开发效率。
摘要由CSDN通过智能技术生成

1.数据库三剑客分别是 mysql驱动包、mybatis-spring-boot-starter、druid连接池

 <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.6</version>
        </dependency>

2.在applicaton.yml中的配置,适合通用模块

server:
  port: 80

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/blog?serverTimezone=UTC
    username: root
    password: root
    type: com.alibaba.druid.pool.DruidDataSource
mybatis:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
    map-underscore-to-camel-case: true
  mapper-locations: classpath*:dao/*.xml

3.推荐一个代码自动生成器

这个Fre MyBatis Tool可以自动帮你生成增删改查代码

 

 

 这里面package是包路径,可以更改为自己的包路径

 然后就自动给我们生成了代码

 

1.实体类

package com.lh.pojo.entity;

import java.io.Serializable;
import java.util.Date;
import lombok.Data;

/**
 * article
 * @author 
 */
@Data
public class Article implements Serializable {
    private Integer id;

    /**
     * 标题
     */
    private String title;

    /**
     * 关键字
     */
    private String keywords;

    /**
     * 描述
     */
    private String desci;

    /**
     * 图片地址
     */
    private String pic;

    /**
     * 内容
     */
    private String content;

    /**
     * 点击量
     */
    private Integer click;

    /**
     * 发表日期
     */
    private Date time;

    private Integer catalogId;

    private static final long serialVersionUID = 1L;
}

2.dao

public interface ArticleDao {
    int deleteByPrimaryKey(Integer id);

    int insert(Article record);

    int insertSelective(Article record);

    Article selectByPrimaryKey(Integer id);

    int updateByPrimaryKeySelective(Article record);

    int updateByPrimaryKey(Article record);

    List<Article> selectist();
}

3.ArticleDao.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.lh.dao.ArticleDao">
  <resultMap id="BaseResultMap" type="com.lh.pojo.entity.Article">
    <id column="id" jdbcType="INTEGER" property="id" />
    <result column="title" jdbcType="VARCHAR" property="title" />
    <result column="keywords" jdbcType="VARCHAR" property="keywords" />
    <result column="desci" jdbcType="VARCHAR" property="desci" />
    <result column="pic" jdbcType="VARCHAR" property="pic" />
    <result column="content" jdbcType="VARCHAR" property="content" />
    <result column="click" jdbcType="INTEGER" property="click" />
    <result column="time" jdbcType="TIMESTAMP" property="time" />
    <result column="catalog_id" jdbcType="INTEGER" property="catalogId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, title, keywords, desci, pic, content, click, `time`, catalog_id
  </sql>
  <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
    select 
    <include refid="Base_Column_List" />
    from article
    where id = #{id,jdbcType=INTEGER}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
    delete from article
    where id = #{id,jdbcType=INTEGER}
  </delete>
  <insert id="insert" keyColumn="id" keyProperty="id" parameterType="com.lh.pojo.entity.Article" useGeneratedKeys="true">
    insert into article (title, keywords, desci, 
      pic, content, click, 
      `time`, catalog_id)
    values (#{title,jdbcType=VARCHAR}, #{keywords,jdbcType=VARCHAR}, #{desci,jdbcType=VARCHAR}, 
      #{pic,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{click,jdbcType=INTEGER}, 
      #{time,jdbcType=TIMESTAMP}, #{catalogId,jdbcType=INTEGER})
  </insert>
  <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.lh.pojo.entity.Article" useGeneratedKeys="true">
    insert into article
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="title != null">
        title,
      </if>
      <if test="keywords != null">
        keywords,
      </if>
      <if test="desci != null">
        desci,
      </if>
      <if test="pic != null">
        pic,
      </if>
      <if test="content != null">
        content,
      </if>
      <if test="click != null">
        click,
      </if>
      <if test="time != null">
        `time`,
      </if>
      <if test="catalogId != null">
        catalog_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="title != null">
        #{title,jdbcType=VARCHAR},
      </if>
      <if test="keywords != null">
        #{keywords,jdbcType=VARCHAR},
      </if>
      <if test="desci != null">
        #{desci,jdbcType=VARCHAR},
      </if>
      <if test="pic != null">
        #{pic,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        #{content,jdbcType=VARCHAR},
      </if>
      <if test="click != null">
        #{click,jdbcType=INTEGER},
      </if>
      <if test="time != null">
        #{time,jdbcType=TIMESTAMP},
      </if>
      <if test="catalogId != null">
        #{catalogId,jdbcType=INTEGER},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="com.lh.pojo.entity.Article">
    update article
    <set>
      <if test="title != null">
        title = #{title,jdbcType=VARCHAR},
      </if>
      <if test="keywords != null">
        keywords = #{keywords,jdbcType=VARCHAR},
      </if>
      <if test="desci != null">
        desci = #{desci,jdbcType=VARCHAR},
      </if>
      <if test="pic != null">
        pic = #{pic,jdbcType=VARCHAR},
      </if>
      <if test="content != null">
        content = #{content,jdbcType=VARCHAR},
      </if>
      <if test="click != null">
        click = #{click,jdbcType=INTEGER},
      </if>
      <if test="time != null">
        `time` = #{time,jdbcType=TIMESTAMP},
      </if>
      <if test="catalogId != null">
        catalog_id = #{catalogId,jdbcType=INTEGER},
      </if>
    </set>
    where id = #{id,jdbcType=INTEGER}
  </update>
  <update id="updateByPrimaryKey" parameterType="com.lh.pojo.entity.Article">
    update article
    set title = #{title,jdbcType=VARCHAR},
      keywords = #{keywords,jdbcType=VARCHAR},
      desci = #{desci,jdbcType=VARCHAR},
      pic = #{pic,jdbcType=VARCHAR},
      content = #{content,jdbcType=VARCHAR},
      click = #{click,jdbcType=INTEGER},
      `time` = #{time,jdbcType=TIMESTAMP},
      catalog_id = #{catalogId,jdbcType=INTEGER}
    where id = #{id,jdbcType=INTEGER}
  </update>

  <select id="selectist" resultMap="BaseResultMap">
    select <include refid="Base_Column_List" />
    from article
  </select>
</mapper>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值