Mybatis应用学习(6)——Spring框架整合与逆向工程 原

栏目: Java · 发布时间: 5年前

内容简介:1. 整合思路:2. 准备环境:1. mybaits需要程序员自己编写sql语句,mybatis官方提供逆向工程可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)

1. 与Spring框架整合

1. 整合思路:

  • 需要spring通过单例方式管理SqlSessionFactory。
  • spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(spring和mybatis整合自动完成)
  • 持久层的mapper都需要由spring进行管理。

2. 准备环境:

  • 主要需要的jar包为:Spring相关jar包、Mybatis相关jar包、mybatis和spring的整合包(mybatis-spring)
  • Spring的配置文件中的相关配置:
<!--配置数据源-->
	<context:property-placeholder location="classpath:conf/db.properties"/>
	<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${jdbc.driver}"></property>
		<property name="url" value="${jdbc.url}"></property>
		<property name="password" value="${jdbc.password}"></property>
		<property name="username" value="${jdbc.name}"></property>
	</bean>
<!--配置sqlSessionFactory-->
	<bean id="ssf" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 配置mybatis的数据源 -->
		<property name="dataSource" ref="datasource"></property>

		<!-- 加载mybatis的配置文件 -->
		<property name="configLocation" value="mybatis/SqlMapConfig.xml" />

		<!-- mapperLocations表示指定Mapper映射文件所在的类路径,用*通配符扫描该路径下的所有xml文件
		如果SqlMapConfig.xml中已经指定,则可以不用写
		<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
		 -->
	</bean>

<!-- 通过MapperScannerConfigurer进行Mapper接口批量扫描,并生成Mapper接口的代理实现类,生成的代理实现类的id是Mapper接口类名的首字母小写
	basePackage指定Mapper接口所在的包
	sqlSessionFactoryBeanName指定配置完成的sqlSessionFactoryBean的id
	 -->
	<bean id="mfc" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="basePackage" value="cn.mapper"></property>
		<property name="sqlSessionFactoryBeanName" value="ssf"></property>
	</bean>

2.  Mybatis逆向工程

1. mybaits需要 程序员 自己编写 sql 语句,mybatis官方提供逆向工程可以针对单表自动生成mybatis执行所需要的代码(mapper.java,mapper.xml、po..)

2. 使用方式:

  • 首先需要导入包,可以通过Maven导入,然后复制粘贴逆向工程的代码以及配置文件                                                                     Mybatis应用学习(6)——Spring框架整合与逆向工程 原 Mybatis应用学习(6)——Spring框架整合与逆向工程 原
  • 配置文件:
<?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="testTables" targetRuntime="MyBatis3">
		<commentGenerator>
			<!-- 是否去除自动生成的注释 true:是 : false:否 -->
			<property name="suppressAllComments" value="true" />
		</commentGenerator>
		<!--数据库连接的信息:驱动类、连接地址、用户名、密码 -->
		<jdbcConnection driverClass="com.mysql.jdbc.Driver"
			connectionURL="jdbc:mysql://localhost:3306/customer?useUnicode=true&characterEncoding=utf8" userId="root"
			password="123456">
		</jdbcConnection>
		<!-- <jdbcConnection driverClass="oracle.jdbc.OracleDriver"
			connectionURL="jdbc:oracle:thin:@127.0.0.1:1521:yycg" 
			userId="yycg"
			password="yycg">
		</jdbcConnection> -->

		<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和 
			NUMERIC 类型解析为java.math.BigDecimal -->
		<javaTypeResolver>
			<property name="forceBigDecimals" value="false" />
		</javaTypeResolver>


		<!-- targetProject:生成PO类的位置,主要修改 -->
		<javaModelGenerator targetPackage="com.customer.pojo"
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
			<!-- 从数据库返回的值被清理前后的空格 -->
			<property name="trimStrings" value="true" />
		</javaModelGenerator>
		
		
        <!-- targetProject:mapper映射文件生成的位置 -->
		<sqlMapGenerator targetPackage="com.customer.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</sqlMapGenerator>
		
		
		<!-- targetPackage:mapper接口生成的位置 -->
		<javaClientGenerator type="XMLMAPPER"
			targetPackage="com.customer.mapper" 
			targetProject=".\src">
			<!-- enableSubPackages:是否让schema作为包的后缀 -->
			<property name="enableSubPackages" value="false" />
		</javaClientGenerator>
		
		
		<!-- 指定数据库表-->
		<table tableName="tb_content"></table>
		<table tableName="tb_content_category"></table>
		<table tableName="tb_item"></table>
		<table tableName="tb_item_cat"></table>
		<table tableName="tb_item_desc"></table>
		<table tableName="tb_item_param"></table>
		<table tableName="tb_item_param_item"></table>
		<table tableName="tb_order"></table>
		<table tableName="tb_order_item"></table>
		<table tableName="tb_order_shipping"></table>
		<table tableName="tb_user"></table> 
		
		<!-- 有些表的字段需要指定 java 类型
		 <table schema="" tableName="">
			<columnOverride column="" javaType="" />
		</table> -->
	</context>
</generatorConfiguration>
  • 逆向工程java代码:
    import java.io.File;
    import java.io.IOException;
    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.XMLParserException;
    import org.mybatis.generator.internal.DefaultShellCallback;
    
    public class GeneratorSqlmap {
    
    	public void generator() throws Exception{
    
    		List<String> warnings = new ArrayList<String>();
    		boolean overwrite = true;
    		//指定 逆向工程配置文件
    		File configFile = new File("D:/java project/MybatisOthers/src/main/java/Generater/generatorConfig.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);
    
    	} 
    	public static void main(String[] args) throws Exception {
    		File file=new File(".");
    		System.out.println(file.getAbsolutePath());
    		try {
    			GeneratorSqlmap generatorSqlmap = new GeneratorSqlmap();
    			generatorSqlmap.generator();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    	}
    
    }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

PHP和MySQL Web开发

PHP和MySQL Web开发

Luke Welling、Laura Thomson / 武欣、邵煜 / 机械工业出版社 / 2005-12 / 78.00元

本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。一起来看看 《PHP和MySQL Web开发》 这本书的介绍吧!

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具