webx学习笔记3--持久层配置

本文介绍如何使用ibatis持久层框架搭建Message_Board数据库,并配置所需的依赖包,包括ibatis-sqlmap、commons-dbcp及mysql-connector-java等。文中详细展示了如何在pom.xml中配置这些依赖,以及如何通过XML文件定义SQL映射,实现数据增删改查操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

创建数据库message_board


1. 数据库表结构的创建:





-----------------------------------------------------------------------------------------------------------------------------------------------------


因为要连接数据库,以及用到了ibatis作为持久层框架(ORM:对象关系映射)。

所以需要在pom.xml配置所需要的依赖包。

依赖的包可以在maven仓库里查找(http://search.maven.org/#search%7Cga%7C1%7C


需要找的驱动依赖包是:

查找ibatis,选择org.apache.servicemix.bundles.ibatis-sqlmap这个包。

查找dbcp,选择commons-dbcp。

查找mysql,选择mysql-connector-java。


这些包都是随便选的,只是为了实现项目的需要。没有特定的标准,如果有特殊需要可以选择别的包。

-----------------------------------------------------------------------------------------------------------------------------------------------------


找到需要的包版本号之后按照包的pom.xml文件,补充项目的pom.xml:
在properties标签中加入版本号:
 <ibatis-version>3.0-beta-10</ibatis-version>
        <dbcp-version>20030825.184428</dbcp-version>
        <mysql-version>5.1.33</mysql-version>

在dependencies中加入依赖的项目号和组号:
        <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-sqlmap</artifactId>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

在依赖管理( dependencyManagemen )中加入详细信息:
            <dependency>
            <groupId>org.apache.ibatis</groupId>
            <artifactId>ibatis-sqlmap</artifactId>
            <version>${ibatis-version}</version>
            </dependency>
            <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>${dbcp-version}</version>
            </dependency>
            <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql-version}</version>
            </dependency>

-----------------------------------------------------------------------------------------------------------------------------------------------------

然后配置持久层框架:



message-sqlmap.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="message" >
	<typeAlias alias="messageDO" type="com.alibaba.webx3.messageboard.dao.object.MessageDO" />
	
	<insert id="insertMessage" parameterClass="messageDO" >
	insert into message (
	            id,
	            title,
	            author,
	            content,
	            gmt_create,
	            gmt_modified
	         )  values (
	           #id#,
	           #title#,
	           #author#,
	           #content#,
	           now(),
	           now()
	         )
	  <selectKey resultClass="int" keyProperty="id">
     	<![CDATA[SELECT LAST_INSERT_ID() AS ID ]]>
      </selectKey>
	</insert>
	
	<select id="selectById" parameterClass="int" resultClass="messageDO" >
	select
	      id,
          title,
          author,
          content,
          gmt_create,
          gmt_modified
	from
	      message
	where
	      id=#id#
	</select>
	
	<select id="selectBylist" parameterClass="map" resultClass="messageDO" >
	select
	      id,
          title,
          author,
          content,
          gmt_create,
          gmt_modified
	from
	      message
	      
	order by gmt_modified desc
	
	limit #from#,#size#
	
	</select>
	
	<update id="updateMessage" parameterClass="messageDO">
	update 
	       message
	set
	       gmt_modified = now()
	   <dynamic prepend="" >
			<isNotEmpty prepend="," property="title">
				title = #title#
			</isNotEmpty>
			<isNotEmpty prepend="," property="author">
				author = #author#
			</isNotEmpty>
			<isNotEmpty prepend="," property="content">
				content = #content#
			</isNotEmpty>
		</dynamic>
    	where id = #id#    
	
	</update>
	
	<delete id="deleteMessage" parameterClass="int">
	delete 
	
	from 
	      message
	where 
	      id=#id#
	</delete>
	
</sqlMap>

dal-data-source.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:context="http://www.springframework.org/schema/context"
	xmlns:p="http://www.springframework.org/schema/p" 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/aop
            	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
				http://www.springframework.org/schema/tx 
				http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!-- 数据库配置 -->
<bean id= "dataSource" class="org.apache.commons.dbcp.BasicDataSource">
	<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
	<property name="url" value="jdbc:mysql://127.0.0.1:3306/message_board"></property>
	<property name="username" value="webx3"></property>
	<property name="password" value="webx3"></property>
	<property name="minIdle" value="0" ></property>
	<property name="maxWait" value="-1"></property>
</bean>

<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
</bean>

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

<!-- iBatis SQL map定义 -->
	<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<property name="configLocation" value="classpath:sqlmap-config.xml" />
	</bean>
</beans>

usr-sqlmap.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd" >
<sqlMap namespace="user">
	<typeAlias alias="userDO" type="com.alibaba.webx3.messageboard.dao.object.UserDO" />
	
	<insert id="insertUser" parameterClass="userDO" >
	insert into user (
	            id,
	            username,
	            password,
	            gmt_create,
	            gmt_modified
	         )  values (
	           #id#,
	           #username#,
	           #password#,
	           now(),
	           now()
	         )
	  <selectKey resultClass="int" keyProperty="id">
     	<![CDATA[SELECT LAST_INSERT_ID() AS ID ]]>
      </selectKey>
	</insert>
	
	<select id="selectByUsername" parameterClass="String" resultClass="userDO" >
	select
	      id,
	      username,
	      password,
	      gmt_create,
	      gmt_modified
	from
	      user
	where
	      username=#username#
	</select>
	
	<update id="updateuser" parameterClass="userDO">
	update 
	       user
	set
	       gmt_modified = now()
	   <dynamic prepend="" >
			<isNotEmpty prepend="," property="username">
				username = #username#
			</isNotEmpty>
			<isNotEmpty prepend="," property="password">
				password = #password#
			</isNotEmpty>
		</dynamic>
    	where id = #id#    
	
	</update>
	
	<delete id="deleteuser" parameterClass="userDO">
	delete 
	
	from 
	       user
	where 
	       id=#id#
	</delete>
	
</sqlMap>

sqlmap-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMapConfig PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN" "http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
	<sqlMap resource="sqlmap/user-sqlmap.xml" ></sqlMap>
	<sqlMap resource="sqlmap/message-sqlmap.xml" ></sqlMap>
</sqlMapConfig>

该文档为官方webx框架文档,对webx进行了全面的讲解,非常实用,并附学习的Demo 为什么要用Webx而不是其它的开源框架? 现在有很多Java的Web框架可供选择,并且它们也都是免费的。例如: • Struts • Webwork • Tapestry • Spring MVC 以上框架都是非常优秀的。说实话,如果阿里巴巴网站在2001年开始,就有这么多可选择的话,无论选择哪一个都不会有问题。因为这些年来,所有的开源Web框架都在互相学习、并趋于相似。Webx也不例外,它吸收了其它框架的很多想法。因此,当你使用Webx的时候,你会觉得在很多方面,它和其它开源的框架非常类似。我并不是说所有的框架都一样好,而是说只要假以时日,所有的框架在发展过程中,必然会积聚好的方面,淘汰坏的方面,从而变得足够好。从这个角度看,的确没有特别明显的理由来选择Webx,但也没有明显的理由不选择Webx。 另一方面,由于每一种框架采用不同的设计,必然会有各自的优势。Webx也是如此 —— 它在某些方面有一些独到的设计,超越了同类框架。Webx有哪些优势呢? Webx的优势 成熟可靠性 这个优势主要是针对阿里巴巴及属下网站而言。因为Webx在阿里巴巴和淘宝用了很多年。对于这种超大访问量的电子商务网站,Webx经受了考验,被证明是成熟可靠的。 开放和扩展性 • 对Spring的直接支持 —— Spring是当今主流的轻量级框架。Webx 3.0和Spring MVC一样, 完全建立在Spring框架之上,故可运用Spring的所有特性。 • 扩展性 —— Webx 3.0对Spring做了扩展,使Spring Bean不再是“bean”,而是升级成“组件”。一个组件可以扩展另一个组件,也可以被其它组件扩展。这种机制造就了Webx的非常好的扩展性,且比未经扩展的Spring更易使用。 • 开放性 —— Webx被设计成多个层次,层次间的分界线很清晰。每个层次都足够开放和易于扩展。你可以使用全部的Webx,也可以仅仅使用到Webx的任何一个层次。 引言 ............................................................................................................................... ix 1. 阅读向导 ............................................................................................................. ix 2. Webx是什么? .................................................................................................... ix 3Webx的历史 ....................................................................................................... ix 4. 为什么要用Webx而不是其它的开源框架? ............................................................. x 5. Webx的优势 ........................................................................................................ x 5.1. 成熟可靠性 ................................................................................................ x 5.2. 开放和扩展性 ............................................................................................. x 6. Webx还缺少什么? .............................................................................................. x 部分 I. Webx框架概览 ......................................................................
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值