1.创建SpringBoot项目
1.1 建一个名为mybatis_test的项目
1.2 选择项目要用到的依赖
1.3 把DemoApplication.java放到example包下
1.4 编写application.properties文件配置好mybatis连接数据库
2. 整合Mybatis逆向工程
2.1 在pom.xml中引入两个依赖,依赖如下:
<!-- mybatis逆向工程jar包 -->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</plugin>
2.2 在resources包下创建一个generator文件夹,然后在文件夹中创建generatorConfig.xml文件
2.3 在generatorConfig.xml配置文件填写以下配置:
根据注释修改成自己需要的配置
<?xml version="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">
<generatorConfiguration>
<context id="testTables" targetRuntime="MyBatis3">
<!-- 生成toString -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<commentGenerator>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?useSSL=FALSE&serverTimezone=UTC"
userId="root"
password="root">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和
NUMERIC 类型解析为java.math.BigDecimal -->
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetProject:生成POJO类的位置 employment -->
<javaModelGenerator targetPackage="com.example.bean"
targetProject="D:\springboot\mybatis_test\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
<!-- 从数据库返回的值被清理前后的空格 -->
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- targetProject:mapper映射文件生成的位置 -->
<sqlMapGenerator targetPackage="mapper"
targetProject="D:\springboot\mybatis_test\src\main\resources">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- targetPackage:mapper接口生成的位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.example.mapper"
targetProject="D:\springboot\mybatis_test\src\main\java">
<!-- enableSubPackages:是否让schema作为包的后缀 -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定数据库表 -->
<table tableName="user"></table>
<!-- <table tableName="comment"></table>-->
<!-- <table tableName="like"></table>-->
<!-- <table tableName="status"></table>-->
<!-- <table tableName="statusimg"></table>-->
<!-- 有些表的字段需要指定java类型
<table schema="" tableName="">
<columnOverride column="" javaType="" />
</table> -->
</context>
</generatorConfiguration>
2.4 点击右侧的Maven找到mybatis-generator插件双击运行
2.5 完成结果
出现 BUILD SUCCESS则运行成功,同时可以看到到所需的文件已经自动生成
2.6 结果效果
User.java、UserMapper.java、UserExample.java、UserMapper.xml显示已生成
3.Mybatis逆向工程的使用
user表结构
UserController.java
package com.example.controller;
import com.example.bean.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/findUserById")
public HashMap<String, Object> findUserById(HttpServletRequest request) {
String openid = request.getParameter("openid");
User userById = userService.findUserById(openid);
HashMap<String, Object> map = new HashMap<>();
map.put("result", userById);
return map;
}
@RequestMapping("/findAllUser")
public HashMap<String, Object> findAllUser() {
HashMap<String, Object> map = new HashMap<>();
List<User> allUser = userService.findAllUser();
map.put("result", allUser);
return map;
}
@RequestMapping("/addUser")
public HashMap<String, Object> addUser(HttpServletRequest request) {
HashMap<String, Object> map = new HashMap<>();
String openid = request.getParameter("openid");
String nickName = request.getParameter("nickName");
User user = new User();
user.setOpenid(openid);
user.setNickname(nickName);
int i = userService.addUser(user);
map.put("result", i);
return map;
}
@RequestMapping("/deleteUserById")
public HashMap<String, Object> deleteUserById(HttpServletRequest request) {
String openid = request.getParameter("openid");
HashMap<String, Object> map = new HashMap<>();
int i = userService.deleteUserById(openid);
map.put("result", i);
return map;
}
@RequestMapping("/updateSignById")
public HashMap<String, Object> updateSignById(HttpServletRequest request) {
HashMap<String, Object> map = new HashMap<>();
String openid = request.getParameter("openid");
String sign = request.getParameter("sign");
User user = new User();
user.setOpenid(openid);
user.setSign(sign);
int i = userService.updateSignById(user);
map.put("result", i);
return map;
}
}
UserServiceImpl.java
package com.example.service.impl;
import com.example.bean.User;
import com.example.bean.UserExample;
import com.example.mapper.UserMapper;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService {
@Autowired
UserMapper userMapper;
@Override
public User findUserById(String openid) {
//根据主键查询
return userMapper.selectByPrimaryKey(openid);
}
@Override
public List<User> findAllUser() {
//不添加条件查全部
UserExample userExample = new UserExample();
return userMapper.selectByExample(userExample);
}
@Override
public int addUser(User user) {
//插入所有字段
// int i = userMapper.insert(user);
int i = userMapper.insertSelective(user);
return i;
}
@Override
public int deleteUserById(String openid) {
// 根据主键删除
// int i = userMapper.deleteByPrimaryKey(openid);
//根据条件删除
UserExample userExample = new UserExample();
userExample.createCriteria().andOpenidEqualTo(openid);
int i = userMapper.deleteByExample(userExample);
return i;
}
@Override
public int updateSignById(User user) {
//有选择性的根据主键更新
int i = userMapper.updateByPrimaryKeySelective(user);
return i;
}
}
注意:mapper接口文件得手动添加@Mapper注解