1. 介紹
Mybatis是目前非常流行的持久層框架笆焰,其逆向工程更是大大縮減了我們的開(kāi)發(fā)時(shí)間。
所謂mybatis逆向工程见坑,就是Mybatis會(huì)根據(jù)我們?cè)O(shè)計(jì)好的數(shù)據(jù)表嚷掠,自動(dòng)生成pojo(實(shí)體)、mapper(接口)以及mapper.xml(映射)
而且會(huì)在文件中給我們生成單表增刪改查的方法和sql
image-20201211164421070
2. 插件使用
2.1 引入依賴(lài) mybatis-generator
<dependencies>
<!--必要-->
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--非必要-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--mybatis-generator的Maven插件-->
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
2.2 加入配置文件
將資料包下的generatorConfig.xml 拷貝到 項(xiàng)目的resouces下
2.2.1 文件名稱(chēng):generratorConfig.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>
<!-- classPathEntry:數(shù)據(jù)庫(kù)的 JDBC驅(qū)動(dòng)的jar 包地址 -->
<classPathEntry location="lib/mysql-connector-java-5.1.47.jar"/>
<!--生成java代碼-->
<context id="context" targetRuntime="MyBatis3">
<!-- 配置生成pojo的序列化的插件-->
<plugin type="org.mybatis.generator.plugins.SerializablePlugin"/>
<!-- 配置生成toString的序列化的插件 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<commentGenerator>
<!-- 是否去除自動(dòng)生成的(english)注釋 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--數(shù)據(jù)庫(kù)連接的信息:驅(qū)動(dòng)類(lèi)荞驴、連接地址不皆、用戶(hù)名、密碼 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/spring_141"
userId="root"
password="root"/>
<!-- 指定生成的實(shí)體類(lèi)的存放位置 -->
<javaModelGenerator targetPackage="com.itheima.domain" targetProject="./src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 指定生成的Dao映射文件的存放位置 -->
<sqlMapGenerator targetPackage="com.itheima.dao" targetProject="./src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!--指定生成的Dao接口的存放位置-->
<javaClientGenerator targetPackage="com.itheima.dao" targetProject="./src/main/java" type="XMLMAPPER">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 指定數(shù)據(jù)庫(kù)表 -->
<table tableName="account" domainObjectName="Account" mapperName="AccountDao"
enableCountByExample="false" enableDeleteByExample="false"
enableSelectByExample="true" enableUpdateByExample="false"/>
</context>
</generatorConfiguration>
拷貝測(cè)試用配置文件
2.2.2 文件名稱(chēng):mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--數(shù)據(jù)庫(kù)環(huán)境信息-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql:///spring"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<!--mapper映射文件-->
<mappers>
<package name="com.itheima.dao"/>
</mappers>
</configuration>
image-20210609092556130
2.2.3 手動(dòng)加入mysql-connector的jar包
image-20210816161734200
2.3 運(yùn)行插件熊楼,生成文件
image-20201220115506038
3. 生成的文件的使用
3.1 基礎(chǔ)操作
public interface AccountDao {
//保存
int insert(Account record);
//動(dòng)態(tài)sql的保存
int insertSelective(Account record);
//主鍵修改
int updateByPrimaryKey(Account record);
//動(dòng)態(tài)sql的主鍵修改
int updateByPrimaryKeySelective(Account record);
//根據(jù)主鍵刪除
int deleteByPrimaryKey(Integer aid);
//主鍵查詢(xún)
Account selectByPrimaryKey(Integer aid);
//條件查詢(xún)
List<Account> selectByExample(AccountExample example);
}
3.2 條件查詢(xún)
模板代碼,直接復(fù)制使用即可
public class MyBatisTest {
private SqlSession sqlSession;
@Before
public void getSqlSession() {
InputStream stream = null;
try {
stream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
sqlSession = factory.openSession(true);
}
@After
public void closeSqlSession() {
sqlSession.close();
}
@Test
public void test1() {
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
AccountExample example = new AccountExample();
}
}
引入配置文件,修改數(shù)據(jù)庫(kù)配置信息
[圖片上傳失敗...(image-3d2362-1629158952976)]
測(cè)試代碼
package com.itheima.test;
import com.itheima.dao.AccountDao;
import com.itheima.domain.AccountExample;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
/*
AccountExample 這哥們有三個(gè)功能
1 條件查詢(xún)
example.createCriteria().條件
2 排序
example.setOrderByClause(排序條件)
3 去重
example.setDistinct(true);
*/
public class MybatisTest {
private SqlSession sqlSession;
@Before
public void getSqlSession() {
InputStream stream = null;
try {
stream = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException e) {
e.printStackTrace();
}
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
sqlSession = factory.openSession(true);
}
@After
public void closeSqlSession() {
sqlSession.close();
}
@Test
public void test1() {
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
AccountExample example = new AccountExample();
//這是example的一個(gè)小弟,作用是用于條件的拼接
AccountExample.Criteria criteria = example.createCriteria();
// criteria.andAidGreaterThan(2);//where aid > 2
// criteria.andBalanceBetween(10f,100f);//WHERE ( aid > 2 and balance between 1 and 100 )
// criteria.andNameLike("B%");//WHERE ( aid > ? and balance between ? and ? and name like B% )
criteria.andAidGreaterThan(2).andBalanceBetween(10f, 100f).andNameLike("B%");
accountDao.selectByExample(example);
}
@Test
public void test2() {
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
AccountExample example = new AccountExample();
//這是example的一個(gè)小弟,作用是用于排序
example.setOrderByClause("aid desc");//from account order by aid desc
accountDao.selectByExample(example);
}
@Test
public void test3() {
AccountDao accountDao = sqlSession.getMapper(AccountDao.class);
AccountExample example = new AccountExample();
//這是example的一個(gè)小弟,作用是用于去重
example.setDistinct(true);
accountDao.selectByExample(example);
}
}
3.3 模板說(shuō)明
* 條件說(shuō)明 * criteria.andXxxIsNull 添加字段xxx為null的條件 * criteria.andXxxIsNotNull 添加字段xxx不為null的條件 * criteria.andXxxEqualTo(value) 添加xxx字段等于value條件 * criteria.andXxxNotEqualTo(value) 添加xxx字段不等于value條件 * criteria.andXxxGreaterThan(value) 添加xxx字段大于value條件 * criteria.andXxxGreaterThanOrEqualTo(value) 添加xxx字段大于等于value條件 * criteria.andXxxLessThan(value) 添加xxx字段小于value條件 * criteria.andXxxLessThanOrEqualTo(value) 添加xxx字段小于等于value條件 * criteria.andXxxIn(List<霹娄?>) 添加xxx字段值在List<?>條件 * criteria.andXxxNotIn(List<孙蒙?>) 添加xxx字段值不在List<项棠?>條件 * criteria.andXxxLike(“%”+value+”%”) 添加xxx字段值為value的模糊查詢(xún)條件 * criteria.andXxxNotLike(“%”+value+”%”) 添加xxx字段值不為value的模糊查詢(xún)條件 * criteria.andXxxBetween(value1,value2) 添加xxx字段值在value1和value2之間條件 * criteria.andXxxNotBetween(value1,value2) 添加xxx字段值不在value1和value2之間條件