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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術(shù)|正則表達(dá)式|

服務(wù)器之家 - 編程語言 - JAVA教程 - 詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫

詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫

2020-07-12 16:04Hongten JAVA教程

本篇文章主要介紹了spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫,具有一定的參考價(jià)值,有興趣的可以了解一下。

本文介紹spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫,具體如下:

項(xiàng)目結(jié)構(gòu):

詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫

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

詳解spring開發(fā)_JDBC操作MySQL數(shù)據(jù)庫

/spring_1100_spring+jdbc/src/com/b510/bean/Person.java

?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.b510.bean;
 
/**
 * 普通的javaBean類Person
 *
 * @author Hongten
 *
 */
public class Person {
 
  /**
   * id號(hào)
*/
  private int id;
  /**
   * 姓名
*/
  private String name;
  /**
   * 年齡
*/
  private int age;
  /**
   * 性別
*/
  private String sex;
 
 
  public Person(int id, String name, int age, String sex) {
    this.id = id;
    this.name = name;
    this.age = age;
    this.sex = sex;
  }
 
  public Person() {
  }
 
  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;
  }
 
  public int getAge() {
    return age;
  }
 
  public void setAge(int age) {
    this.age = age;
  }
 
  public String getSex() {
    return sex;
  }
 
  public void setSex(String sex) {
    this.sex = sex;
  }
 
}

/spring_1100_spring+jdbc/src/com/b510/service/PersonService.java

?
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
44
45
package com.b510.service;
 
import java.util.List;
 
import com.b510.bean.Person;
 
public interface PersonService {
 
  /**
   * 保存Person
   *
   * @param person
*/
  public abstract void save(Person person);
 
  /**
   * 更新Person
   *
   * @param person
*/
  public abstract void update(Person person);
 
  /**
   * 獲取Person
   *
   * @param id
   * @return
*/
  public abstract Person getPerson(Integer id);
 
  /**
   * 獲取所有Person
   *
   * @return
*/
  public abstract List<Person> getPerson();
 
  /**
   * 刪除指定id的Person
   *
   * @param id
*/
  public abstract void delete(Integer id);
 
}

/spring_1100_spring+jdbc/src/com/b510/service/impl/PersonServiceBean.java

?
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.b510.service.impl;
 
import java.util.List;
 
import javax.sql.DataSource;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import com.b510.bean.Person;
import com.b510.service.PersonService;
 
/**
 * 業(yè)務(wù)bean
 *
 * @author Hongten
 *
 */
public class PersonServiceBean implements PersonService {
 
  /**
   * 數(shù)據(jù)源
*/
  private DataSource dataSource;
  /**
   * spring提供的jdbc操作輔助類
*/
  private JdbcTemplate jdbcTemplate;
 
  // 設(shè)置數(shù)據(jù)源
  public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
  }
 
  public void save(Person person) {
    jdbcTemplate.update("insert into person(name,age,sex)values(?,?,?)",
        new Object[] { person.getName(), person.getAge(),
            person.getSex() }, new int[] { java.sql.Types.VARCHAR,
            java.sql.Types.INTEGER, java.sql.Types.VARCHAR });
  }
 
  public void update(Person person) {
    jdbcTemplate.update("update person set name=?,age=?,sex=? where id=?",
        new Object[] { person.getName(), person.getAge(),
            person.getSex(), person.getId() }, new int[] {
            java.sql.Types.VARCHAR, java.sql.Types.INTEGER,
            java.sql.Types.VARCHAR, java.sql.Types.INTEGER });
 
  }
 
  public Person getPerson(Integer id) {
    Person person = (Person) jdbcTemplate.queryForObject(
        "select * from person where id=?", new Object[] { id },
        new int[] { java.sql.Types.INTEGER }, new PersonRowMapper());
    return person;
 
  }
 
  @SuppressWarnings("unchecked")
  public List<Person> getPerson() {
    List<Person> list = jdbcTemplate.query("select * from person", new PersonRowMapper());
    return list;
 
  }
 
  public void delete(Integer id) {
    jdbcTemplate.update("delete from person where id = ?", new Object[] { id },
        new int[] { java.sql.Types.INTEGER });
 
  }
}

/spring_1100_spring+jdbc/src/com/b510/service/impl/PersonRowMapper.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.b510.service.impl;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import org.springframework.jdbc.core.RowMapper;
 
import com.b510.bean.Person;
 
public class PersonRowMapper implements RowMapper {
 
  @Override
  public Object mapRow(ResultSet set, int index) throws SQLException {
    Person person = new Person(set.getInt("id"), set.getString("name"), set
        .getInt("age"), set.getString("sex"));
    return person;
  }
 
}

/spring_1100_spring+jdbc/src/com/b510/test/SpringJDBCTest.java

?
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
44
45
46
47
48
49
package com.b510.test;
 
import java.util.List;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.b510.bean.Person;
import com.b510.service.PersonService;
 
public class SpringJDBCTest {
 
  public static void main(String[] args) {
    ApplicationContext act = new ClassPathXmlApplicationContext("bean.xml");
 
    PersonService personService = (PersonService) act
        .getBean("personService");
 
    Person person = new Person();
    person.setName("蘇東坡");
    person.setAge(21);
    person.setSex("男");
 
    // 保存一條記錄
    personService.save(person);
 
    List<Person> person1 = personService.getPerson();
    System.out.println("++++++++得到所有Person");
    for (Person person2 : person1) {
      System.out.println(person2.getId() + " " + person2.getName()
          + "  " + person2.getAge() + " " + person2.getSex());
    }
    Person updatePerson = new Person();
    updatePerson.setName("Divide");
    updatePerson.setAge(20);
    updatePerson.setSex("男");
    updatePerson.setId(5);
    // 更新一條記錄
    personService.update(updatePerson);
    System.out.println("******************");
 
    // 獲取一條記錄
    Person onePerson = personService.getPerson(2);
    System.out.println(onePerson.getId() + " " + onePerson.getName()
        + " " + onePerson.getAge() + " " + onePerson.getSex());
    // 刪除一條記錄
    personService.delete(1);
  }
}

/spring_1100_spring+jdbc/src/bean.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
37
38
39
40
41
42
43
44
45
46
47
48
<?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">
  <!--配置數(shù)據(jù)源 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="org.gjt.mm.mysql.Driver" />
    <property name="url"
      value="jdbc:mysql://localhost:3307/spring?useUnicode=true&amp;characterEncoding=UTF-8" />
    <property name="username" value="root" />
    <property name="password" value="root" />
    <!-- 連接池啟動(dòng)時(shí)的初始值 -->
    <property name="initialSize" value="1" />
    <!-- 連接池的最大值 -->
    <property name="maxActive" value="300" />
    <!-- 最大空閑值.當(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>
  <!--
    采用注解方式來配置事務(wù)。針對(duì)數(shù)據(jù)源的事務(wù)管理器
    ,把我們定義的數(shù)據(jù)源注入到DataSourceTransactionManager類的屬性dataSource中
  -->
  <bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--
    引入命名空間: 1.xmlns:tx="http://www.springframework.org/schema/tx
    2.http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    采用@Transaction注解方式使用事務(wù)管理器
  -->
  <tx:annotation-driven transaction-manager="txManager" />
 
  <!-- 配置業(yè)務(wù)bean:PersonServiceBean -->
  <bean id="personService" class="com.b510.service.impl.PersonServiceBean">
    <!-- 向?qū)傩詃ataSource注入數(shù)據(jù)源 -->
    <property name="dataSource" ref="dataSource"></property>
  </bean>
</beans>

運(yùn)行結(jié)果;

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2012-3-9 23:30:57 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1a05308: display name [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]; startup date [Fri Mar 09 23:30:57 CST 2012]; root of context hierarchy
2012-3-9 23:30:57 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
2012-3-9 23:30:58 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@1a05308]: org.springframework.beans.factory.support.DefaultListableBeanFactory@2bb514
2012-3-9 23:30:58 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2bb514: defining beans [dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,personService]; root of factory hierarchy
++++++++得到所有Person
2 TomCat  12 女
3 hongten  21 男
4 liufang  21 女
5 Divide  20 男
6 Jone  20 女
7 蘇東坡  21 男
******************
2 TomCat 12 女

當(dāng)然我們可以用配置文件來存放我們的數(shù)據(jù)源信息:

/spring_1100_spring+jdbc/src/jdbc.properties

?
1
2
3
4
5
6
7
8
driverClassName=org.gjt.mm.mysql.Driver
url=jdbc\:mysql\://localhost\:3307/spring?useUnicode\=true&characterEncoding\=UTF-8
username=root
password=root
initialSize=1
maxActive=300
maxIdle=2
minIdle=1

相應(yīng)要修改:

/spring_1100_spring+jdbc/src/bean.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?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">
 
  <!-- 讀取jdbc.properties配置文件 -->
  <context:property-placeholder location="classpath:jdbc.properties" />
  <!--配置數(shù)據(jù)源 -->
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
    destroy-method="close">
    <property name="driverClassName" value="${driverClassName}" />
    <property name="url" value="${url}" />
    <property name="username" value="${username}" />
    <property name="password" value="${password}" />
    <!-- 連接池啟動(dòng)時(shí)的初始值 -->
    <property name="initialSize" value="${initialSize}" />
    <!-- 連接池的最大值 -->
    <property name="maxActive" value="${maxActive}" />
    <!-- 最大空閑值.當(dāng)經(jīng)過一個(gè)高峰時(shí)間后,連接池可以慢慢將已經(jīng)用不到的連接慢慢釋放一部分,一直減少到maxIdle為止 -->
    <property name="maxIdle" value="${maxIdle}" />
    <!-- 最小空閑值.當(dāng)空閑的連接數(shù)少于閥值時(shí),連接池就會(huì)預(yù)申請(qǐng)去一些連接,以免洪峰來時(shí)來不及申請(qǐng) -->
    <property name="minIdle" value="${minIdle}" />
  </bean>
  <!--
    采用注解方式來配置事務(wù)。針對(duì)數(shù)據(jù)源的事務(wù)管理器
    ,把我們定義的數(shù)據(jù)源注入到DataSourceTransactionManager類的屬性dataSource中
  -->
  <bean id="txManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
  </bean>
  <!--
    引入命名空間: 1.xmlns:tx="http://www.springframework.org/schema/tx
    2.http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
    采用@Transaction注解方式使用事務(wù)管理器
  -->
  <tx:annotation-driven transaction-manager="txManager" />
 
  <!-- 配置業(yè)務(wù)bean:PersonServiceBean -->
  <bean id="personService" class="com.b510.service.impl.PersonServiceBean">
    <!-- 向?qū)傩詃ataSource注入數(shù)據(jù)源 -->
    <property name="dataSource" ref="dataSource"></property>
  </bean>
</beans>

運(yùn)行結(jié)果是相同的:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@c1b531: display name [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]; startup date [Sat Mar 10 00:23:59 CST 2012]; root of context hierarchy
2012-3-10 0:23:59 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
2012-3-10 0:23:59 org.springframework.context.support.AbstractApplicationContext obtainFreshBeanFactory
信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1b531]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1aa57fb
2012-3-10 0:23:59 org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
信息: Loading properties file from class path resource [jdbc.properties]
2012-3-10 0:23:59 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1aa57fb: defining beans [org.springframework.beans.factory.config.PropertyPlaceholderConfigurer#0,dataSource,txManager,org.springframework.aop.config.internalAutoProxyCreator,org.springframework.transaction.annotation.AnnotationTransactionAttributeSource#0,org.springframework.transaction.interceptor.TransactionInterceptor#0,org.springframework.transaction.config.internalTransactionAdvisor,personService]; root of factory hierarchy
++++++++得到所有Person
2 TomCat  12 女
3 hongten  21 男
4 liufang  21 女
5 Divide  20 男
6 Jone  20 女
7 蘇東坡  21 男
8 蘇東坡  21 男
******************
2 TomCat 12 女

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/hongten/archive/2012/03/09/java_spring_jdbc.html

延伸 · 閱讀

精彩推薦
  • JAVA教程JavaWeb工程中集成YMP框架快速上手(二)

    JavaWeb工程中集成YMP框架快速上手(二)

    YMP是一個(gè)非常簡(jiǎn)單、易用的一套輕量級(jí)JAVA應(yīng)用開發(fā)框架,設(shè)計(jì)原則主要側(cè)重于簡(jiǎn)化工作任務(wù)、規(guī)范開發(fā)流程、提高開發(fā)效率。對(duì)YMP框架感興趣的小伙伴們可...

    有理想的魚1672020-04-02
  • JAVA教程淺談FileItem類的常用方法

    淺談FileItem類的常用方法

    下面小編就為大家?guī)硪黄獪\談FileItem類的常用方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧 ...

    jingxian4812020-06-03
  • JAVA教程sqlserver的jdbc配置方法

    sqlserver的jdbc配置方法

    這篇文章主要介紹了sqlserver的jdbc配置方法,需要的朋友可以參考下 ...

    Java教程網(wǎng)4592019-11-21
  • JAVA教程Java生成非對(duì)稱型加密公鑰和私鑰的方法

    Java生成非對(duì)稱型加密公鑰和私鑰的方法

    這篇文章主要介紹了Java生成非對(duì)稱型加密公鑰和私鑰的方法,涉及java非對(duì)稱加密的原理與實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下 ...

    鑒客4892019-12-27
  • JAVA教程java制作廣告圖片自動(dòng)輪播控件

    java制作廣告圖片自動(dòng)輪播控件

    本文給大家分享了2款java實(shí)現(xiàn)的首頁廣告圖片自動(dòng)輪播的控件,分別是PC端和移動(dòng)端的,效果非常不錯(cuò),有需要的小伙伴可以參考下。 ...

    hebedich3222020-01-13
  • JAVA教程java多線程編程之為什么要進(jìn)行數(shù)據(jù)同步

    java多線程編程之為什么要進(jìn)行數(shù)據(jù)同步

    數(shù)據(jù)同步就是指在同一時(shí)間,只能由一個(gè)線程來訪問被同步的類變量,當(dāng)前線程訪問完這些變量后,其他線程才能繼續(xù)訪問,下面看一下為什么要進(jìn)行數(shù)據(jù)...

    java技術(shù)網(wǎng)3592019-11-04
  • JAVA教程Java+Nginx實(shí)現(xiàn)POP、IMAP、SMTP郵箱代理服務(wù)

    Java+Nginx實(shí)現(xiàn)POP、IMAP、SMTP郵箱代理服務(wù)

    本篇文章的內(nèi)容是介紹Java+Nginx如何實(shí)現(xiàn)POP、IMAP、SMTP郵箱代理服務(wù),步驟詳細(xì),思路清新,需要的朋友可以參考下 ...

    高文龍1792019-12-28
  • JAVA教程java開發(fā)微信公眾號(hào)支付

    java開發(fā)微信公眾號(hào)支付

    這篇文章主要給大家結(jié)合微信支付接口開發(fā)的實(shí)踐,從獲取用戶授權(quán)到各主要接口的使用方法等方面介紹微信支付的關(guān)鍵點(diǎn)技術(shù),有需要的小伙伴可以參考下...

    hebedich3272020-01-02
主站蜘蛛池模板: 性直播免费 | www.久久精品视频 | 免费视频完整版在线观看网站 | 欧美日韩亚洲一区二区三区在线观看 | 茄子香蕉视频 | 亚洲人影院 | 国产在线观看91精品一区 | 强行扒开美女大腿挺进 | 男神插曲女生动漫完整版动漫 | 2021久久| 亚洲精品免费视频 | 国产精品思瑞在线观看 | 甜宠巨肉h文1v1校园 | 青青青在线免费 | 99久久精品免费看国产一区 | 色图18p| 久久久91精品国产一区二区 | 成人中文字幕在线高清 | 免费精品国产在线观看 | 久久最新地址获取 | 国产成人免费片在线观看 | 日产精品一卡2卡三卡4乱码久久 | 欧美一级片在线免费观看 | 精品在线视频一区 | 秋霞午夜伦午夜高清福利片 | 午夜影院和视费x看 | 久久久无码精品亚洲A片猫咪 | 窝窝午夜理伦影院 | 日本一区免费观看 | 国产成人久久精品推最新 | 国产色司机在线视频免费观看 | 亚洲va久久久噜噜噜久久狠狠 | 国产3344视频在线观看免费 | 暖暖高清日本在线 | 91香蕉小视频 | 动漫美女胸被狂揉扒开吃奶动态图 | 亚洲AV午夜精品麻豆AV | 美女私人影院 | 好紧好爽的午夜寂寞视频 | 女人c交zzzooo在线观看 | 精品美女国产互换人妻 |