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

时间:2023-12-15 22:38:02

1.使用idea新建项目

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

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.项目结构

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

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 启动验证

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

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

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