利用maven创建webx3项目——实现简单的留言板(四)
配置持久层4、配置数据源
首先在本地新建一个mysql数据库,端口设为3306,数据库的名字为message_board,其中有两个表
?
user表
?
?
message表
?
?
?
在持久层使用spring框架中的ibatis,所以需要在项目中集成ibaits框架,而且数据库使用的是mysql,也要在项目中导入mysql的驱动依赖包,此外这个项目中会使用数据库连接池,所以也要导入数据库连接池的依赖包。
?
依赖的包可以在maven仓库里查找(http://search.maven.org/#search%7Cga%7C1%7C);
?
查找ibatis,选择org.apache.servicemix.bundles.ibatis-sqlmap这个包,版本号2.3.4.726_4;
?
查找dbcp,选择commons-dbcp,版本号1.2.2;
?
查找mysql,选择mysql-connector-java,版本号5.1.6
?
注意:以上选的包只是随便选的,只是为了实现本项目的功能,没有什么标准,如果有其他要求,可以选择其他的包。
?
因为我们使用maven管理项目,可以方便的在pom.xml中配置这些依赖。
?
在pom.xml中增加的配置项如下:
?
-----------------------------------
?
设置依赖的版本号:
?
?
<properties> ...... <ibatis-version>2.3.4.726_4</ibatis-version> <dbcp-version>1.2.2</dbcp-version> <mysql-version>5.1.6</mysql-version></properties>?
?
设置依赖的包:
?
<dependencies> ...... <!-- 集成 ibatis -导入依赖包--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> </dependency> <dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.ibatis-sqlmap</artifactId> </dependency> <!-- 导入mysql驱动jar包 --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency> </dependencies>
?
?
设置管理依赖项:
?
?
<dependencyManagement> <dependencies> <!-- 集成 ibatis -导入依赖包--> <dependency> <groupId>commons-dbcp</groupId> <artifactId>commons-dbcp</artifactId> <version>${dbcp-version}</version> </dependency> <dependency> <groupId>org.apache.servicemix.bundles</groupId> <artifactId>org.apache.servicemix.bundles.ibatis-sqlmap</artifactId> <version>${ibatis-version}</version> </dependency> <!-- 导入mysql驱动jar包 --> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>${mysql-version}</version></dependency></dependencies>......<dependencyManagement>
?
-----------------------------------
?
然后配置项目的持久层框架
?
首先在src/main/resources中建立以下文件:
?
? ? ? ? ? ?
?
配置上面的文件
?
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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"><!-- 数据库配置 --><bean id= "dataSource" 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"ref="dataSource" /></bean><bean id="transactionTemplate" ref="transactionManager"></property></bean><!-- iBatis SQL map定义 --><bean id="sqlMapClient" ref="dataSource" /><property name="configLocation" value="classpath:sqlmap-config.xml" /></bean></beans>
?
-----------------------------------
?
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>
?
-----------------------------------
?
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" parameter>insert into message ( id, title, author, content, gmt_create, gmt_modified ) values ( #id#, #title#, #author#, #content#, now(), now() ) <selectKey resultkeyProperty="id"> <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> </selectKey></insert><select id="selectById" parameterresult>select id, title, author, content, gmt_create, gmt_modifiedfrom messagewhere id=#id#</select><select id="selectBylist" parameterresult>select id, title, author, content, gmt_create, gmt_modifiedfrom message order by gmt_modified desclimit #from#,#size#</select><update id="updateMessage" parameter><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" parametername="code"><?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" parameter>insert into user ( id, username, password, gmt_create, gmt_modified ) values ( #id#, #username#, #password#, now(), now() ) <selectKey resultkeyProperty="id"> <![CDATA[SELECT LAST_INSERT_ID() AS ID ]]> </selectKey></insert><select id="selectByUsername" parameterresult>select id, username, password, gmt_create, gmt_modifiedfrom userwhere username=#username#</select><update id="updateuser" parameter><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 userwhere id=#id#</delete></sqlMap>
-----------------------------------
?
至此,数据源配置结束。?