一,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("更新成功"); }*/ } }