详解MyBatis动态生成表插入数据

本文介绍了MyBatis相对于Hibernate的优势,并通过实例展示了如何使用MyBatis进行动态SQL来实现表数据的插入。文章详细讲解了Spring集成MyBatis的配置过程,包括ApplicationContext.xml的配置、映射文件的设置,以及DAO层的userDao.xml和userDao.java的编写。
摘要由CSDN通过智能技术生成

MyBaits学习起来比hibernate容易上手的多,因为MyBatis 包含一个非常强大的查询缓存特性,它可以非常方便地配置和定制。但针对高级查询,Mybatis需要手动编写SQL语句,以及ResultMap,而Hibernate 提供的方法完成持久层操作。程序员甚至不需要对SQL 的熟练掌握, Hibernate/OJB 会根据制定的存储逻辑,自动生成对应的SQL 并调用JDBC 接口加以执行。但是MyBaits可以通过特殊的SQL语句编写来减少对SQL的依赖,在这里介绍简单的动态成表插入数据的例子。
首先用spring集成mybaits来配置好ApplicationContext.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:p="http://www.springframework.org/schema/p"  
    xmlns:aop="http://www.springframework.org/schema/aop"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
    http://www.springframework.org/schema/aop    
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">  
<!-- 配置数据源-->  
    <bean id="jdbcDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
    <property name="driverClassName">  
        <value>oracle.jdbc.driver.OracleDriver</value>  
    </property>  
    <property name="url">  
        <value>jdbc:oracle:thin:@172.18.1.252:1521:ORA9IS</value>  
       <!--springmybaitis是我的数据库  -->
    </property>  
    <property name="username">  
        <value>test</value>  
    </property>  
    <property name="password">  
        <value>test</value>  
    </property>  
</bean>  
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="jdbcDataSource" />  
    <property name="configLocation" value</
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值