新建springBoot工程
<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.0modelVersion>
<groupId>comgroupId>
<artifactId>itcloudartifactId>
<version>0.0.1-SNAPSHOTversion>
<packaging>jarpackaging>
<name>itcloudname>
<description>Demo project for Spring Bootdescription>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>1.5.9.RELEASEversion>
<relativePath/>
parent>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
<java.version>1.8java.version>
properties>
<dependencies>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>1.3.1version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.github.pagehelpergroupId>
<artifactId>pagehelper-spring-boot-starterartifactId>
<version>RELEASEversion>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druidartifactId>
<version>1.1.5version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
<plugin>
<groupId>org.mybatis.generatorgroupId>
<artifactId>mybatis-generator-maven-pluginartifactId>
<version>1.3.5version>
<configuration>
<verbose>trueverbose>
<overwrite>trueoverwrite>
configuration>
plugin>
plugins>
build>
project>
<generatorConfiguration>
<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>
<sqlMapGenerator targetPackage="mybatis" targetProject=".\src\main\resources">
<property name="enableSubPackages" value="true"/>
sqlMapGenerator>
<javaClientGenerator type="XMLMAPPER" targetPackage="com.itcloud.mapper" targetProject=".\src\main\java">
<property name="enableSubPackages" value="true"/>
javaClientGenerator>
<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>
F:\spring-root\spring-boot-mybatis>
F:\spring-root\spring-boot-mybatis> mvn mybatis-generator:generate
反向工程生成的 Mapper.java无法被springBoot自动注入,需要在类上面添加@Mapper*注解,或者在程序的主类上面进行先关包的扫描
演示:下面二者用其一即可
1.@Mapper
@Mapper
public interface GoodsMapper {
//此处内容被省略
}
[email protected](“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);
}
}
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
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 = goodsMapper.selectAll();
PageInfo pageInfo = new PageInfo(goods);
System.out.println(pageInfo.getPages()); //结果: 11
}
}