package com.template.daoImpl;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import com.template.dao.UserDao;
import com.template.model.User;
public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public User getUserById(int id) {
String sql = "select id, username from T_USER where id=?";
Object[] args = new Object[]{id};
final User user = new User();
jdbcTemplate.query(sql, args, new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
}
});
return user;
}
public void saveUser(User user) {
String sql = "insert into T_USER(username, password) values(?,?)";
Object[] args = new Object[]{user.getId(),user.getUsername()};
int[] argTypes = new int[]{Types.VARCHAR, Types.VARCHAR};
jdbcTemplate.update(sql, args, argTypes);
}
Boolean b =false;
public boolean exitUser(String username, String password) {
String sql = "select username from T_USER where username=? and password=?";
Object[] args = new Object[]{username,password};
RowCallbackHandler argTypes = new RowCallbackHandler(){
public void processRow(ResultSet rs) throws SQLException {
if(rs.getString("username").equals("")){
b=false;
}else{
b=true;
}
}
};
jdbcTemplate.query(sql, args, argTypes);
System.out.println(b);
return b;
}
}
------------------------------------------------------------------------applicationContext
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
---------------------------------------------------------------junitTest
package com.template.daoImpl;
import static org.junit.Assert.fail;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.template.dao.UserDao;
public class UserDaoImplTest {
private BeanFactory beanFactory =null;
private UserDao dao =null;
@Before
public void setUp() throws Exception {
beanFactory = new ClassPathXmlApplicationContext("applicationContext.xml");
dao = (UserDao) beanFactory.getBean("userDao");
}
@After
public void tearDown() throws Exception {
dao =null;
beanFactory =null;
}
@Test
public void testGetUserById() {
dao.getUserById(1);
}
@Test
public void testSaveUser() {
}
@Test
public void testExitUser() {
dao.exitUser("admin","123");
}
}
------------------------------------------------------------------------------
antlr-2.7.6.jar
asm-all-3.2.jar
aspectjrt.jar
aspectjweaver.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar
com.springsource.org.apache.commons.pool-1.5.3.jar
commons-beanutils-1.7.0.jar
commons-collections-3.1.jar
commons-collections-3.2.jar
commons-dbutils-1.3-javadoc.jar
commons-dbutils-1.3-sources.jar
commons-dbutils-1.3.jar
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-lang-2.3.jar
commons-logging-1.0.4.jar
commons-logging-api-1.1.jar
db2jcc_license_cu.jar
db2jcc.jar
dom4j-1.6.1.jar
ezmorph-1.0.3.jar
freemarker-2.3.16.jar
hibernate-jpa-2.0-api-1.0.0.Final.jar
hibernate3.jar
httpclient-4[1].0.1.jar
httpcore-4[1].0.1.jar
javassist-3.12.0.GA.jar
jsf-api.jar
jsf-impl.jar
json-lib-2.1-jdk15.jar
jstl-1.2.jar
jta-1.1.jar
log4j-1.2.16.jar
mysql-connector-java-5.0.4-bin.jar
ognl-3.0.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.3.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.spring-library-3.0.5.RELEASE.libd
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar
oro-2.0.8.jar
slf4j-api-1.6.1.jar
slf4j-log4j12-1.6.1.jar
struts2-core-2.2.1.jar
struts2-spring-plugin-2.2.1.jar
xwork-core-2.2.1.jar
jdbcTemplate-spring对jdbc的支持
原创mb6444ed45406a4 ©著作权
©著作权归作者所有:来自51CTO博客作者mb6444ed45406a4的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
实践使用Spring JDBC API JdbcTemplate来对MySQL数据库进行增删改查操作
实践使用Spring JDBC API JdbcTemplate来对MySQL数据库进行增删改查操作
sql JdbcTemplate Spring JDBC API MySQL -
Spring JDBC、Spring JdbcTemplate 、
Spring JDBC抽象框架所带来的价值将在以下几个方面得以体现:(注:使用了Spring JDBC抽象框架之后,应用开发人员只需要
Spring JDBC Spring Template JdbcTemplate sql spring -
Spring对jdbc支持
使用步骤: 1)引入jar文件 spring-jdbc-3.2.5.RELEASE.jar spring-tx-3.2.5.RELEASE.jar 2) 优化
sql spring java xml mysql -
Spring中支持JDBC的JdbcTemplate模板类的使用
概述 Spring JDBC抽象框架core包提供了JDBC模板类,其中JdbcTe固定部分,如连接的创建及关闭。
spring JdbcTemplate 模版 bc java -
hualinux spring 3.16:Spring对JDBC的支持(了解)
Spring有现成的JDBC模板JdbcTemplate ,可以对SQL进行操作,但实际开发中一般不这样用,初学者
hualinux hualinux spring JdbcTemplate spring xml开发 -
Spring对Hibernate及JDBC提供的支持
关键字:Spring对Hibernate集JDBC提供的支持。提供的分页支持:** * 使用hql 语句进行操作 * @p
JDBC Spring Hibernate SQL .net -
Spring对JDBC及hibernate的支持
原文地址:Spring对JDBC及hibernate的支持作者:木马网吧一:作用:1.定义:spring的dao不是新发明一种技术,而是对原来技术的一种封装,定义了一套简单实用的api2.使用dao的好处:降低了业务逻辑层和具体的持久层技
Spring JDBC Hibernate spring hibernate