欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring2.0+Ibatis的组合 iBATISMySQLSQLApacheJDBC 

程序员文章站 2022-06-09 08:08:30
...
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

<!-- 数据库配置 -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost/drp</value>
</property>
<property name="username">
<value>root</value>
</property>
<property name="password">
<value>123</value>
</property>
</bean>

<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation"
value="classpath:SqlMapConfig.xml">
</property>
</bean>

    <bean  id="BaseDao" class="com.lf.drp.dao.imp.BaseDaoImp">
    <property name="dataSource" ref="dataSource"></property>
<property name="sqlMapClient" ref="sqlMapClient"></property>
    </bean>
    <!-- 用户DAO -->
    <bean id="UserDao" class="com.lf.drp.dao.imp.UserDaoImp" parent="BaseDao">
    </bean>
</beans>
<!--Ibatis配置 -->
<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE sqlMapConfig     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-config-2.dtd">

<sqlMapConfig>
  <sqlMap  resource="com/lf/drp/sqlmapping/xml/T_User.xml"/>
  <sqlMap  resource="com/lf/drp/sqlmapping/xml/T_Area.xml"/>
</sqlMapConfig>
<!--T_User的配置 -->
<!DOCTYPE sqlMap     
    PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"     
    "http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap>

<!--对象类型 参数设定 方便下面 增删改查 调用 -->
<typeAlias alias="userInfo" type="com.lf.drp.entity.T_User" />  
 
<!--查询全部 -->
<select id="selectAllUser" resultClass="userInfo">
select * from T_User
</select>

<!--查询获得用户Id -->
<select id="getUserById" parameterClass="int"
resultClass="userInfo">
select * from T_User where user_no=#id#
</select>
<!-- 通过筛选获得用户 -->
<select id="getUsers" parameterClass="java.util.HashMap"
resultClass="userInfo">
select * from T_User
<!-- 动态拼接 -->
<dynamic prepend="where">
            <!-- 筛选条件1-->
<isPropertyAvailable property="loginName">
<isNotNull property="loginName">
<isNotEmpty prepend="and" property="loginName">
loginName like '%$loginName$%'
</isNotEmpty>
</isNotNull>
</isPropertyAvailable>
            <!-- 筛选条件2 -->
            <isNotNull property="contect_Tel">
            <isNotEmpty prepend="and " property="contect_Tel">
            contect_Tel=#contect_Tel#
            </isNotEmpty>
            </isNotNull>
            <!-- 筛选条件3-->
            <isNotNull property="start_Date">
                  <isNotNull property="end_Date">
                      <isNotEmpty property="start_Date">
                         <isNotEmpty prepend="and" property="end_Date">
                         create_Date between #start_Date# and  #end_Date#
                         </isNotEmpty>
                      </isNotEmpty>
                  </isNotNull>
            </isNotNull>


</dynamic>

</select>


<!--用户登录-->
<select id="login" parameterClass="String" resultClass="userInfo">
select * from T_User where loginName=#loginName#
</select>
<!-- 用户修改密码 -->
<update id="modifyUserPwd" parameterClass="userInfo">
update T_User set passWord=#passWord# where user_no=#user_no#
</update>
<!--用户修改-->
<update id="modifyUser" parameterClass="userInfo">
update T_User set loginName = #loginName# ,contect_Tel =
#contect_Tel# ,email =#email# WHERE user_no=#user_no#
</update>

</sqlMap>