java ibatis持久化框架配置

1. application*-db.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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<!-- 数据源配置 -->
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/empires" />
		<property name="username" value="root" />
		<property name="password" value="" />
	</bean>

	<!-- sqlMapClient 配置 -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="configLocation">
			<value>classpath:ibatis/sqlmap-config.xml</value>
		</property>
	</bean>

	<bean id="sqlMapClientTemplate" class="org.springframework.orm.ibatis.SqlMapClientTemplate">
		<property name="sqlMapClient">
			<ref bean="sqlMapClient" />
		</property>
	</bean>

	<!-- spring 事务配置 -->
	<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
	</bean>

	<bean id="transactionTemplate"
class="org.springframework.transaction.support.TransactionTemplate">
		<property name="transactionManager">
			<ref bean="transactionManager" />
		</property>
	</bean>

	<!-- 开启事务,事务管理器不指定使用默认的transactionManager -->
	<tx:annotation-driven />
</beans>


2. sqlmap-config.xml
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" 
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
	<settings useStatementNamespaces="true" enhancementEnabled="true"
		lazyLoadingEnabled="true" />
	<sqlMap resource="ibatis/userInfo-sqlmap-mapping.xml" />
</sqlMapConfig>


3. userInfo-sqlmap-mapping.xml
<sqlMap>
    <resultMap id="RM.UserInfo" class="com.empires.module.UserInfo">
     <result property="id" column="id" javaType="java.lang.String" jdbcType="VARCHAR"  />
     <result property="userName" column="user_name" javaType="java.lang.String" jdbcType="VARCHAR"  />
     <result property="userId" column="user_id" javaType="java.lang.String" jdbcType="VARCHAR"  />		
    </resultMap>
    
    <sql id="UserInfo.columns">
	id, user_name, user_id, login_name, password, phone, mobile, address
    </sql>
		
    <insert id="moaprod.UserInfo.insert">
	INSERT INTO user_info (
	  id ,user_name ,user_id ,login_name ,password ,phone ,mobile ,address 
	) VALUES (#id#,#userName#,#userId#,#loginName#,#password#,#phone#,#mobile#,#address#)
	<!--<selectKey resultClass="java.lang.String" type="post" keyProperty="id" >
            select last_insert_id()
    	</selectKey>        
	--></insert>
...

你可能感兴趣的:(ibatis 持久化框架)