亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java開(kāi)發(fā)之Spring連接數(shù)據(jù)庫(kù)方法實(shí)例分析

 更新時(shí)間:2015年10月19日 14:59:18   作者:煙大洋仔  
這篇文章主要介紹了Java開(kāi)發(fā)之Spring連接數(shù)據(jù)庫(kù)方法,以實(shí)例形式較為詳細(xì)的分析了Java Spring開(kāi)發(fā)中針對(duì)數(shù)據(jù)庫(kù)的相關(guān)操作技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了Java開(kāi)發(fā)之Spring連接數(shù)據(jù)庫(kù)方法。分享給大家供大家參考,具體如下:
接口:

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類(lèi):

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; 
 } 
}

接口實(shí)現(xiàn):

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池啟動(dòng)時(shí)的初始值 --> 
     <property name="password" value="123"/> 
     <!-- 連接name="initialSize" value="${initialSize}"/>--> 
     <property name="initialSize" value="1"/> 
     <!-- 連接池的最大值 --> 
     <property name="maxActive" value="500"/> 
     <!-- 最大空閑值.當(dāng)經(jīng)過(guò)一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 --> 
     <property name="maxIdle" value="2"/> 
     <!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請(qǐng)去一些連接,以免洪峰來(lái)時(shí)來(lái)不及申請(qǐng) --> 
     <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> 

測(cè)試類(lèi):

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); 
 } 
}

數(shù)據(jù)庫(kù):

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

希望本文所述對(duì)大家Java程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置

    Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置

    這篇文章主要介紹了Springboot Thymeleaf實(shí)現(xiàn)HTML屬性設(shè)置,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2007-11-11
  • Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor)

    Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor)

    mybatis中sqlsession下的四大對(duì)象是指:executor, statementHandler,parameterHandler,resultHandler對(duì)象。這篇文章主要介紹了Mybatis中SqlSession下的四大對(duì)象之執(zhí)行器(executor),需要的朋友可以參考下
    2019-04-04
  • SpringBoot實(shí)現(xiàn)自定義線(xiàn)程池的方法

    SpringBoot實(shí)現(xiàn)自定義線(xiàn)程池的方法

    這篇文章主要介紹了SpringBoot中的自定義線(xiàn)程池解析,實(shí)現(xiàn)自定義線(xiàn)程池重寫(xiě)spring默認(rèn)線(xiàn)程池的方式使用的時(shí)候,只需要加@Async注解就可以,不用去聲明線(xiàn)程池類(lèi),需要的朋友可以參考下
    2023-11-11
  • Java中的ReentrantLock使用解析

    Java中的ReentrantLock使用解析

    這篇文章主要介紹了Java中的ReentrantLock使用解析,ReentrandLock即可重入鎖,可重入鎖解決的是重入鎖定的問(wèn)題,重入鎖定指的是當(dāng)一個(gè)線(xiàn)程執(zhí)行邏輯時(shí),需要兩次獲取鎖,而該鎖不可重入就會(huì)導(dǎo)致內(nèi)部嵌套無(wú)法獲取鎖導(dǎo)致Reentrance Lockout發(fā)生,需要的朋友可以參考下
    2023-11-11
  • MyBatis-Plus常見(jiàn)面試題和答案大全

    MyBatis-Plus常見(jiàn)面試題和答案大全

    Mybatis-Plus是一個(gè)基于Mybatis的增強(qiáng)工具,它簡(jiǎn)化了Mybatis的開(kāi)發(fā)流程,提供了許多實(shí)用的功能,如自動(dòng)生成代碼、分頁(yè)查詢(xún)、條件構(gòu)造器、性能分析等,這篇文章主要給大家介紹了關(guān)于MyBatis-Plus常見(jiàn)面試題和答案的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Java Red5服務(wù)器實(shí)現(xiàn)流媒體視頻播放

    Java Red5服務(wù)器實(shí)現(xiàn)流媒體視頻播放

    這篇文章主要介紹了Java Red5服務(wù)器實(shí)現(xiàn)流媒體視頻播放,對(duì)視頻播放感興趣的同學(xué),可以參考下
    2021-04-04
  • Java客戶(hù)端服務(wù)端上傳接收文件實(shí)現(xiàn)詳解

    Java客戶(hù)端服務(wù)端上傳接收文件實(shí)現(xiàn)詳解

    這篇文章主要介紹了Java客戶(hù)端服務(wù)端上傳接收文件實(shí)現(xiàn)詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-07-07
  • Java Spring Cloud Bus 實(shí)現(xiàn)配置實(shí)時(shí)更新詳解

    Java Spring Cloud Bus 實(shí)現(xiàn)配置實(shí)時(shí)更新詳解

    這篇文章主要介紹了SpringCloud Bus如何實(shí)現(xiàn)配置刷新,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2021-09-09
  • Java如何獲取對(duì)象屬性及對(duì)應(yīng)值

    Java如何獲取對(duì)象屬性及對(duì)應(yīng)值

    這篇文章主要介紹了Java如何獲取對(duì)象屬性及對(duì)應(yīng)值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • SpringBoot MockMvc單元測(cè)試的示例代碼

    SpringBoot MockMvc單元測(cè)試的示例代碼

    這篇文章主要介紹了SpringBoot MockMvc單元測(cè)試的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-11-11

最新評(píng)論