spring_150907_sqlmapclientdaosupport_getSqlMapClient
1、新建java工程:spring_150907_sqlmapclientdaosupport_getSqlMapClient,如下图所示:
2、工程里添加spring、hibernate、ibatis相关jar包,如下图所示:
3、新建DogPetService接口:
package com.spring.service; public interface DogPetService {
public void queryAllDogPets();
}
4、新建DogPetService接口的实现类DogPetServicImpl:
package com.spring.service.impl; import java.util.List; import javax.annotation.Resource; import org.springframework.stereotype.Controller; import com.spring.service.DogPetService;
import com.spring.dao.DogPetDAO;
import com.spring.model.DogPet; @Controller(value="DogPetService")
public class DogPetServiceImpl implements DogPetService{
private DogPetDAO dogPetDAO; public DogPetDAO getDogPetDAO() {
return dogPetDAO;
} @Resource(name="dogPetDAO")
public void setDogPetDAO(DogPetDAO dogPetDAO) {
this.dogPetDAO = dogPetDAO;
} @Override
public void queryAllDogPets() {
List<DogPet> list = dogPetDAO.queryAllDogPets();
if(list != null)
{
for(DogPet d:list)
{
System.out.println(d.toString());
}
}
}
}
5、新建DogPetDAO类:
package com.spring.dao; import java.util.List; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.spring.model.DogPet; public class DogPetDAO extends SqlMapClientDaoSupport
{ public List<DogPet> queryAllDogPets()
{
List<DogPet> list = null;
try
{
list = getSqlMapClient().queryForList("c_queryDog");
}
catch (Exception e)
{
e.printStackTrace();
}
return list;
} }
5、新建实体类DogPet:
package com.spring.model; public class DogPet { private int id;
private String name;
private int age;
private String kind;
private String sex;
private String health; public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getHealth() {
return health;
}
public void setHealth(String health) {
this.health = health;
} public String toString()
{
return id+"--"+name+"--"+kind+"--"+age+"--"+health;
}
}
7、新建jdbc.properties属性文件:
driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@10.32.145.254:1521:cspperf
user=icdwf
password=icdwf
8、新建beans.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 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.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config/> <context:component-scan base-package="com.spring"></context:component-scan> </beans>
9、新建dao.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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" 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/jee http://www.springframework.org/schema/jee/spring-jee-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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 利用annotation配置声明式事物管理 begin-->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="${driver}" />
<property name="url" value="${url}" />
<property name="username" value="${user}" />
<property name="password" value="${password}" />
</bean> <!--<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="annotatedClasses">
<list>
<value>com.spring.model.DogPet</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.MySQLDialect
</value>
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean> --> <!--<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="query*" read-only="true" />
<tx:method name="*" />
</tx:attributes>
</tx:advice> <aop:config>
<aop:pointcut id="dogPetServiceOperation"
expression="execution(* com.spring..*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="dogPetServiceOperation" />
</aop:config> --> <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" >
<value>classpath:sqlMapConfig.xml</value>
</property>
<property name="dataSource" ref="dataSource"/>
</bean> <!--<tx:annotation-driven transaction-manager="txManager"/> --><!-- 利用annotation配置声明式事物管理 end--> <!--
1、继承HibernateDaoSupport类,第一种数据库访问方式 设置HibernateDaoSupport抽象类
<bean id="hibernateDaoSupport"
class="org.springframework.orm.hibernate3.support.HibernateDaoSupport"
abstract="true">
<property name="sessionFactory" ref="sessionFactory" />
</bean> dao的操作的bean
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO" parent="hibernateDaoSupport" /> 2、继承HibernateDaoSupport类,第二种数据库访问方式
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean> --> <!--
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory" />
</bean> -->
<bean id="dogPetDAO" class="com.spring.dao.DogPetDAO">
<property name="sqlMapClient" ref="sqlMapClient"/>
</bean> </beans>
10、新建sqlMapConfig文件:
<?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>
<!--<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" errorTracingEnabled="true" maxRequests="32"
maxSessions="10" maxTransactions="5" useStatementNamespaces="false" />
--><!--<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="JDBC.ConnectionURL" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="JDBC.Username" value="icdwf" />
<property name="JDBC.Password" value="icdwf" />
<property name="Pool.MaximumActiveConnections" value="10" />
<property name="Pool.MaximumIdleConnections" value="5" />
<property name="Pool.MaximumCheckoutTime" value="120000" />
<property name="Pool.TimeToWait" value="500" />
<property name="Pool.PingQuery" value="select 1 from ACCOUNT" />
<property name="Pool.PingEnabled" value="false" />
<property name="Pool.PingConnectionsOlderThan" value="1" />
<property name="Pool.PingConnectionsNotUsedFor" value="1" />
</dataSource>
</transactionManager>
-->
<sqlMap resource="dogPet.xml" />
</sqlMapConfig>
11、新建dogPet.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE sqlMap
PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">
<sqlMap namespace="Dog">
<typeAlias alias="dog" type="com.spring.model.DogPet" />
<select id="c_queryDog" resultClass="dog">
<![CDATA[
select *
from t_dog
]]>
</select>
<delete id="c_deleteDog" parameterClass="dog">
<![CDATA[
delete t_dog
where name=#name#
and id = #id#
]]>
</delete>
<delete id="c_updateDog" parameterClass="dog">
<![CDATA[
update t_dog
set name=#name#
where id = #id#
]]>
</delete>
<select id="c_queryDogById" parameterClass="dog"
resultClass="dog">
<![CDATA[
select *
from t_dog
where name=#name#
and id = #id#
]]>
</select>
<!--<insert id="c_insertDog" parameterClass="dog">
INSERT INTO t_dog (
id,
name,
password,
userlevel,
registtime,
extend1,
extend2,
extend3)
VALUES (
#id#,
#name#,
#password#,
#userLevel#,
#registTime#,
#extend1#,
#extend2#,
#extend3#
)
</insert> --></sqlMap>
12、新建test类用于测试的:
package com.spring.test; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.spring.service.DogPetService; public class ServiceTest { public static void main(String[] args)
{
ApplicationContext ctx = new ClassPathXmlApplicationContext(new String[]{"beans.xml","dao.xml"});
DogPetService dogPetService = (DogPetService)ctx.getBean("DogPetService");
dogPetService.queryAllDogPets();
} }
spring_150907_sqlmapclientdaosupport_getSqlMapClient的更多相关文章
随机推荐
- mongodb replica set 和 nodejs中使用mongoose连接replica
一.mongodb replication 介绍 官网上的第一句话就是Replication is the process of synchronizing data across multiple ...
- windows7中用vitualbox安装OS X 10.11 El Capitan 及 Xcode 7.0--转载
在 Win 7或8 下使用 VirtualBOX 虚拟机安装 OS X 10.11 El Capitan 及 Xcode 7.0 来源:http://bbs.feng.com/read-htm-tid ...
- HBuilder mui登录和访问控制教程--转载
HBuilder mui登录和访问控制教程 mui中提供了登录的模板页,但是对于登录后各个页面的访问控制,刷新等并没有官方的推荐方案.我在这里简单说一种初级的解决方案吧,肯定有不足指出,欢迎批评指正. ...
- OpenCV---图像加载与保存
一:获取图像的信息 什么是图像: 结构化存储的数据信息 图像属性: -通道数目 -高与宽 -像素数据 -位图深度 import cv2 as cv def get_image_info(image): ...
- EL表达式格式化日期
在EL表达式中要显示"yyyy-MM-dd"格式的日期: 使用<fmt:>格式化标签 1 在页面上导入 <%@ taglib prefix=" ...
- Spring整合JMS(四)——事务管理(转)
*注:别人那复制来的 Spring提供了一个JmsTransactionManager用于对JMS ConnectionFactory做事务管理.这将允许JMS应用利用Spring的事务管理特性.Jm ...
- npm 的使用指南
npm 使用指南 因为有写关于node.js的配置的博客,还有node和gulp的前端信息配置使用,其中有很多命令都用到了npm.所以这里要着重介绍一下npm. 1 npm介绍 npm(mode pa ...
- 01 DIV+CSS 固定页面布局
本文讲解使用DIV+CSS布局最基本的内容,读完本文你讲会使用DIV+CSS进行简单的页面布局. DIV+CSS布局中主要CSS属性介绍: Float: Float属性是DIV+CSS布局中最基本也是 ...
- Jenkins 通过ssh 拷贝文件到远程机器上。
想实现的目的是: 在构建之前,从jenkins master上拷贝脚本到需要运行的机器上(linux ssh). 本来是通过publish over ssh 的transfer set可以直接设置,但 ...
- 【CODEVS】1034 家园
[算法]网络流-最大流(dinic) [题解] 飞船有可承载人数限制,地球为源点,月球为汇点,人像水流一样从以飞船上限为容量的边流向汇点. 人在各站点都面临着上船与否的选择,难以用DP解决最优策略,于 ...