• springboot+mysql+mybatis+Mybatis-Generator+druid 项目demo


    1.使用idea新建项目

    2.使用Mybatis-Generator自动生成Dao、Model、Mapping相关文件

    3.配置application.yml文件

    server:
      port: 8080
    
    spring:
      datasource:
       driver-class-name: com.mysql.jdbc.Driver
       url: jdbc:mysql://127.0.0.1:3306/day01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
       username: root
       password: ......
       # 使用druid数据源
       type: com.alibaba.druid.pool.DruidDataSource
    
    mybatis:
      type-aliases-package: com.example.duowei.mapper
      mapper-locations: classpath:mapper/*.xml
    

    4.项目结构

    5.各个部分的内容

    1.AccountController

    package com.example.test.controller;
    
    import com.example.test.Service.AccountService;
    import com.example.test.entity.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.util.List;
    
    
    @RestController
    @RequestMapping("")
    public class AccountController {
        @Autowired
        private AccountService accountService;
    
        @GetMapping("/select/list")
        public List<Account> selectUserList() {
            return this.accountService.selectAccountList();
        }
    }
    

      2.AccountService

    package com.example.test.Service;
    
    import com.example.test.dao.AccountMapper;
    import com.example.test.entity.Account;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import java.util.List;
    
    @Service
    public class AccountService {
        @Autowired
        private AccountMapper accountMapper;
        public List<Account> selectAccountList() {
            return accountMapper.selectAccountList();
        }
    }
    

      3.AccountMapper

    package com.example.test.dao;
    
    import com.example.test.entity.Account;
    import java.util.List;
    
    import org.apache.ibatis.annotations.*;
    
    @Mapper
    public interface AccountMapper {
        /**
         * 查詢所有的賬戶信息
         * @return
         */
        public List<Account> selectAccountList();
    }
    

     4.AccountMapper.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.example.test.dao.AccountMapper">
      <resultMap id="BaseResultMap" type="com.example.test.entity.Account">
        <!--
          WARNING - @mbggenerated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        <constructor>
          <idArg column="id" javaType="java.lang.Integer" jdbcType="INTEGER" />
          <arg column="USERNAME" javaType="java.lang.String" jdbcType="VARCHAR" />
          <arg column="MONEY" javaType="java.lang.Integer" jdbcType="INTEGER" />
        </constructor>
      </resultMap>
      <sql id="Base_Column_List">
        <!--
          WARNING - @mbggenerated
          This element is automatically generated by MyBatis Generator, do not modify.
        -->
        id, USERNAME, MONEY
      </sql>
    
      <select id="selectAccountList" resultMap="BaseResultMap"  >
         SELECT
            <include refid="Base_Column_List" />
          FROM `account`
      </select>
    </mapper>
    

      ok 启动验证

    数据库部分比较简单,随便建立一张表就可以。

    另外项目已经上传至github,附上链接 https://github.com/Eric-chenjy/springboot-mysql-mybatis-Mybatis-Generator-druid-demo.git

  • 相关阅读:
    电池的并联与串联
    [转]为什么我会认为SAP是世界上最好用最牛逼的ERP系统,没有之一?
    go module
    thinkPHP5.1自动生成目录结构
    java多线程-锁分析
    Walle 2.0(瓦力)的安装
    轻量日志系统Loki
    Zabbix5.0的安装(超详细)
    政策制定的艺术
    浅谈对golang中的defer,panic,recover理解
  • 原文地址:https://www.cnblogs.com/jycjy/p/10683299.html
Copyright © 2020-2023  润新知