内容简介: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导入,然后复制粘贴逆向工程的代码以及配置文件
- 配置文件:
<?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(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
猜你喜欢:- Ghidra 9.1 发布,软件逆向工程框架
- radare2 5.3.1 发布,逆向工程框架
- NSA 的软件逆向工程框架 Ghidra 曝出漏洞
- 美国国家安全局释出其软件逆向工程框架 Ghidra
- Cutter 2.0.1 发布,radare2 逆向工程框架的 GUI
- Cutter 1.8.3 发布, radare2 逆向工程框架的 GUI
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
计算机程序设计艺术(第3卷)-排序和查找(英文影印版)
(美)Donald E.Knuth / 清华大学出版社 / 2002-9 / 85.00元
《计算机程序设计艺术排序和查找(第3卷)(第2版)》内容简介:这是对第3卷的头一次修订,不仅是对经典计算机排序和查找技术的最全面介绍,而且还对第1卷中的数据结构处理技术作了进一步的扩充,通盘考虑了将大小型数据库和内外存储器。它遴选了一些经过反复检验的计算机方法,并对其效率做了定量分析。第3卷的突出特点是对“最优排序”一节作了修订,对排列论原理与通用散列法作了全新讨论。一起来看看 《计算机程序设计艺术(第3卷)-排序和查找(英文影印版)》 这本书的介绍吧!