一、准备相关依赖
在pom文件中添加generator依赖
<dependency>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-core</artifactId>
<version>1.4.1</version>
</dependency>
二、配置xml
在资源目录创建
<!DOCTYPE generatorConfiguration PUBLIC
"-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="simple" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.cj.jdbc.Driver"
connectionURL="jdbc:mysql:///mydb?serverTimezone=GMT%2B8"
userId="root"
password="root"/>
<!--实体类-->
<javaModelGenerator targetPackage="com.godgy.pojo" targetProject="src/main/java"/>
<!--mapper映射文件-->
<sqlMapGenerator targetPackage="com.godgy.mapper" targetProject="src/main/resources"/>
<!--mapper接口-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.godgy.mapper" targetProject="src/main/java"/>
<!--表名-->
<table tableName="book" />
</context>
</generatorConfiguration>
三、创建generator类,并运行
package com.godgy.utils;
/**
* @author : Mr.Godgy
* @version : 1.0
* @date : 2022/8/13
* @description:
*/
import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.exception.InvalidConfigurationException;
import org.mybatis.generator.exception.XMLParserException;
import org.mybatis.generator.internal.DefaultShellCallback;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class GeneratorMain {
public static void main(String[] args) throws XMLParserException, IOException, InvalidConfigurationException, SQLException, InterruptedException {
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//配置文件的位置
File configFile = new File("E:\\JavaProject\\myssmdemo\\src\\main\\resources\\generator.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);
myBatisGenerator.generate(null);
}
}
这里直接右键运行main方法,即可根据数据库相关表,生成对应的pojo和mapper以及映射文件。
四、测试功能
package com.godgy.test;
import com.godgy.mapper.BookMapper;
import com.godgy.pojo.Book;
import com.godgy.pojo.BookExample;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
/**
* @author : Mr.Godgy
* @version : 1.0
* @date : 2022/8/13
* @description:
*/
public class MybatisTest {
@Test
public void testFindAll() throws IOException {
//配置资源位置
InputStream resourceAsStream = Resources.getResourceAsStream("MybatisConfig.xml");
//创建会话工厂
SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(resourceAsStream);
//创建一个会话
SqlSession sqlSession = ssf.openSession(true);
//通过反射获取对象方法
BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
//执行查询方法
BookExample bookExample = new BookExample();
List<Book> books = bookMapper.selectByExample(bookExample);
System.out.println(books);
//关闭会话(不要忘了)
sqlSession.close();
}
}
感谢您的来访,获取更多精彩文章请收藏本站。
© 版权声明
THE END
暂无评论内容