Bootstrap

spring整合mybatis和逆向工程

1.导入相关依赖

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.2</version>
        </dependency> 
         
    <build>
        <plugins>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.4.2</version>
                <configuration>
                    <verbose>true</verbose>
                    <overwrite>true</overwrite>
                    <configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>org.mybatis</groupId>
                        <artifactId>mybatis</artifactId>
                        <version>3.5.13</version>
                    </dependency>
                    <dependency>
                        <groupId>com.mysql</groupId>
                        <artifactId>mysql-connector-j</artifactId>
                        <version>8.0.33</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

2.服务的相关配置

#服务的相关配置
server.port=8080
# 默认是/
server.servlet.context-path=/boot
#视图解析器,用不上了
#spring.mvc.view.suffix=
#spring.mvc.view.prefix=

#静态资源配置
#静态资源路径
#spring.mvc.static-path-pattern=/**
#是否支持依赖循环引用
#spring.main.allow-circular-references=true

#导入了JDBC的启动器,必须配置数据源,否则无法启动
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/mybatis

mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

3.在spring中配置mybatis

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--    开启注解-->
    <context:component-scan base-package="com.lanou"></context:component-scan>

    <context:property-placeholder location="classpath:application.properties" />
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="url" value="jdbc:mysql://localhost:3306/mybatis"></property>
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="password" value="root"></property>
        <property name="username" value="root"></property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <property name="basePackage" value="com.lanou.mapper"></property>
    </bean>

<!--    事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
<!--    注解式事务-->
    <tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
</beans>

4.配置逆向工程

<?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>
    <properties resource="application.properties"></properties>
    <context id="mysql" targetRuntime="MyBatis3Simple">
        <!--        插件-->
        <plugin type="org.mybatis.generator.plugins.SerializablePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"></plugin>
        <plugin type="org.mybatis.generator.plugins.ToStringPlugin"></plugin>
        <!--        覆盖原来的映射文件中的内容,而不是新增-->
        <plugin type="org.mybatis.generator.plugins.UnmergeableXmlMappersPlugin"></plugin>
        <!--        取消默认的注释-->
        <commentGenerator>
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>
        <!--        连接数据库-->
        <jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
                        connectionURL="jdbc:mysql://localhost:3306/mybatis"
                        userId="root"
                        password="root"></jdbcConnection>
        <!--        表字段类型和实体类中属性类型转换-->
        <javaTypeResolver>
            <property name="forceBigDecimals" value="true"/>
        </javaTypeResolver>
        <!--        生成实体类-->
        <javaModelGenerator targetPackage="com.lanou.model" targetProject="src/main/java"></javaModelGenerator>
        <!--        生成映射文件-->
        <sqlMapGenerator targetPackage="com.lanou.mapper" targetProject="src/main/resources"></sqlMapGenerator>
        <!--    生成接口-->
        <javaClientGenerator type="XMLMAPPER" targetPackage="com.lanou.mapper" targetProject="src/main/java"></javaClientGenerator>
        <!--        数据库表-->
        <table tableName="person" domainObjectName="Person"></table>
    </context>
</generatorConfiguration>

5.点击右侧Maven按钮,即可开始自动生成本局实体类,接口,映射文件

在这里插入图片描述

;