spring整合系列学习笔记——springBoot整合mybatis

xiaoxiao2021-02-28  14

三、springBoot整合mybatis逆向工程

新建springBoot工程

1.【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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com</groupId> <artifactId>itcloud</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>itcloud</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>RELEASE</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.5</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <!--打包插件--> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- 反向工程插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.5</version> <configuration> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build> </project>

2.【resources/generatorConfig.xml】建立此文件,文件名必须为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"> <!-- 配置文件需要注意的地方:因为是maven插件,所以必须使用generatorConfig.xml文件作为文件的名称 --> <generatorConfiguration> <!--定义mysql驱动jar的位置,绝对路径--> <classPathEntry location="C:\Users\DELL\.m2\repository\mysql\mysql-connector-java\5.1.40\mysql-connector-java-5.1.40.jar"/> <!--数据库连接--> <context id="mysql" targetRuntime="MyBatis3"> <commentGenerator > <property name="suppressDate" value="true"/> <property name="suppressAllComments" value="true"/> </commentGenerator> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8" userId="root" password="admin"> </jdbcConnection> <javaTypeResolver> <property name="forceBigDecimals" value="false"/> </javaTypeResolver> <!--实体类生成策略--> <javaModelGenerator targetPackage="com.itcloud.pojo" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true"/> <property name="trimStrings" value="true"/> <!--是否添加构造函数--> <property name="constructorPackages" value="true"/> </javaModelGenerator> <!--*Mapper.xml文件生成策略--> <sqlMapGenerator targetPackage="mybatis" targetProject=".\src\main\resources"> <property name="enableSubPackages" value="true"/> </sqlMapGenerator> <!--dao层生成策略,*Mapper.java生成策略--> <javaClientGenerator type="XMLMAPPER" targetPackage="com.itcloud.mapper" targetProject=".\src\main\java"> <property name="enableSubPackages" value="true"/> </javaClientGenerator> <!--此处不生成example的目的是不想用自动生成的example,因为这完全可以自己来控制sql语句,会让程序显得更加简单 --> <table tableName="goods" domainObjectName="Goods" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"> <columnOverride column="detail" jdbcType="VARCHAR"/> </table> <table tableName="orders" domainObjectName="Orders" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>

3.命令行进入目录:/spring-boot-mybatis/,执行maven命令

F:\spring-root\spring-boot-mybatis> F:\spring-root\spring-boot-mybatis> mvn mybatis-generator:generate

4.生成反向工程的文件,文件结构如下

反向工程生成的 Mapper.java无法被springBoot自动注入,需要在类上面添加@Mapper*注解,或者在程序的主类上面进行先关包的扫描

演示:下面二者用其一即可

​ 1.@Mapper

@Mapper public interface GoodsMapper { //此处内容被省略 }

​ 2.@org.mybatis.spring.annotation.MapperScan(“com.itcloud.mapper”)

package com.itcloud; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.itcloud.mapper") public class ItcloudApplication { public static void main(String[] args) { SpringApplication.run(ItcloudApplication.class, args); } }

6.配置文件编写:【resources/application.yml】

server: port: 9090 spring: datasource: type: com.alibaba.druid.pool.DruidDataSource url: jdbc:mysql://localhost:3306/spring?characterEncoding=utf-8 driver-class-name: com.mysql.jdbc.Driver username: root password: admin # 整合mybatis mybatis: type-aliases-package: com.itcloud.pojo mapper-locations: classpath:mybatis/*.xml pagehelper: helper-dialect: mysql # responable进行合理化分页,如果pageNum<=0时候会查询第一页的数据。如果pageNum>总页数,会查询最后一页的数据。 reasonable: true supportMethodsArguments: true params: count=countSql

5.springBoot测试

package com.itcloud; import java.math.BigDecimal; import java.util.List; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.itcloud.mapper.GoodsMapper; import com.itcloud.pojo.Goods; @RunWith(SpringRunner.class) @SpringBootTest(classes = ItcloudApplication.class) public class ItcloudApplicationTests { @Autowired private GoodsMapper goodsMapper; @Test public void contextLoads() { Goods goods = goodsMapper.selectByPrimaryKey(3); System.out.println(goods.getDetail());//结果:一款适合所有篮球爱好者的篮球,朴素的外观不失优雅 } //批量插入,可以直接在数据库中查看 @Test public void insert() { for(int x=1 ;x<101 ;x++){ Goods good = new Goods() ; good.setPrice(new BigDecimal("44.39").add(new BigDecimal(x/10))); good.setTitle("商品"+x); good.setDetail("商品"+x+",很实惠"); goodsMapper.insert(good) ; } } //分页 @Test public void split() { PageHelper.startPage(1,10) ; List<Goods> goods = goodsMapper.selectAll(); PageInfo pageInfo = new PageInfo(goods); System.out.println(pageInfo.getPages()); //结果: 11 } }
转载请注明原文地址: https://www.6miu.com/read-2400127.html

最新回复(0)