<dependency><groupId>org.mybatis.generator</groupId><artifactId>mybatis-generator-core</artifactId><version>1.3.7</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.47</version></dependency>
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE generatorConfigurationPUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN""http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd"><generatorConfiguration><!--<context id="MySQLTables" targetRuntime="MyBatis3">--><context id="MySQLTables" targetRuntime="MyBatis3Simple"><property name="javaFileEncoding" value="UTF-8"/><property name="beginningDelimiter" value="`"/><property name="endingDelimiter" value="`"/><!--支持分页--><plugin type="org.mybatis.generator.plugins.RowBoundsPlugin"/><!--生成的bean支持可序列化--><plugin type="org.mybatis.generator.plugins.SerializablePlugin"/><!--生成的bean有hashCode实现--><plugin type="org.mybatis.generator.plugins.EqualsHashCodePlugin"/><!--生成的bean有toString实现--><plugin type="org.mybatis.generator.plugins.ToStringPlugin"/><!-- 不生成注释 --><commentGenerator><property name="suppressAllComments" value="true"/></commentGenerator><!--配置数据库--><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&useSSL=false"userId="root"password="123456"></jdbcConnection><javaTypeResolver><property name="forceBigDecimals" value="false"/></javaTypeResolver><!--bean类 targetPackage指定生成文件的目录 targetProject从根目录开始指向targetPackage--><javaModelGenerator targetPackage="example.bean" targetProject="src/main/java"><property name="enableSubPackages" value="true"/><property name="trimStrings" value="true"/></javaModelGenerator><!--xml文件,注意XML是以追加的形式保存到文件中;如果要重新生成,先删除之前的--><sqlMapGenerator targetPackage="example.mapper" targetProject="src/main/resources"><property name="enableSubPackages" value="true"/></sqlMapGenerator><!-- mapper 接口--><javaClientGenerator type="XMLMAPPER" targetPackage="example.mapper" targetProject="src/main/java"><property name="enableSubPackages" value="true"/></javaClientGenerator><!--指定 table --><table tableName="t_user" domainObjectName="User" modelType="flat" delimitIdentifiers="true" delimitAllColumns="true"><generatedKey column="id" sqlStatement="MySql" identity="true"/></table></context>
</generatorConfiguration>
package example.tool;import org.mybatis.generator.api.MyBatisGenerator;
import org.mybatis.generator.config.Configuration;
import org.mybatis.generator.config.xml.ConfigurationParser;
import org.mybatis.generator.internal.DefaultShellCallback;import java.io.File;
import java.util.ArrayList;
import java.util.List;public class MybatisGeneratorTool {public static void main(String[] args) {try {List<String> warnings = new ArrayList<>();boolean overwrite = true;String path = MybatisGeneratorTool.class.getResource("/mybatis-generator.xml").getPath();System.out.println(path);File configFile = new File(path);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);} catch (Exception e) {e.printStackTrace();}}
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><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://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8&useSSL=false"/><property name="username" value="root"/><property name="password" value="123456"/></dataSource></environment></environments><mappers><mapper class="example.mapper.UserMapper"/></mappers>
</configuration>
<dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.6</version>
</dependency>
import example.bean.User;
import example.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class AppTest {public static void main(String[] args) throws Exception {SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsReader("mybatis-config.xml"));SqlSession sqlSession = factory.openSession();UserMapper mapper = sqlSession.getMapper(UserMapper.class);User user = mapper.selectByPrimaryKey(1);System.out.println(user);sqlSession.close();}
}
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态