初始化项目
- com.example.mybatisgeneratordemo
最终的文件目录结构
配置pom.xml文件
"1.0" encoding="UTF-8"?>
"http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.5.RELEASE
com.example
mybatis-generator-demo
0.0.1-SNAPSHOT
mybatis-generator-demo
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
mysql
mysql-connector-java
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.7
mysql
mysql-connector-java
8.0.15
org.mybatis.generator
mybatis-generator-core
1.3.7
Generate MyBatis Artifacts
package
generate
true
true
src/main/resources/mybatis/generatorConfig.xml
配置appliaction.yml
server:
port: 8051
servlet:
context-path: /springboot
mybatis:
# 对应实体类的包名
type-aliases-package: com.komiles.study.domain
mapper-locations: classpath:mybatis/mapper/*.xml
config-location: classpath:mybatis/mybatis-config.xml
spring:
datasource:
url: jdbc:mysql://127.0.0.1:3306/komo?characterEncoding=utf-8
username: root
password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver
配置generatorConfig.xml
"1.0" encoding="UTF-8"?>
DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
"Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
"autoDelimitKeywords" value="true" />
"beginningDelimiter" value="" />
"endingDelimiter" value="
" />
"javaFileEncoding" value="utf-8" />
"org.mybatis.generator.plugins.SerializablePlugin" />
"org.mybatis.generator.plugins.ToStringPlugin" />
"suppressAllComments" value="true"/>
"suppressDate" value="true" />
"com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/komo?characterEncoding=utf-8&nullCatalogMeansCurrent=true"
userId="root"
password="123456">
"forceBigDecimals" value="false"/>
"com.example.mybatisgeneratordemo.domain" targetProject="src/main/java">
"enableSubPackages" value="true"/>
"trimStrings" value="true"/>
"mapper" targetProject="src/main/resources/mybatis" >
"enableSubPackages" value="false" />
"com.example.mybatisgeneratordemo.mapper" targetProject="src/main/java" type="XMLMAPPER" >
"enableSubPackages" value="false" />
"t_user" domainObjectName="User">
"id" sqlStatement="Mysql" identity="true" />
mybatis-generator 自动生成文件
service层
我们需要做的是 写service层逻辑
UserService.java
package com.example.mybatisgeneratordemo.service;
import com.example.mybatisgeneratordemo.domain.User;
/**
* @author komiles@163.com
* @date 2020-03-22 23:05
*/
public interface UserService {
User getUser(Integer id);
}
UserServiceImpl.java
package com.example.mybatisgeneratordemo.service.impl;
import com.example.mybatisgeneratordemo.domain.User;
import com.example.mybatisgeneratordemo.mapper.UserMapper;
import com.example.mybatisgeneratordemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* @author komiles@163.com
* @date 2020-03-22 22:58
*/
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;
@Override
public User getUser(Integer id) {
return userMapper.selectByPrimaryKey(id);
}
}
Controller 层
UserController.java
package com.example.mybatisgeneratordemo.controller;
import com.example.mybatisgeneratordemo.domain.User;
import com.example.mybatisgeneratordemo.service.UserService;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* @author komiles@163.com
* @date 2020-03-22 23:08
*/
@RequestMapping("/test")
@RestController
public class UserController {
@Autowired
private UserService userService;
@GetMapping("/userInfo")
public User getUserInfo(@RequestParam("user_id") Integer id)
{
System.out.println(id);
return userService.getUser(id);
}
}
如果无法加载
需要在启动文件上添加@MapperScan注解
访问地址
地址:http://127.0.0.1:8051/springboot/test/userInfo?user_id=1
展示效果:
项目demo地址:https://github.com/KoMiles/spring-example/tree/master/mybatis-demo
Original: https://www.cnblogs.com/wangkongming/p/12549328.html
Author: KoMiles
Title: 【spring-boot】mybatis-generator 使用入门
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/539708/
转载文章受原作者版权保护。转载请注明原作者出处!