一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java開發之Spring連接數據庫方法實例分析

Java開發之Spring連接數據庫方法實例分析

2020-01-10 16:26煙大洋仔 JAVA教程

這篇文章主要介紹了Java開發之Spring連接數據庫方法,以實例形式較為詳細的分析了Java Spring開發中針對數據庫的相關操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了Java開發之Spring連接數據庫方法。分享給大家供大家參考,具體如下:
接口:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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;
 }
}

接口實現:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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配置

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?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>

測試類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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);
 }
}

數據庫:

?
1
2
3
4
5
6
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程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本道色综合久久影院 | 办公室里被迫高h | 成人精品视频 成人影院 | 亚洲 综合 欧美在线视频 | 午夜私人影院在线观看 视频 | 国产福利不卡 | 国产福利视频一区二区微拍 | 暗卫调教女主肉高h | 色多多在线观看视频 | 亚洲乱码尤物193yw在线播放 | 免费网站看v片在线成人国产系列 | 国外成品精品1688 | 午夜久久免费视频 | 草莓香蕉绿巨人丝瓜榴莲18 | 麻豆视频入口 | 欧美va免费精品高清在线 | 4438全国最大成人网视频 | 欧洲vodafone精品性 | 互换身体全集免费观看 | girlfriend动漫在线播放 | 十大免费批日的软件 | 99re在线视频免费观看 | 国产精品久久久久久久久免费 | 亚洲精品久久玖玖玖玖 | 91亚洲一区二区在线观看不卡 | avtt天堂在线 | 国产日产欧产精品精品软件 | 男人边吃奶边做好爽视频免费 | 变态女王麻麻小说在线阅读 | 高h文恩好大好爽 | 精品国产乱码久久久久久免费流畅 | 精品一久久香蕉国产二月 | 九九热视频免费 | 国产美女极品免费视频 | 国产va免费精品高清在线 | 激情图片 激情小说 | 国产日韩精品一区二区三区 | 欧美亚洲高清日韩成人 | 91亚洲精品第一综合不卡播放 | 国产欧美日韩精品一区二区三区 | 国产精品成人亚洲 |