一,Dao

IUserinfDao
package com.dkt.dao;

import java.util.List;

import com.dkt.entity.Userinfo;

public interface IUserinfDao {

	public int addUserinfo(Userinfo ui);
// 查询返回的是map集合,一行数据一个map,直接去key为属性
public List<Userinfo> queryUserinfo(Userinfo ui);
public Userinfo queryUser(int i); public String queryUserphone(int id);//查询单个字段
public int deleteUserinfo(int id);
}
UserinfDaoImpl 
package com.dkt.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List; import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper; import com.dkt.dao.IUserinfDao;
import com.dkt.entity.Userinfo; public class UserinfDaoImpl extends JdbcTemplate implements IUserinfDao{ public int addUserinfo(Userinfo ui) {
int i = super.update("insert into userinfo values(null,?,?,?,?,?)",
new Object[]{ui.getUserphone(),ui.getUserpnike(),ui.getUserpwd(),ui.getUsergreate().intValue(),ui.getUserreagedate()});
return i;
} public List<Userinfo> queryUserinfo(Userinfo ui) {
List<Userinfo> list = super.query("select * from userinfo where userphone='"+ui.getUserphone()+"'",
new BeanPropertyRowMapper<Userinfo>(Userinfo.class));
return list;
} public int deleteUserinfo(int id) {
int delete = super.update("delete from userinfo where userid= ? ", new Object[]{id});
return delete;
} public int updateUserinfo(Userinfo ui) {
int update = super.update("update userinfo set userpnike = ? where userid= ? ", new Object[]{ui.getUserpnike(),ui.getUserid()});
return update;
} public String queryUserphone(int id) {
String i = super.queryForObject("select userphone from userinfo where userid=?", new Object[]{id},String.class);
return i;
} public Userinfo queryUser(int i) {
return super.queryForObject(" select * from userinfo where userid=?",
new Object[]{i},
// new BeanPropertyRowMapper<Userinfo>(Userinfo.class)
// 内部类实现得到对象
new RowMapper<Userinfo>(){
public Userinfo mapRow(ResultSet arg0, int arg1)
throws SQLException {
Userinfo userinfo = new Userinfo(arg0.getString("userphone"),
arg0.getString("userpwd"),
arg0.getTimestamp("userreagedate"));
return userinfo;
}
});
} }

二,entity

package com.dkt.entity;

import java.sql.Timestamp;

/**
* Userinfo entity. @author MyEclipse Persistence Tools
*/ public class Userinfo implements java.io.Serializable { // Fields private Integer userid;
private String userphone;
private String userpnike;
private String userpwd;
private Integer usergreate;
private Timestamp userreagedate; // Constructors /** default constructor */
public Userinfo() {
} /** minimal constructor */
public Userinfo(String userphone, String userpwd, Timestamp userreagedate) {
this.userphone = userphone;
this.userpwd = userpwd;
this.userreagedate = userreagedate;
} public Userinfo(String userphone, String userpnike, String userpwd,
Timestamp userreagedate) {
super();
this.userphone = userphone;
this.userpnike = userpnike;
this.userpwd = userpwd;
this.userreagedate = userreagedate;
} /** full constructor */
public Userinfo(String userphone, String userpnike, String userpwd,
Integer usergreate, Timestamp userreagedate) {
this.userphone = userphone;
this.userpnike = userpnike;
this.userpwd = userpwd;
this.usergreate = usergreate;
this.userreagedate = userreagedate;
} // Property accessors public Integer getUserid() {
return this.userid;
} public void setUserid(Integer userid) {
this.userid = userid;
} public String getUserphone() {
return this.userphone;
} public void setUserphone(String userphone) {
this.userphone = userphone;
} public String getUserpnike() {
return this.userpnike;
} public void setUserpnike(String userpnike) {
this.userpnike = userpnike;
} public String getUserpwd() {
return this.userpwd;
} public void setUserpwd(String userpwd) {
this.userpwd = userpwd;
} public Integer getUsergreate() {
return this.usergreate;
} public void setUsergreate(Integer usergreate) {
this.usergreate = usergreate;
} public Timestamp getUserreagedate() {
return this.userreagedate;
} public void setUserreagedate(Timestamp userreagedate) {
this.userreagedate = userreagedate;
} }

三,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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"><value>com.mysql.jdbc.Driver</value> </property>
<property name="username"><value>root</value> </property>
<property name="password"><value>123456</value> </property>
<property name="url"><value>jdbc:mysql://localhost:3306/marry?userUnicode=true&characterEncoding=utf-8</value> </property>
</bean> <bean id="userdao" class="com.dkt.dao.impl.UserinfDaoImpl">
<property name="dataSource" ref="ds"></property>
</bean> </beans> 

四,test

package com.dkt.test;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List; import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dkt.dao.impl.UserinfDaoImpl;
import com.dkt.entity.Userinfo; public class TsetJDBC { public static void main(String[] args) { BeanFactory bf = new ClassPathXmlApplicationContext("applicationContext.xml");
UserinfDaoImpl udao = (UserinfDaoImpl)bf.getBean("userdao");
/*int i = udao.addUserinfo(new Userinfo("13993723892", "小黑", "qewres",9,new Timestamp(new Date().getTime())));
if (i>0) {
System.out.println("添加成功");
}*/
Userinfo userinfo = new Userinfo();
userinfo.setUserphone("13996932874");
List<Userinfo> user = udao.queryUserinfo(userinfo);
for (Userinfo userinfo2 : user) {
System.out.println(userinfo2.getUserid());
} /*Userinfo user2 = udao.queryUser(10);
System.out.println(user2.getUserphone());*/ /*int i = udao.deleteUserinfo(11);
if (i>0) {
System.out.println("删除成功");
}*/
/*System.out.println(udao.queryUserphone(10)); Userinfo ui = new Userinfo();
ui.setUserid(4);
ui.setUserpnike("咳咳");
int u = udao.updateUserinfo(ui);
if (u>0) {
System.out.println("更新成功");
}*/
}
}

  

springJDBC 中JdbcTemplate 类方法使用的更多相关文章

  1. SpringJDBC中jdbcTemplate 的使用

    一:定义 SpringJDBC是spring官方提供的一个持久层框架,对JDBC进行了封装,提供了一个JDBCTemplated对象简化JDBC的开发.但Spring本身不是一个orm框架,与hibe ...

  2. OC基础--OC中的类方法和对象方法

    PS:个人感觉跟C#的静态方法和非静态方法有点类似,仅仅是有点类似.明杰老师说过不要总跟之前学过的语言做比较,但是个人觉得,比较一下可以加深印象吧.重点是自己真的能够区分开! 一.OC中的对象方法 1 ...

  3. Spring 中jdbcTemplate 实现执行多条sql语句

    说一下Spring框架中使用jdbcTemplate实现多条sql语句的执行: 很多情况下我们需要处理一件事情的时候需要对多个表执行多个sql语句,比如淘宝下单时,我们确认付款时要对自己银行账户的表里 ...

  4. python中,类方法和静态方法区别。

    面相对象程序设计中,类方法和静态方法是经常用到的两个术语. 逻辑上讲:类方法是只能由类名调用:静态方法可以由类名或对象名进行调用. 在C++中,静态方法与类方法逻辑上是等价的,只有一个概念,不会混淆. ...

  5. Spring中JdbcTemplate的基础用法

    Spring中JdbcTemplate的基础用法 1.在DAO中使用JdbcTemplate 一般都是在DAO类中使用JdbcTimplate,在XML配置文件中配置好后,可以在DAO中注入即可. 在 ...

  6. 【转】java中Thread类方法介绍

    原文: java中Thread类方法介绍 http://blog.csdn.net/seapeak007/article/details/53395609 这篇文章找时间分析一下!!!:http:// ...

  7. 使用type在对象方法中调用类方法

    type简介 type在Python中的作用是创建一个类. 我们创建类的时候一般会使用这样的方法: # -*- coding:utf-8 -*- class Student(object): coun ...

  8. SpringJDBC的JdbcTemplate在MySQL5.7下不支持子查询的问题

    org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [ SELECT ...

  9. SSM-Spring-19:Spring中JdbcTemplate

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- Spring自带一个ORM持久化框架JdbcTemplate,他可以说是jdbc的加强版,但是对最细微的控制肯 ...

随机推荐

  1. Elasticsearch分页

    Elasticsearch的数据都存在每个节点的分片中,当执行搜索时每个分片独立搜索后,数据再经过整合返回.ElasticSearch的搜索请求一次请求最大量为10000.如果超过则会发生错误.那么, ...

  2. C#里面const和readonly

    一.const关键字限定一个变量不允许被改变. 使用const在一定程度上可以提高程序的安全性和可靠性. 1.用于修改字段或局部变量的声明,表示指定的字段或局部变量的值是常数,不能被修改. 2.常数声 ...

  3. (转)ReentrantLock可重入锁的使用场景

    原文: http://my.oschina.net/noahxiao/blog/101558

  4. 使用SUI框架下的<a>标签点击跳转页面不刷新的问题

    最近写好了几个页面,今天试着将各个页面的链接打通,然后问题就来了...(╯︵╰) 这里看一下原来想要实现的两个页面跳转的效果--点击图一标注的栏目可以跳转到一个新的页面图二... 按照之前写a标签的跳 ...

  5. 【hdu6121】 Build a tree 简单数学题

    题目传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6121 我好像推得挺久的诶..... 题目大意:给你一棵有$n$个点的树,根节点为$0$,对于其余节点 ...

  6. 2018春招-今日头条笔试题-第三题(python)

    题目描述:2018春招-今日头条笔试题5题(后附大佬答案-c++版) 解题思路: 本题的做法最重要的应该是如何拼出‘1234567890’,对于输入表达试获得对应的结果利用python内置函数eval ...

  7. 【树】Count Complete Tree Nodes

    题目: 求完全二叉树节点数. 思路: 满二叉树的节点数是2^k-1,k是树的深度. 所以我们可以先判断该树是否为满二叉树,然后是的话直接返回结果,如果不是递归地求解子树. 这样不用遍历所有的节点.复杂 ...

  8. 编写Android工程里测试代码的步骤

    第一步: 写个类去继承 AndroidTestCase public class TestStudent extends AndroidTestCase 并且编写一个测试的方法, 注意,测试的方法必须 ...

  9. Linux vim 编辑命令

    vi命令命令模式:yy:复制 光标所在的这一行 4yy:复制 光标所在行开始向下的4行p: 粘贴dd:剪切 光标所在的这一行2dd:剪切 光标所在行 向下 2行D:从当前的光标开始剪切,一直到行末d0 ...

  10. Ubuntu11.10 带图形安装步骤详解

    不多说,直接上干货! 成功!  关于Ubuntu的网络设置.静态IP设置,见 Ubuntukylin-14.04-desktop( 不带分区)安装步骤详解 Ubuntukylin-14.04-desk ...