Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件。
1.如何使用
第一步:在resources目录下创建mybatis文件夹,以及新建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">
<generatorConfiguration>
<!--jdbc的驱动jar-->
<classPathEntry
location="D:\workspace\dailywork\Liming\repository\mysql\mysql-connector-java\8.0.19\mysql-connector-java-8.0.19.jar"/>
<context id="my" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="false"/>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库的配置-->
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/wthink?serverTimezone=UTC"
userId="root"
password="root"/>
<!-- model 实体文件配置 -->
<javaModelGenerator targetPackage="com.wthink.day705.pojo"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- mapper.xml文件生成配置 -->
<sqlMapGenerator targetPackage="com.wthink.day705.dao"
targetProject="src/main/java">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- mapper接口生成配置 -->
<javaClientGenerator targetPackage="com.wthink.day705.dao"
targetProject="src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!--<table tableName="CTAS_FEE_BASE" domainObjectName="FeeBase"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>-->
<!--生成的表名-->
<table tableName="sb_class"></table>
</context>
</generatorConfiguration>
第二步:在你pom.xml中引入坐标跟插件
<!--Mybatis-genarator 反向生成工具-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.5</version>
</dependency>
<build>
<!--不让maven忽略的文件配置-->
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- generator插件 -->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<configurationFile>src/main/resources/mybatis/generatorConfig.xml</configurationFile>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
<executions>
<execution>
<id>Generate MyBatis Artifacts</id>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
第三步:执行生成工具的插件指令 双击
第四步:生成dao层,实体层,xmL文件
2.生成的dao层中的接口的含义
/*根据某个条件去查询数量*/
int countByExample(SbClassExample example);
/*根据某个条件去删除*/
int deleteByExample(SbClassExample example);
/*根据主键去删除*/
int deleteByPrimaryKey(Integer cid);
/*插入*/
int insert(SbClass record);
/*判断插入的字段中,是否有空值,如有空值,则不插入那个字段,默认使用 他数据库中的默认值*/
int insertSelective(SbClass record);
/*根据条件去查询返回列表*/
List<SbClass> selectByExample(SbClassExample example);
/*根据主键去查询,返回对象*/
SbClass selectByPrimaryKey(Integer cid);
/*根据条件去更新数据,如果数据中的字段有空值,则那个字段不更新*/
int updateByExampleSelective(@Param("record") SbClass record, @Param("example") SbClassExample example);
/*根据条件去更新数据*/
int updateByExample(@Param("record") SbClass record, @Param("example") SbClassExample example);
/*根据主键去更新数据,如果数据中的字段有空值,则那个字段不更新*/
int updateByPrimaryKeySelective(SbClass record);
/*根据主键去更新数据*/
int updateByPrimaryKey(SbClass record);
3.具体如何在项目中使用
写在test里面
package com.wthink.day705;
import com.wthink.day705.dao.SbClassMapper;
import com.wthink.day705.pojo.SbClass;
import com.wthink.day705.pojo.SbClassExample;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.List;
/*
* 单元测试 ,一般用于测试service服务层吗,跟dao层的接口是否有效
* */
@RunWith(SpringRunner.class)
@SpringBootTest
@EnableAutoConfiguration
public class DemoTest {
@Autowired
private SbClassMapper sbClassMapper;
/*
* 查询所有
* */
@Test
public void queryAll(){
List<SbClass> cla = sbClassMapper.selectByExample(new SbClassExample());
for (SbClass sbClass : cla) {
System.out.println(sbClass.getCname());
}
}
/*
* 按条件查询
* */
@Test
public void test2(){
//创建 sql语句的构造器
SbClassExample sbClassExample = new SbClassExample();
// select * from 表名 where id = ?
/* studentExample.createCriteria().andIdEqualTo(1);*/
//模糊查询王 的数据
//select * from 表名 where name like "%王%" and sex ='男' and age=21
/* studentExample.createCriteria().andNameLike("%"+"王"+"%")
.andSexEqualTo("男").andAgeEqualTo(21);*/
sbClassExample.setOrderByClause("cid desc");
//使用selectByExample 查询返回的都是一个集合
List<SbClass> cla = sbClassMapper.selectByExample(sbClassExample);
for (SbClass clas : cla) {
System.out.println(clas.getCname());
}
}
@Test
public void test3(){
/*根据主键去查询数据 返回时一个对象*/
SbClass cla = sbClassMapper.selectByPrimaryKey(1);
System.out.println(cla.getCid());
System.out.println(cla.getCname());
/*修改数据后,更新数据 根据主键去更新数据*/
cla.setCname("李明");
sbClassMapper.updateByPrimaryKey(cla);
}
@Test
public void test4(){
SbClass cla = new SbClass();
cla.setCname("小乔");
cla.setCprice(100);
cla.setCintroduce("好");
sbClassMapper.insertSelective(cla);
}
}