zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Android Studio cannot launch avd in emulator问题解决
    ios设备安装破解软件及自己下载的软件ipa教程
    解决数据库连接错误 您在wp-config.php文件中提供的数据库用户名和密码可能不正确,或者无法连接到localhost上的数据库服务器,这意味着您的主机数据库服务器已停止工作。
    地图工具类大集合
    在家云看景点的皮蛋TV
    在线本地视频翻译
    南瓜影视特权
    韩剧tv特权来了
    网盘下载合集,宽带多快,我多快
    白嫖永久免费电脑端美图秀秀
  • 原文地址:https://www.cnblogs.com/jycjy/p/10683299.html
Copyright © 2011-2022 走看看