步骤1.在pom.xml文件中添加MBG插件
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.4.0</version>
<configuration>
<!-- 这里要修改相应的路径-->
<configurationFile>${basedir}/src/main/resources/mybatis/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysqlversion}</version>
</dependency>
</dependencies>
</plugin>
步骤2:编写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">
<!-- 要是爆红,就是idea没下载dtd,只需下载即可-->
<generatorConfiguration>
<context id="MYSQLTables" targetRuntime="MyBatis3">
<!-- 关闭注释的生成-->
<commentGenerator>
<property name="suppressAllComments" value="true"></property>
</commentGenerator>
<!-- 虽然插件中配置了<overwrite>true</overwrite>,但是只对java文件有效,对mapper文件无效,使用UnnergeableXmlMappersPlugin才可以覆盖xml文件-->
<!-- <plugin type="org.mybatis.generator.plugins.UnnergeableXmlMappersPlugin"></plugin>-->
<!-- 设置数据库连接,需要修改-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://127.0.0.1:3306/mybatisdemo"
userId="root"
password="123456">
</jdbcConnection>
<javaTypeResolver >
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!-- 实体类-->
<javaModelGenerator targetPackage="com.sqm.domain" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
<property name="trimStrings" value="true" />
</javaModelGenerator>
<!-- mapper映射文件-->
<sqlMapGenerator targetPackage="mybatis.mapper" targetProject="src/main/resources">
<property name="enableSubPackages" value="true" />
</sqlMapGenerator>
<!-- mapper到接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.sqm.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="true" />
</javaClientGenerator>
<!-- 选择数据表(data),设置实体类名为knowledge-->
<table tableName="data" domainObjectName="knowledge"></table>
<!-- <table schema="DB2ADMIN" tableName="ALLTYPES" domainObjectName="Customer" >-->
<!-- <property name="useActualColumnNames" value="true"/>-->
<!-- <generatedKey column="ID" sqlStatement="DB2" identity="true" />-->
<!-- <columnOverride column="DATE_FIELD" property="startDate" />-->
<!-- <ignoreColumn column="FRED" />-->
<!-- <columnOverride column="LONG_VARCHAR_FIELD" jdbcType="VARCHAR" />-->
<!-- </table>-->
</context>
</generatorConfiguration>
步骤3:在maven中运行相应的插件
点击mybatis-generator:generate 右键运行Maven构建
最后结果
自动生成成功
如何利用生成的代码?
MGB会帮我们自动生成许多方法
package com.sqm.mapper;
import com.sqm.domain.knowledge;
import com.sqm.domain.knowledgeExample;
import java.util.List;
import org.apache.ibatis.annotations.Param;
public interface knowledgeMapper {
//求和
long countByExample(knowledgeExample example);
//删除
int deleteByExample(knowledgeExample example);
//通过主键删除
int deleteByPrimaryKey(String name);
//普通插入 一般都用这个!!,区别就是,要是插入值中有些是空,普通插入就是空,而智能插入则不插入
// insert into data(name, symbolsize, category, des) values(1,2,null, null) 普通插入
// insert into data(name, symbolsize) values(1,2) 智能插入
int insert(knowledge record);
//智能插入
int insertSelective(knowledge record);
//传入空对象或者null则返回全部 并且可以设置条件查询
List<knowledge> selectByExample(knowledgeExample example);
//通过主键查询
knowledge selectByPrimaryKey(String name);
//选择性更新,用这个
// 第一个参数,改变后的值 第二个参数查找的条件
int updateByExampleSelective(@Param("record") knowledge record, @Param("example") knowledgeExample example);
int updateByExample(@Param("record") knowledge record, @Param("example") knowledgeExample example);
//更新一定要用这个
int updateByPrimaryKeySelective(knowledge record);
//不要用这个
int updateByPrimaryKey(knowledge record);
}
如何利用如下所示
我们新建一个test类来进行演示
import com.sqm.domain.knowledge;
import com.sqm.domain.knowledgeExample;
import com.sqm.mapper.UserMapper;
import com.sqm.mapper.knowledgeMapper;
import org.apache.ibatis.annotations.Param;
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;
import java.lang.reflect.Array;
import java.sql.SQLException;
import java.util.*;
public class KnowledgeDaoTest {
knowledgeMapper mapper;
SqlSession sqlSession;
@Before
public void before(){
try {
InputStream inputStream = Resources.getResourceAsStream("mybatis/mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
sqlSession = sqlSessionFactory.openSession();
sqlSession.getConnection().setAutoCommit(true);
mapper = sqlSession.getMapper(knowledgeMapper.class);
sqlSession.getConnection().setAutoCommit(true);
inputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
@After
public void after(){
sqlSession.close();
}
@Test
public void countByExample(){
knowledgeExample knowledgeExample = new knowledgeExample();
knowledgeExample.createCriteria().andCategoryEqualTo("维吾尔族");
long total = mapper.countByExample(knowledgeExample);//输出所有数据条数
System.out.println(total);
}
@Test
public void deleteByExample(){
knowledgeExample knowledgeExample = new knowledgeExample();
knowledgeExample.createCriteria().andCategoryIn(Arrays.asList(new String[]{"维吾尔族"}));
mapper.deleteByExample(knowledgeExample);
}
@Test
public void deleteByPrimaryKey(){
mapper.deleteByPrimaryKey("发展历史");
}
@Test//普通插入
public void insert(){
knowledge knowledge = new knowledge();
knowledge.setName("史启明");
knowledge.setCategory("汉族");
mapper.insert(knowledge);
}
@Test
public void insertSelective(){
knowledge knowledge = new knowledge();
knowledge.setName("史启2");
knowledge.setCategory("汉族");
mapper.insertSelective(knowledge);
}
@Test//传入空对象或者null则返回全部 并且可以设置条件查询
public void selectByExample(){
knowledgeExample example = new knowledgeExample();
// knowledgeExample.Criteria criteria = example.createCriteria();
// criteria.andCategoryEqualTo("语言");//设置条件查找
example.createCriteria().andCategoryIn(Arrays.asList(new String[]{"语言", "维吾尔族"})).andSymbolsizeLessThan(70);//where in 还可以串联
// example.createCriteria().andCategoryEqualTo("科学");//要是连着写两行,会自动用or串接起来,要是写成1行则会用and串接
List<knowledge> knowledgeList = mapper.selectByExample(example);
for (knowledge knowledge : knowledgeList) {
System.out.println(knowledge.toString());
}
}
@Test//通过主键查询
public void selectByPrimaryKey(){
knowledge kno = mapper.selectByPrimaryKey("中心");
System.out.println(kno.toString());
}
@Test//选择性更新,用这个
public void updateByExampleSelective(){
//更改
knowledge knowledge = new knowledge();
knowledge.setDes("我最爱的吃的");
//查找的条件
knowledgeExample knowledgeExample = new knowledgeExample();
knowledgeExample.createCriteria().andCategoryEqualTo("饮食");
mapper.updateByExampleSelective(knowledge, knowledgeExample);
}
@Test//这个别用
public void updateByExample(){}
@Test//更新操作一定要用这个
public void updateByPrimaryKeySelective(){
knowledge knowledge = new knowledge();
knowledge.setName("《乌古斯可汗传》");
knowledge.setSymbolsize(200);
mapper.updateByPrimaryKeySelective(knowledge);
}
@Test//更新操作不要用这个
public void updateByPrimaryKey(){
knowledge knowledge = new knowledge();
knowledge.setName("《乌古斯可汗传》");
knowledge.setSymbolsize(200);
mapper.updateByPrimaryKey(knowledge);
}
}
版权声明:本文为falldeep原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。