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(); } } }
来源:oschina
链接:https://my.oschina.net/u/3352298/blog/2962021