spring+ibatis+mysql简单搭建
??<!--这是POJO映射文件的根元素-->
<sqlMap?namespace="Student">
??<!--
????select元素的id属性用来标识此元素,resultClass属性的值是Java类的全限定名(即包括类的包名)。
????resultClass属性可以让您指定一个Java类,根据ResultSetMetaData将其自动映射到JDBC的ResultSet。
????只要是Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。
????parameterClass属性是参数的类型,此属性的值是Java类的全限定名(即包括类的包名)。 它是可选的,但强烈建议使用。它的目的是
????限制输入参数的类型为指定的Java类,并 优化框架的性能。
??-->
??<select?id="getStudentById"?resultClass="cn.itcast.Student"
????parameterClass="int">
????select id,firstname,lastname from student where id=#value#
??</select>
??<insert?id="insertStudent"?parameterClass="cn.itcast.Student">
????insert into student(firstname,lastname) values(#firstname#,#lastname#)
??</insert>
</sqlMap>
(2)连接mysql数据库的配置文件:jdbc.propertiesjdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/itcast
jdbc.username=root
jdbc.password=admin
(3)Ibatis的总控文件:sql-map-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="configfile/Student.xml"?/>
</sqlMapConfig>
3、此时该Spring出场了,Spring的配置文件(applicationContext.xml)将jdbc.properties配置文件的内容关联成DataSource,再把DataSource关联到Ibatis的配置文件, 最后统一封装成为SqlMapClientTemplate模板类向外提供服务。?<?xml?version="1.0"?encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
??<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
??<bean?id="propertyConfig"
????class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
????<property?name="location">
??????<value>configfile/jdbc.properties</value>
????</property>
??</bean>
??<!--配置一个数据源,根据上面propertyConfig指定的location去找数据库连接的配置信息-->
??<bean?id="dataSource"
????class="org.springframework.jdbc.datasource.DriverManagerDataSource">
????<property?name="driverClassName">
??????<value>${jdbc.driver}</value>
????</property>
????<property?name="url">
??????<value>${jdbc.url}</value>
????</property>
????<property?name="username">
??????<value>${jdbc.username}</value>
????</property>
????<property?name="password">
??????<value>${jdbc.password}</value>
????</property>
??</bean>
??<!--根据dataSource和configLocation创建一个SqlMapClient-->
??<bean?id="sqlMapClient"?class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
????<property?name="configLocation">
??????<value>configfile/sql-map-config.xml</value>
????</property>
????<property?name="dataSource">
??????<ref?bean="dataSource"?/>
????</property>
??</bean>
??<!--根据sqlMapClien创建一个SqlMapClient模版类-->
??<bean?id="sqlMapClientTemplate"?class="org.springframework.orm.ibatis.SqlMapClientTemplate">
????<property?name="sqlMapClient">
??????<ref?bean="sqlMapClient"?/>
????</property>
??</bean>
??<!--将上面的模版类织入到我们的DAO对象中-->
??<bean?id="studentDao"?class="cn.itcast.StudentDaoSqlMap">
????<property?name="sqlMapClientTemplate">
??????<ref?bean="sqlMapClientTemplate"?/>
????</property>
??</bean>
</beans>