逆向工程

导包

<dependency> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-core</artifactId> <version>1.3.7</version> </dependency>

进入http://www.mybatis.org/generator/configreference/xmlconfig.html

复制这一大块代码,在项目上右击新建一个xml文件,粘贴进去
修改如下

<?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="DB2Tables" targetRuntime="MyBatis3"><!-- 不添加注释--><commentGenerator><property name="suppressAllComments" value="true" />
</commentGenerator><!-- 配置数据库连接信息 --><jdbcConnection driverClass="com.mysql.jdbc.Driver"connectionURL="jdbc:mysql://localhost:3306/dao"userId="root"password="root"></jdbcConnection><javaTypeResolver ><property name="forceBigDecimals" value="false" /></javaTypeResolver><!-- 指定javabean生成的位置 --><javaModelGenerator targetPackage="cn.sjxy.aop.domain" targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /><property name="trimStrings" value="true" /></javaModelGenerator><!-- 指定sql映射文件生成位置(mapper.xml) --><sqlMapGenerator targetPackage="mapper"  targetProject=".\src\main\resources"><property name="enableSubPackages" value="true" /></sqlMapGenerator><!-- 指定dao接口生成位置 --><javaClientGenerator type="XMLMAPPER" targetPackage="cn.sjxy.aop.dao"  targetProject=".\src\main\java"><property name="enableSubPackages" value="true" /></javaClientGenerator><!-- 指定每个表生成策略 --><table tableName="student" domainObjectName="Student"></table><table tableName="class" domainObjectName="Class"></table></context>
</generatorConfiguration>

下面进行生成操作

复制下面这串代码

新建一个测试类,写个main方法粘贴进去

package cn.sjxy.aop.test;import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;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;public class GenTest {public static void main(String[] args) {List<String> warnings = new ArrayList<String>();boolean overwrite = true;File configFile = new File("generator.xml");ConfigurationParser cp = new ConfigurationParser(warnings);try {Configuration config = cp.parseConfiguration(configFile);DefaultShellCallback callback = new DefaultShellCallback(overwrite);MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config, callback, warnings);myBatisGenerator.generate(null);} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (XMLParserException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InvalidConfigurationException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

导包,加try catch,右击运行java application,完成
查看实体包,接口包和mapper文件夹

批量插入

导包

<dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>5.1.7.RELEASE</version></dependency>

新建测试类

package cn.sjxy.aop.test;import java.util.Random;
import java.util.UUID;import org.apache.ibatis.session.SqlSession;
import org.junit.Test;import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import cn.sjxy.aop.dao.ClassMapper;
import cn.sjxy.aop.dao.StudentMapper;
import cn.sjxy.aop.domain.Class;
import cn.sjxy.aop.domain.Student;@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"classpath:spring.xml"})
public class DaoTest {@AutowiredSqlSession sqlSession;
//  @Autowired
//  ClassMapper classMapper;@AutowiredStudentMapper studentMapper;@Testpublic void test1() {//ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
//      ClassMapper classMapper = context.getBean(ClassMapper.class);
//      //System.out.print(studentMapper);
//      String[] names = context.getBeanDefinitionNames();
//      for(String name : names) {
//          System.out.println(name);
//      }//     classMapper.insertSelective(new Class(null, "实验班"));
//      classMapper.insertSelective(new Class(null, "普通版"));
//      classMapper.insertSelective(new Class(null, "强化班"));
//      classMapper.insertSelective(new Class(null, "火箭班"));StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Random rand = new Random();  for(int i=0;i<200;i++) {int r = rand.nextInt(100)+1;int cid = rand.nextInt(4)+1;String name = UUID.randomUUID().toString().substring(0,5);mapper.insertSelective(new Student(null, name, r, cid));}System.out.println("批量插入完成");//studentMapper.insertSelective(new Student(null, "xiaobai", r, cid));}
}

配置文件spring.xml 中
添加

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><context:component-scan base-package="cn.sjxy.aop"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /></context:component-scan><context:property-placeholder location="classpath:jdbc.properties" />数据源<bean id="c3p0" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driver}" /><property name="jdbcUrl" value="${jdbc.url}" /><property name="user" value="${jdbc.user}" /><property name="password" value="${jdbc.password}" /></bean>整合mybatis<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"><property name="configLocation" value="classpath:mybatis.xml" /><property name="dataSource" ref="c3p0" /><property name="mapperLocations" value="classpath:mapper/*.xml" /></bean>用于批量插入<bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate"><constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /><constructor-arg name="executorType" value="BATCH" /></bean><bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"><property name="basePackage" value="cn.sjxy.aop.dao"/><property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/><property name="sqlSessionTemplateBeanName" value="sqlSession"></property></bean><bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"><property name="dataSource" ref="c3p0"></property></bean><tx:annotation-driven transaction-manager="tx"/></beans>

mybatis逆向工程和批量插入相关推荐

  1. Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案

    Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 参考文章: (1)Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案 (2)https://www.cnblogs. ...

  2. Java mybatis实现mysql批量插入

    记录下来方便自己,同时也希望能对比较迷惑的小盆友有所帮助 1.把批量插入的数据生成一个List集合 2.用java控制一次插入的条数和集合 // 每次插入10条int len = count, inc ...

  3. 16、mybatis动态sql 批量插入

    文章目录 1.EmployeeMapper 2.EmployeeMapper.xml(以逗号间隔执行一条语句)(推荐) 3.Test 4.以分号间隔执行每条语句(第二种方式) 5.Oracle下的批量 ...

  4. MyBatis foreach语句批量插入数据

    本例技术:Spring+SpringMVC+MyBatis+Oracle 问题描述:需要将程序里的一个集合保存到数据库里,集合的类型对应数据库的一个实体,若在程序里遍历集合再一条条保存到数据库表中有点 ...

  5. Mybatis Plus 实现批量插入

    Mybatis Plus 的 IService 接口中提供了批量插入的方法,然而,它的内部实现逻辑竟然是这样的: 居然是循环单条插入?!逗人玩嘛,好吧,自己动手,丰衣足食. 一. 添加依赖 <! ...

  6. 转:Mybatis与JDBC批量插入数据库哪个更快

    转自 http://www.cnblogs.com/fnz0/p/5713102.html, https://www.cnblogs.com/wxw7blog/p/8706797.html [转]: ...

  7. Mybatis 批量操作(批量插入、批量更新、批量删除)总结

    文章目录 一.批量插入 二.批量更新 三.批量删除 一.批量插入 <insert id="insertBatch" parameterType="java.util ...

  8. mybatis以及mybatisplus批量插入问题

    1. 思路分析: 批量插入是我们日常开放经常会使用到的场景,一般情况下我们也会有两种方案进行实施,如下所示. 方案一 就是用 for 循环循环插入: 优点:JDBC 中的 PreparedStatem ...

  9. MyBatis 使用 foreach 批量插入

    yml文件 spring:datasource:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/ ...

最新文章

  1. __bridge 使用注意
  2. ES6 Map数据结构
  3. 计算机行业从业者的核心竞争力,计算机行业:创新企业上市新规发布,重视具备核心竞争力的真成长.pdf...
  4. 2018第九届蓝桥杯C/C++ B国赛 —— 第四题:调手表
  5. ArcGIS.Server.9.2.DotNet在ElementGraphicsLayer画点、线、折线、面、圆、矩形的代码
  6. 服务器配置织梦系统,DedeCMS织梦系统设置说明:核心设置
  7. Python Mysql 数据库操作
  8. Java并发编程的艺术 记录(三)
  9. 《游戏学习》java实现连珠五子棋完整代码
  10. win10家庭版开启远程桌面(带rdpwrap.ini)
  11. 计算机测试穿越,计算机的迷雾,如何穿越?
  12. 集合查询和查询结果处理
  13. vue 高德地图 不同区域显示不同颜色_高德地图这样用成为你的图表神器
  14. 【阅读摘要】第6章 电子元器件与组件的热设计
  15. 主磁盘分区和逻辑磁盘分区的区别是什么?
  16. 生成固定的句子,句子中含有给定的多个关键词,python代码实现
  17. 一篇文章带你快速了解荧光蛋白
  18. JTA Error creating bean with name 'transactionManager' defin
  19. jsp_asp_php,PHP/JSP/ASP
  20. python getter setter_python的getter和setter方法使用详解

热门文章

  1. PayPal用户如何避免账户被冻结
  2. 微信小程序storage操作报错,判断是否能取到值
  3. 微信小程序js无符号整型转换有符号整型
  4. 软考-错题积累(数据库)
  5. m基于Matlab的fir和iir数字滤波器的设计与仿真
  6. JavaScript卡布列克常数
  7. WPS Office AI实战:智能表格化身智能助理
  8. (2012年旧文)纪念史蒂夫乔布斯---IT界的普罗米修斯
  9. 庄小威超分辨storm_哈佛大学庄小威加盟北大生物动态光学成像中心
  10. STM32中的通信协议