spring与ibatis整合的配置文件

[color=green]然,要下载到ibatis和spring的jar包。然后,修改applicationContext.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:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-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/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
default-autowire="byName">
<context:annotation-config></context:annotation-config>

<!--此bean告诉Spring去哪找数据库的配置信息,因为有此Bean才出现下面用${}标记来取变量的语句-->
<bean id="propertyConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath: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>classpath: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>
<!-- 事务配置 -->
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>

<!--
使用annotation定义事务 <tx:annotation-driven
transaction-manager="transactionManager" />
-->
<!-- 使用annotation定义事务 -->
<tx:annotation-driven />

<!-- 自动扫描Service,同时还启用了注释驱动自动注入的功能 -->
<context:component-scan base-package="com.kentop.wireless" />

<!-- 保证POJO中标注@Required的属性被注入 -->
<bean
class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" />

</beans>
然后建一个properties文件,内容如下:jdbc.properties
jdbc.driver=数据库驱动
jdbc.url=数据库的url地址
jdbc.username=数据库用户名
jdbc.password=数据库密码

然后要配置web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>wireless</display-name>

<!--
Spring ApplicationContext配置文件的路径,可使用通配符,多个路径用,号分隔 此参数用于后面的Spring
Context Loader
-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>

<!-- Character Encoding filter -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>


<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!--Spring ApplicationContext 载入 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Spring 刷新Introspector防止内存泄露 -->
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>

<!-- session超时定义,单位为分钟 -->
<session-config>
<session-timeout>20</session-timeout>
</session-config>


<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>


接下来要写ibatis的配置文件:
sql-map-config.xml:

<?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="Student.xml"/>
</sqlMapConfig>

注意:这个sql-ma-config.xml文件需要在在applicationContext.xml中指定。
然后就可以开始写ibatis的sqlMap了。
Student.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">
<!--这是POJO映射文件的根元素-->
<sqlMap namespace="Student">
<!--select元素的id属性用来标识此元素,resultClass属性的值是Java类的全限定名(即包括类的包名)。
resultClass属性可以让您指定一个Java类,根据ResultSetMetaData将其自动映射到JDBC的ResultSet。
只要是Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。
parameterClass属性是参数的类型,此属性的值是Java类的全限定名(即包括类的包名)。
它是可选的,但强烈建议使用。它的目的是 限制输入参数的类型为指定的Java类,并
优化框架的性能。-->
<select id="getStudentById" resultClass="pojo.Student" parameterClass="int">
select id,firstname,lastname from student where id=#value#
</select>

<insert id="insertStudent" parameterClass="pojo.Student">
insert into student(firstname,lastname) values(#firstname#,#lastname#)
</insert>
</sqlMap>

对应的Student.java文件内容如下:

package pojo;

public class Student implements java.io.Serializable {

private static final long serialVersionUID = -8263105765203850824L;

private Integer id;

private String firstname;

private String lastname;

public String getFirstname() {
return firstname;
}

public void setFirstname(String firstname) {
this.firstname = firstname;
}

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getLastname() {
return lastname;
}

public void setLastname(String lastname) {
this.lastname = lastname;
}
}

对应的StudenDao.java文件如下:

package dao;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

import pojo.Student;
@Service
public class StudentDao {

private SqlMapClientTemplate sqlMapClientTemplate;
public SqlMapClientTemplate getSqlMapClientTemplate() {
return sqlMapClientTemplate;
}
@Required
public void setSqlMapClientTemplate(SqlMapClientTemplate sqlMapClientTemplate) {
this.sqlMapClientTemplate = sqlMapClientTemplate;
}
public Student getStudent(String id){
return (Student)sqlMapClientTemplate.queryForObject(“getStudentById", id);
}

}

使用的时候,比如在JSP页面使用的时候,可以这样用:

ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
StudentDao sd=(StudentDao)ctx.getBean("studentDao");
Student student = sd.getStaffById(11);
out.println(student.getId());
[/color][size=large][/size]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值