第零步,在pom.xml中新增加generator插件,如图:
    springboot学习-整合mybatis使用Generator自动生成代码 - 图1
    配置代码贴出来:UTF-8UTF-81.8

    1. <dependencies>
    2. <dependency>
    3. <groupId>org.springframework.boot</groupId>
    4. <artifactId>spring-boot-starter-web</artifactId>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.mybatis.spring.boot</groupId>
    8. <artifactId>mybatis-spring-boot-starter</artifactId>
    9. <version>1.3.2</version>
    10. </dependency>
    11. <dependency>
    12. <groupId>org.springframework.boot</groupId>
    13. <artifactId>spring-boot-starter-test</artifactId>
    14. <scope>test</scope>
    15. </dependency>
    16. <dependency>
    17. <groupId>mysql</groupId>
    18. <artifactId>mysql-connector-java</artifactId>
    19. </dependency>
    20. <dependency>
    21. <groupId>com.alibaba</groupId>
    22. <artifactId>druid</artifactId>
    23. <version>1.1.0</version>
    24. </dependency>
    25. <dependency>
    26. <groupId>org.mybatis.generator</groupId>
    27. <artifactId>mybatis-generator-core</artifactId>
    28. <version>1.3.5</version>
    29. </dependency>
    30. </dependencies>
    31. <build>
    32. <plugins>
    33. <plugin>
    34. <groupId>org.springframework.boot</groupId>
    35. <artifactId>spring-boot-maven-plugin</artifactId>
    36. </plugin>
    37. <plugin>
    38. <groupId>org.mybatis.generator</groupId>
    39. <artifactId>mybatis-generator-maven-plugin</artifactId>
    40. <configuration>
    41. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
    42. <overwrite>true</overwrite>
    43. <verbose>true</verbose>
    44. </configuration>
    45. </plugin>
    46. </plugins>
    47. </build>

    第一步,编辑generator文件:

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE generatorConfiguration
    3. PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
    4. "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
    5. <generatorConfiguration>
    6. <!-- 数据库驱动:换成你本地的驱动包位置-->
    7. <classPathEntry location="E:\mysql-connector-java-5.1.46\mysql-connector-java-5.1.46.jar"/>
    8. <context id="DB2Tables" targetRuntime="MyBatis3">
    9. <commentGenerator>
    10. <property name="suppressDate" value="true"/>
    11. <!-- 是否去除自动生成的注释 true:是 : false:否 -->
    12. <property name="suppressAllComments" value="true"/>
    13. </commentGenerator>
    14. <!--数据库链接URL,用户名、密码 -->
    15. <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot?useSSL=false" userId="root" password="1011">
    16. </jdbcConnection>
    17. <javaTypeResolver>
    18. <property name="forceBigDecimals" value="false"/>
    19. </javaTypeResolver>
    20. <!-- 生成模型的包名和位置-->
    21. <javaModelGenerator targetPackage="com.javazhiyin.entity" targetProject="src/main/java">
    22. <property name="enableSubPackages" value="true"/>
    23. <property name="trimStrings" value="true"/>
    24. </javaModelGenerator>
    25. <!-- 生成映射文件的包名和位置-->
    26. <sqlMapGenerator targetPackage="resources.mapper" targetProject="src/main">
    27. <property name="enableSubPackages" value="true"/>
    28. </sqlMapGenerator>
    29. <!-- 生成DAO的包名和位置-->
    30. <javaClientGenerator type="XMLMAPPER" targetPackage="com.javazhiyin.dao" targetProject="src/main/java">
    31. <property name="enableSubPackages" value="true"/>
    32. </javaClientGenerator>
    33. <!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
    34. <table tableName="user_t" domainObjectName="User" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
    35. </context>
    36. </generatorConfiguration>

    此时的项目结构如图所示:
    springboot学习-整合mybatis使用Generator自动生成代码 - 图2

    第二步,运行generator生成代码:

    run-Edit Configurations-点击绿色的加号,选择maven:
    springboot学习-整合mybatis使用Generator自动生成代码 - 图3
    填写name及运行命令mybatis-generator:generate -e:
    springboot学习-整合mybatis使用Generator自动生成代码 - 图4
    第三步,运行generator ,生成代码:

    运行结束之后,如果成功,我们可以看到按照预想已经生成了entity、dao、及Mapper映射文件。
    springboot学习-整合mybatis使用Generator自动生成代码 - 图5
    第四步,编写controller,实现我们所需的功能:

    1. package com.javazhiyin.controller;
    2. import com.javazhiyin.dao.UserMapper;
    3. import com.javazhiyin.entity.User;
    4. import org.springframework.web.bind.annotation.*;
    5. import javax.annotation.Resource;
    6. /**
    7. Created by 57783 on 2018/7/6.
    8. */
    9. @RestController
    10. public class UserController {
    11. @Resource
    12. private UserMapper userMapper;
    13. @GetMapping("/showUser/{id}")
    14. public User getUser(@PathVariable("id") Integer id){
    15. User user = this.userMapper.selectByPrimaryKey(id);
    16. return user;
    17. }
    18. @PostMapping(value = "/addUser")
    19. public void addUser(@RequestParam("username") String username,
    20. @RequestParam("password") String password,
    21. @RequestParam("age") Integer age){
    22. User user = new User();
    23. user.setUserName(username);
    24. user.setPassword(password);
    25. user.setAge(age);
    26. userMapper.insert(user);
    27. }
    28. @PutMapping(value = "updUser/{id}")
    29. public void updUser(@PathVariable("id") Integer id,
    30. @RequestParam("username") String username,
    31. @RequestParam("password") String password,
    32. @RequestParam("age") Integer age){
    33. User user = new User();
    34. user.setId(id);
    35. user.setUserName(username);
    36. user.setPassword(password);
    37. user.setAge(age);
    38. userMapper.updateByPrimaryKey(user);
    39. }
    40. @DeleteMapping(value = "delUser/{id}")
    41. public void delUser(@PathVariable("id") Integer id){
    42. userMapper.deleteByPrimaryKey(id);
    43. }
    44. }

    最后,我们修改Bootdemo04Application启动类,添加@MapperScan注解,使其可以扫描DAO层接口:

    1. package com.javazhiyin;
    2. import org.mybatis.spring.annotation.MapperScan;
    3. import org.springframework.boot.SpringApplication;
    4. import org.springframework.boot.autoconfigure.SpringBootApplication;
    5. @SpringBootApplication
    6. @MapperScan("com.javazhiyin.dao")
    7. public class Bootdemo04Application {
    8. public static void main(String[] args) {
    9. SpringApplication.run(Bootdemo04Application.class, args);
    10. }
    11. }

    只有这四个增删改查可能不会感觉多方便,如果有大量的表,省去我们编写基本的DAO、Entity及Mapper映射,回非常的nice.

    注:本文转载自https://www.javazhiyin.com/,若有侵权