Java開發(fā)之Spring連接數(shù)據(jù)庫(kù)方法實(shí)例分析
本文實(shí)例講述了Java開發(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類:
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)過一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
<property name="maxIdle" value="2"/>
<!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請(qǐng)去一些連接,以免洪峰來時(shí)來不及申請(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è)試類:
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ì)有所幫助。
- Java編程中使用JDBC API連接數(shù)據(jù)庫(kù)和創(chuàng)建程序的方法
- java開發(fā)中基于JDBC連接數(shù)據(jù)庫(kù)實(shí)例總結(jié)
- Java程序連接數(shù)據(jù)庫(kù)的常用的類和接口介紹
- 完整java開發(fā)中JDBC連接數(shù)據(jù)庫(kù)代碼和步驟
- Java使用JDBC連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
- java連接數(shù)據(jù)庫(kù)增、刪、改、查工具類
- java使用jdbc連接數(shù)據(jù)庫(kù)工具類和jdbc連接mysql數(shù)據(jù)示例
- 通過代理類實(shí)現(xiàn)java連接數(shù)據(jù)庫(kù)(使用dao層操作數(shù)據(jù))實(shí)例分享
- Java連接數(shù)據(jù)庫(kù)步驟解析(Oracle、MySQL)
相關(guān)文章
SpringBoot構(gòu)造器注入循環(huán)依賴及解決方案
這篇文章主要介紹了SpringBoot構(gòu)造器注入循環(huán)依賴及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程
這篇文章主要介紹了基于Java的電梯系統(tǒng)實(shí)現(xiàn)過程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-10-10
mybatis?報(bào)錯(cuò)顯示sql中有兩個(gè)limit的解決
這篇文章主要介紹了mybatis?報(bào)錯(cuò)顯示sql中有兩個(gè)limit的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Java C++解決在排序數(shù)組中查找數(shù)字出現(xiàn)次數(shù)問題
本文終于介紹了分別通過Java和C++實(shí)現(xiàn)統(tǒng)計(jì)一個(gè)數(shù)字在排序數(shù)組中出現(xiàn)的次數(shù)。文中詳細(xì)介紹了實(shí)現(xiàn)思路,感興趣的小伙伴可以跟隨小編學(xué)習(xí)一下2021-12-12
MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析
這篇文章主要介紹了MyBatis-Plus實(shí)現(xiàn)邏輯刪除功能解析,有時(shí)候并不需要真正的刪除數(shù)據(jù),而是想邏輯刪除,方便數(shù)據(jù)恢復(fù),MyBatis-Plus可以很方便的實(shí)現(xiàn)邏輯刪除的功能,需要的朋友可以參考下2023-11-11
Spring?Data?JPA實(shí)現(xiàn)持久化存儲(chǔ)數(shù)據(jù)到數(shù)據(jù)庫(kù)的示例代碼
Spring Data JPA是Spring基于JPA規(guī)范的基礎(chǔ)上封裝的?套 JPA 應(yīng)?框架,可使開發(fā)者?極簡(jiǎn)的代碼即可實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的訪問和操作。本文我們來了解如何用Spring?Data?JPA框架實(shí)現(xiàn)數(shù)據(jù)持久化存儲(chǔ)到數(shù)據(jù)庫(kù),感興趣的可以了解一下2022-04-04
java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解
這篇文章主要介紹了java ant包中的org.apache.tools.zip實(shí)現(xiàn)壓縮和解壓縮實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-04-04

