mybatis逆向工程和批量插入

逆向工程

导包

<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 generatorConfiguration
  PUBLIC "-//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 block
			e.printStackTrace();
		} catch (XMLParserException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvalidConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.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 {

	@Autowired
	SqlSession sqlSession;
//	@Autowired
//	ClassMapper classMapper;
	@Autowired
	StudentMapper studentMapper;
	@Test
	public 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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
		http://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>
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值