MyBatis-generator+maven完成mybatis例子
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis-generator+maven完成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>
<context id="mysqlgenerator" targetRuntime="MyBatis3">
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/ygy"
userId="root"
password="***" />
<!--targetProject 后面跟的是model放的位置 targetPackage放的包名-->
<javaModelGenerator targetPackage="com.nomico271.model" targetProject="src" />
<sqlMapGenerator targetPackage="com.nomico271.mapper" targetProject="src" />
<javaClientGenerator type="XMLMAPPER" targetPackage="com.nomico271.mapper" targetProject="src" />
<table tableName="ygy1"/>
</context>
</generatorConfiguration>
二.pox.xml文件
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>generator</groupId>
<artifactId>generator</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>generator Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.3.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
//××引包
</dependencies>
<build>
<finalName>generator</finalName>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.34</version>
</dependency>
</dependencies>
<configuration>
<overwrite>true</overwrite>
</configuration>
</plugin>
</plugins>
</build>
</project>
完成上面的配置 然后打开maven-->plugins--->mybatis-generator--->mybatis-generator:generate
运行mybatis-generator:generate完成自动生成所需要的文件
三.添加数据到数据库中
import com.nomico271.model.Ygy1;
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 java.io.IOException;
import java.io.Reader;
public class main {
static Reader reader;
static SqlSession session;
public static void main(String[] args) {
try {
reader= Resources.getResourceAsReader("ygy.xml");
} catch (IOException e) {
e.printStackTrace();
}
finally {
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);
session=sqlSessionFactory.openSession();
Ygy1 ygy1=new Ygy1();
ygy1.setId(3);
ygy1.setName("sdhuaik");
session.insert("insert",ygy1);
session.commit();
session.close();
}
}
}
运行程序显示 说明添加成功(其他删,改,查相似)
以上是关于MyBatis-generator+maven完成mybatis例子的主要内容,如果未能解决你的问题,请参考以下文章
maven插件mybatis-generator自动生成代码
IDEA——mybatis-generator插件自动生成实体代码(Maven)
转载maven插件mybatis-generator自动生成