内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

怎么在spring中通過jdbc連接數(shù)據(jù)庫

這篇文章將為大家詳細(xì)講解有關(guān)怎么在spring中通過jdbc連接數(shù)據(jù)庫,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

創(chuàng)新互聯(lián)公司堅持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時代的安平網(wǎng)站設(shè)計、移動媒體設(shè)計的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!

首先看下整個工程的架構(gòu)目錄:

怎么在spring中通過jdbc連接數(shù)據(jù)庫

需要的jar包:

怎么在spring中通過jdbc連接數(shù)據(jù)庫

一、建表

create table student(
 id int primary key auto_increment,
 name varchar(32),
 age int,
 phone varchar(32)
);

二、新建與數(shù)據(jù)庫對應(yīng)JavaBean

package com.etoak.bean;
 
public class Student {
 /**
 * 一個標(biāo)準(zhǔn)的javaBean對象 : 
 *  表字段對應(yīng)的屬性
 *  屬性對應(yīng)的getter、setter方法
 *  無參構(gòu)造器
 *  除id[主鍵]之外其他參數(shù)組成的構(gòu)造器
 *  所有參數(shù)組成的構(gòu)造器
 */
 
 private Integer id;
 private String name;
 private Integer age;
 private String phone;
 public Student() {
 super();
 }
 public Student(String name, Integer age, String phone) {
 super();
 this.name = name;
 this.age = age;
 this.phone = phone;
 }
 public Student(Integer id, String name, Integer age, String phone) {
 super();
 this.id = id;
 this.name = name;
 this.age = age;
 this.phone = phone;
 }
 public Integer getId() {
 return id;
 }
 public void setId(Integer id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public Integer getAge() {
 return age;
 }
 public void setAge(Integer age) {
 this.age = age;
 }
 public String getPhone() {
 return phone;
 }
 public void setPhone(String phone) {
 this.phone = phone;
 }
}

三、spring的applicationContext配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
 <!-- 
 使用spring提供的整合jdbc功能
 需要導(dǎo)入DAO層提供的兩個jar包[spring-jdbc spring-tx]
 
 通過ioc依賴注入 將JdbcTemplate注入給StuDaoImpl
 -->
 <bean id="dao" class="com.etoak.dao.StuDaoImpl">
 <!-- 
  name="jt"  setJt(JdbcTemplate jt)
  ref="jt"  id="jt"
  自定義對象 ref=""
  -->
 <property name="jt" ref="jt"></property>
 </bean>
 
 <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="ds"></property>
 </bean>
 <!-- 
 此時的JdbcTemplate還不具備數(shù)據(jù)庫連接能力
 為了讓其具備數(shù)據(jù)庫連接能力,需要為其提供DataSource 連接池、數(shù)據(jù)源
  setDataSource(DataSource ds)
 需要在ioc容器中再配置一個DataSource對象:
  driverClassName
  url
  username
  password
  maxIdle
  maxActive
  maxWait
 
 DataSource 接口
  1 實現(xiàn)類  
  BasicDataSource  commons-dbcp.jar
  spring框架自帶了DataSource實現(xiàn)類
  DriverManagerDataSource
   setDriverClassName(String driver)
   setUrl(String url)
   setUsername(String u)
   setPassword(String p)
  [
   ref屬性 : 表示調(diào)用該方法需要注入的數(shù)據(jù)類型 : 自定義類型/引用類型
   value屬性 : 表示調(diào)用該方法需要注入的數(shù)據(jù)類型 : 基本數(shù)據(jù)類型/String類型/Class類型
  ] 
  2 工廠bean
 -->
 <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
 <property name="driverClassName" value="com.MySQL.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://localhost:3306/etoak"/>
 <property name="username" value="root"/>
 <property name="password" value="root"/>
 </bean>
 
</beans>

四、編寫Dao

package com.etoak.dao;
 
import java.util.List;
import java.util.Map;
 
import org.springframework.jdbc.core.JdbcTemplate;
 
import com.etoak.bean.Student;
 
/**
 * 使用jdbc方式對student表數(shù)據(jù)進(jìn)行CRUD操作
 *  1 傳統(tǒng)的jdbc開發(fā)方式 [ConFactory ...]
 *  2 spring提供的整合方案  JdbcTemplate
 */
public class StuDaoImpl {
 
 private JdbcTemplate jt;
 public void setJt(JdbcTemplate jt) {
 this.jt = jt;
 }
 
 /**
 * JdbcTemplate將連接數(shù)據(jù)庫執(zhí)行添加操作的流程封裝在其update(sql)
 */
 public boolean addStu(Student stu){
 String sql = "insert into student values(null,?,?,?)";
 Object[] args = {stu.getName() , stu.getAge() , stu.getPhone()};
 int result = jt.update(sql , args);
 // result 執(zhí)行當(dāng)前操作影響的數(shù)據(jù)量
 return result==1;
 }
 
 public boolean delStuById(Integer id){
 String sql = "delete from student where id="+id;
 return jt.update(sql)==1;
 }
 public boolean updateStu(Student stu){
 String sql = "update student set name=?,age=?,phone=? where id=?";
 Object[] args = {stu.getName() , stu.getAge() , stu.getPhone() , stu.getId()};
 return jt.update(sql , args)==1;
 }
 
 /**
 * jt.queryForMap(sql) - Map 
 *  Jdbc不是ORM工具,不知道sql查詢的對應(yīng)哪個對象
 *  只能將查詢出的關(guān)系型數(shù)據(jù)封裝在一個Map集合中返回
 *  {字段名=字段值,...}
 *  map.get("id/name/age/phone") 
 * 注意 : 
 *  在使用queryForMap(sql)查詢單條數(shù)據(jù)時
 *  必須能夠確保根據(jù)傳入的sql語句能夠并且只能查詢出單條數(shù)據(jù)
 *  否則使用該方法會拋出異常
 */
 public Map selStuById(Integer id){
 String sql = "select * from student where id="+id;
 Map map = jt.queryForMap(sql);
 return map;
 }
 
 // List<Map> 每一個student被封裝成了一個Map對象
 public List selectAllStus(){
 String sql = "select * from student";
 return jt.queryForList(sql);
 }
 
 public int selectStuCount(){
 String sql = "select count(*) from student";
 return jt.queryForInt(sql);
 }
 
 public List selectStusByPage(int start , int max){
 String sql = "select * from student limit ?,?";
 Object[] args = {start , max};
 return jt.queryForList(sql , args);
 }
}

五、測試

package com.etoak.test;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
import com.etoak.bean.Student;
import com.etoak.dao.StuDaoImpl;
 
public class Test {
 
 public static void main(String[] args) {
 ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
 StuDaoImpl dao = (StuDaoImpl)ac.getBean("dao");
 Student stu = new Student("sheldon",30,"111");
 boolean flag = dao.addStu(stu);
 System.out.println(flag);
 }
 
}

關(guān)于怎么在spring中通過jdbc連接數(shù)據(jù)庫就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

當(dāng)前文章:怎么在spring中通過jdbc連接數(shù)據(jù)庫
網(wǎng)站路徑:http://www.rwnh.cn/article44/ghccee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、Google自適應(yīng)網(wǎng)站、標(biāo)簽優(yōu)化網(wǎng)站排名、品牌網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化
石台县| 广河县| 望谟县| 黄梅县| 静安区| 桦南县| 尼木县| 南宁市| 甘孜县| 固始县| 新兴县| 汉阴县| 靖江市| 霸州市| 湖北省| 新邵县| 沙坪坝区| 济南市| 平山县| 揭西县| 武定县| 东乌珠穆沁旗| 崇州市| 铜陵市| 台北市| 玉树县| 安吉县| 特克斯县| 公主岭市| 定日县| 巴林右旗| 楚雄市| 瑞昌市| 长治县| 紫金县| 洛扎县| 仁怀市| 塔城市| 芦溪县| 瓦房店市| 深水埗区|