继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

(五)SpringBoot2.0基础篇- Mybatis与插件生成代码

月关宝盒
关注TA
已关注
手记 379
粉丝 105
获赞 673

SpringBoot与Mybatis合并

  一、创建SpringBoot项目,引入相关依赖包:

复制代码

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.cn</groupId>
  <artifactId>spring-boot-mybatis</artifactId>
  <version>1.0-SNAPSHOT</version>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
  </properties>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.0.1.RELEASE</version>
  </parent>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!-- mysql 数据库驱动. -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>

    <!-- spring-boot mybatis依赖 -->
    <dependency>
      <groupId>org.mybatis.spring.boot</groupId>
      <artifactId>mybatis-spring-boot-starter</artifactId>
      <version>1.3.2</version>
    </dependency>

  </dependencies>

  <build>
    <plugins>
      <!-- 打包插件 -->
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>

      <plugin>
        <!--Mybatis-generator插件,用于自动生成Mapper和POJO-->
        <groupId>org.mybatis.generator</groupId>
        <artifactId>mybatis-generator-maven-plugin</artifactId>
        <version>1.3.2</version>
        <configuration>
          <!--配置文件的位置-->
          <configurationFile>
            src/main/resources/mybatis/mybatis-generator.xml          </configurationFile>
          <verbose>true</verbose>
          <overwrite>true</overwrite>
        </configuration>
        <executions>
          <execution>
            <id>Generate MyBatis Artifacts</id>
            <goals>
              <goal>generate</goal>
            </goals>
          </execution>
        </executions>
        <dependencies>
          <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
          </dependency>
          <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.3.2</version>
          </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>

</project>

复制代码

  项目目录:

  https://img2.mukewang.com/5b1667ef00013dd204080401.jpg

  二、创建mybatis-generator.xml生成器配置文件(在lib目录中放入相应的mysql连接器mysql-connector-java-5.1.46.jar):

复制代码

<?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>
    <!-- 数据库驱动 -->
    <classPathEntry location="./lib/mysql-connector-java-5.1.46.jar"/>

    <context id="DB2Tables" targetRuntime="MyBatis3">
        <commentGenerator>
            <property name="suppressDate" value="true"/>
            <!-- 是否去除自动生成的注释 true:是 ; false:否 -->
            <property name="suppressAllComments" value="true"/>
        </commentGenerator>

        <!--数据库链接URL,用户名、密码 -->
         <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://192.168.1.121:3306/test?useSSL=false"
            userId="root" password="admincss"> </jdbcConnection>

        <!--数据库链接URL,用户名、密码 -->
        <!--<jdbcConnection driverClass="oracle.jdbc.driver.OracleDriver"
                        connectionURL="jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=FTdevdb02.dafycredit.com)(PORT=1521))(CONNECT_DATA=(SERVER=dedicated)(SERVICE_NAME=devdb02)))"
                        userId="wechat" password="Test$20150104">
        </jdbcConnection>-->

        <javaTypeResolver>
            <property name="forceBigDecimals" value="false"/>
        </javaTypeResolver>

        <!-- 生成模型的包名和位置 -->
        <javaModelGenerator targetPackage="com.cn.entity"
                            targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
            <property name="trimStrings" value="true"/>
        </javaModelGenerator>

        <!-- 生成映射文件的包名和位置 -->
        <sqlMapGenerator targetPackage="mybatis.mapper"
                         targetProject="src/main/resources">
            <property name="enableSubPackages" value="true"/>
        </sqlMapGenerator>

        <!-- 生成DAO的包名和位置 -->
        <javaClientGenerator type="XMLMAPPER"
                             targetPackage="com.cn.mapper"
                             targetProject="src/main/java">
            <property name="enableSubPackages" value="true"/>
        </javaClientGenerator>


        <!-- 要生成哪些表 -->
        <table tableName="student" domainObjectName="Student"
               enableCountByExample="false" enableUpdateByExample="false"
               enableDeleteByExample="false" enableSelectByExample="false"
               selectByExampleQueryId="false">
        </table>

    </context>
</generatorConfiguration>

复制代码

  由于上一步已添加相关的插件依赖,只需在maven管理的plugins中启动mybatis-generator:

  https://img1.mukewang.com/5b1667f80001281106200565.jpg

  三、创建application.properties、controller、service:

复制代码

spring.datasource.url=jdbc:mysql://192.168.1.121:3306/testspring.datasource.username=root
spring.datasource.password=admincss
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#必须配置,否则会找不到映射的xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml

复制代码

StudentController.java

StudentService.java

StudentServiceImpl.java

  四、在生成的Mapper类中,添加@Mapper注解,目的在于将该Mapper添加进spring容器中或者可以在启动类中配置@MapperScan扫描范围:

复制代码

package com.cn.mapper;import com.cn.entity.Student;import org.apache.ibatis.annotations.Mapper;

@Mapperpublic interface StudentMapper {    int deleteByPrimaryKey(Integer id);    int insert(Student record);    int insertSelective(Student record);

    Student selectByPrimaryKey(Integer id);    int updateByPrimaryKeySelective(Student record);    int updateByPrimaryKey(Student record);
}

复制代码

  五、启动,测试:

https://img3.mukewang.com/5b166801000157ad12800800.jpg

 

 

代码实例:https://gitee.com/lfalex/spring-boot-example/tree/dev/spring-boot-mybatis

原文出处

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP