程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java開辟之Spring銜接數據庫辦法實例剖析

Java開辟之Spring銜接數據庫辦法實例剖析

編輯:關於JAVA

Java開辟之Spring銜接數據庫辦法實例剖析。本站提示廣大學習愛好者:(Java開辟之Spring銜接數據庫辦法實例剖析)文章只能為提供參考,不一定能成為您想要的結果。以下是Java開辟之Spring銜接數據庫辦法實例剖析正文


本文實例講述了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法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved