欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java开发之Spring连接数据库方法实例分析

程序员文章站 2024-03-06 12:28:07
本文实例讲述了java开发之spring连接数据库方法。分享给大家供大家参考,具体如下: 接口: package cn.com.service; impor...

本文实例讲述了java开发之spring连接数据库方法。分享给大家供大家参考,具体如下:
接口:

package cn.com.service; 
import java.util.list; 
import cn.com.bean.personbean; 
public interface personservice { 
 //保存 
 public void save(personbean person); 
 //更新 
 public void update(personbean person); 
 //获取person 
 public personbean getperson(int id); 
 public list<personbean> getpersonbean(); 
 //删除记录 
 public void delete(int personid); 
}

person bean类:

package cn.com.bean; 
public class personbean { 
 private int id; 
 private string name; 
 public personbean(string name) { 
  this.name=name; 
 } 
 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; 
 } 
}

接口实现:

package cn.com.service.impl; 
import java.util.list; 
import javax.sql.datasource; 
import org.springframework.jdbc.core.jdbctemplate; 
import cn.com.bean.personbean; 
import cn.com.service.personservice; 
public class personserviceimpl implements personservice { 
 private jdbctemplate jdbctemplate; 
 public void setdatasource(datasource datasource) { 
  this.jdbctemplate = new jdbctemplate(datasource); 
 } 
 @override 
 public void save(personbean person) { 
  // todo auto-generated method stub 
  jdbctemplate.update("insert into person(name) values(?)", new object[]{person.getname()}, 
    new int[]{java.sql.types.varchar}); 
 } 
 @override 
 public void update(personbean person) { 
  // todo auto-generated method stub 
  jdbctemplate.update("update person set name=? where id=?", new object[]{person.getname(),person.getid()}, 
    new int[]{java.sql.types.varchar,java.sql.types.integer}); 
 } 
 @override 
 public personbean getperson(int id) { 
  // todo auto-generated method stub 
  return (personbean)jdbctemplate.queryforobject("select * from person where id=?", new object[]{id}, 
    new int[]{java.sql.types.integer},new personrowmapper() ); 
 } 
 @suppresswarnings("unchecked") 
 @override 
 public list<personbean> getpersonbean() { 
  // todo auto-generated method stub 
  return (list<personbean>)jdbctemplate.query("select * from person", 
    new personrowmapper() ); 
 } 
 @override 
 public void delete(int personid) { 
  // todo auto-generated method stub 
  jdbctemplate.update("delete from person where id=?", new object[]{personid},
    new int[]{java.sql.types.integer}); 
 } 
}

rowmapper:

package cn.com.service.impl; 
import java.sql.resultset; 
import java.sql.sqlexception; 
import org.springframework.jdbc.core.rowmapper; 
import cn.com.bean.personbean; 
public class personrowmapper implements rowmapper { 
 @override 
 public object maprow(resultset rs, int index) throws sqlexception { 
  // todo auto-generated method stub 
  personbean person =new personbean(rs.getstring("name")); 
  person.setid(rs.getint("id")); 
  return person; 
 } 
}

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:context="http://www.springframework.org/schema/context" 
  xmlns:aop="http://www.springframework.org/schema/aop" 
  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/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
   http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
   ">   
    <!-- <context:property-placeholder location="classpath:jdbc.properties"/> --> 
    <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource" destroy-method="close"> 
    <property name="driverclassname" value="com.mysql.jdbc.driver"/> 
    <property name="url" value="jdbc:mysql://localhost:3306/wy"/> 
    <property name="username" value="root"/> 
    <!-- property池启动时的初始值 --> 
     <property name="password" value="123"/> 
     <!-- 连接name="initialsize" value="${initialsize}"/>--> 
     <property name="initialsize" value="1"/> 
     <!-- 连接池的最大值 --> 
     <property name="maxactive" value="500"/> 
     <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxidle为止 --> 
     <property name="maxidle" value="2"/> 
     <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 --> 
     <property name="minidle" value="1"/> 
    </bean> 
    <bean id="txmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> 
    <property name="datasource" ref="datasource"/> 
    </bean> 
    <tx:annotation-driven transaction-manager="txmanager"/> 
    <bean id="personservice" class="cn.com.service.impl.personserviceimpl"> 
    <property name="datasource" ref="datasource"></property> 
    </bean> 
</beans> 

测试类:

package junit.test; 
import static org.junit.assert.*; 
import org.junit.beforeclass; 
import org.junit.test; 
import org.springframework.context.applicationcontext; 
import org.springframework.context.support.classpathxmlapplicationcontext; 
import cn.com.bean.personbean; 
import cn.com.service.personservice; 
public class persontest2 { 
 private static personservice personservice; 
 @beforeclass 
 public static void setupbeforeclass() throws exception { 
 applicationcontext act=new classpathxmlapplicationcontext("beans.xml"); 
 personservice=(personservice) act.getbean("personservice"); 
 } 
 @test 
 public void save() { 
 personservice.save(new personbean("wyy")); 
 } 
 @test 
 public void update() { 
 personbean person=personservice.getperson(1); 
 person.setname("wy"); 
 personservice.update(person); 
 } 
 @test 
 public void getperson() { 
 personbean person=personservice.getperson(1); 
 system.out.println(person.getname()); 
 } 
 @test 
 public void delete() { 
 personservice.delete(1); 
 } 
}

数据库:

create table 
create table `person` ( 
 `id` int(11) not null auto_increment, 
 `name` varchar(10) not null, 
 primary key (`id`) 
) engine=innodb auto_increment=2 default charset=utf8

希望本文所述对大家java程序设计有所帮助。