spring boot 整合Mybatis(逆向工程)

 

创建项目

导入依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.5.3</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.example</groupId>
   <artifactId>springboot-thymeleaf</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>springboot-thymeleaf</name>
   <description>Demo project for Spring Boot</description>
<!--   版本控制-->
   <properties>
      <java.version>1.8</java.version>
      <repackage.classifier/>
      <spring-native.version>0.10.2-SNAPSHOT</spring-native.version>
   </properties>
   <dependencies>
<!--      web-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-web</artifactId>
      </dependency>
<!--      模板引擎-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-thymeleaf</artifactId>
      </dependency>
<!--      热部署工具-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-devtools</artifactId>
         <scope>runtime</scope>
         <optional>true</optional>
      </dependency>
<!--      mysql驱动 默认8.0-->
      <dependency>
         <groupId>mysql</groupId>
         <artifactId>mysql-connector-java</artifactId>
      </dependency>
<!--      yaml使用有提示-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-configuration-processor</artifactId>
         <optional>true</optional>
      </dependency>
<!--      lombok-->
      <dependency>
         <groupId>org.projectlombok</groupId>
         <artifactId>lombok</artifactId>
         <optional>true</optional>
      </dependency>
<!--      测试-->
      <dependency>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-test</artifactId>
         <scope>test</scope>
      </dependency>
<!--      druid的starter自动配置(包含了druid的jar)-->
      <dependency>
         <groupId>com.alibaba</groupId>
         <artifactId>druid-spring-boot-starter</artifactId>
         <version>1.1.17</version>
      </dependency>
      <!--   log4j-->
      <!-- https://mvnrepository.com/artifact/log4j/log4j -->
      <dependency>
         <groupId>log4j</groupId>
         <artifactId>log4j</artifactId>
         <version>1.2.17</version>
      </dependency>
<!--      整合mybatis 导入mybatis-->
      <dependency>
         <groupId>org.mybatis.spring.boot</groupId>
         <artifactId>mybatis-spring-boot-starter</artifactId>
         <version>2.2.0</version>
      </dependency>
   <!-- mybatis逆向工程jar包 -->
      <dependency>
         <groupId>org.mybatis.generator</groupId>
         <artifactId>mybatis-generator-core</artifactId>
         <version>1.3.4</version>
      </dependency>
   </dependencies>
   <build>
      <plugins>
         <plugin>
<!--            maven打包-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
               <excludes>
                  <exclude>
                     <groupId>org.projectlombok</groupId>
                     <artifactId>lombok</artifactId>
                  </exclude>
               </excludes>
            </configuration>
         </plugin>
<!--         逆向工程插件-->
         <plugin>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-maven-plugin</artifactId>
            <version>1.3.2</version>
            <configuration>
               <overwrite>true</overwrite>   
<!--                这里是maven启动的时候寻找xml的地址-->            <configurationFile>src/main/resources/generator/generatorConfig.xml</configurationFile>
            </configuration>
         </plugin>
      </plugins>
   </build>

</project>

        

创建逆向工程的.xml

<!DOCTYPE generatorConfiguration PUBLIC
        "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
    <!-- 数据库驱动:选择你的本地硬盘在maven下载的包上面的数据库驱动包-->
    <classPathEntry location="E:\maven\mysql\mysql-connector-java\8.0.26\mysql-connector-java-8.0.26.jar"/>
    <context id="simple" targetRuntime="MyBatis3Simple">
<!--        不要生成注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true" />
        </commentGenerator>
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC"
                        password="123456"
                        userId="root"
        />
        <!-- pojo-->
        <javaModelGenerator targetPackage="com.example.pojo" targetProject="src/main/java"/>
        <!--sqlMapGenerator:sql映射文件生成器;指定xml生成的地方  -->
        <sqlMapGenerator targetPackage="Mapper" targetProject="src/main/resources/"/>
        <!-- javaClientGenerator:dao接口生成的地方 -->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.example.dao" targetProject="src/main/java"/>
        <!--数据库表名生成的pojo类名-->
        <table tableName="t_product" domainObjectName="Product"/>
    </context>
</generatorConfiguration>

 

 

 就执行成功了

创建MybatisConfig,xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
</configuration>

 创建application.yaml配置数据源(xml也可以)

#配置数据源
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mybatis?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

#配置mybatis
mybatis:
 config-location: classpath:Mybatis/mybatisConfig.xml #mybatis文件配置地址
  mapper-locations: classpath:Mybatis/Mapper/*.xml    #mapper文件配置地址

测试代码

@Service
public class mybatisTest {
    @Autowired
    ProductMapper mapper;

    /**
     * 测试整合mybatis查询id
     * @param id
     * @return
     */
    public Product selectByPrimaryKey(Integer id){
        return mapper.selectByPrimaryKey(id);
    }
}

控制器

@Controller
public class mybstisTest {

    @Autowired
    mybatisTest mybatisTest;

    @GetMapping("mybatis")
    @ResponseBody
    public Product selectByPrimaryKey(@RequestParam(value = "id",required = false) Integer id ){
        return mybatisTest.selectByPrimaryKey(id);
    }
}

就执行成功啦 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水果不是橙子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值