spring通过jdbc连接数据库

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

spring通过jdbc连接数据库

  2021-04-03 我要评论

首先看下整个工程的架构目录:

需要的jar包:

一、建表

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

二、新建与数据库对应JavaBean

package com.etoak.bean;
 
public class Student {
 /**
 * 一个标准的javaBean对象 : 
 *  表字段对应的属性
 *  属性对应的getter、setter方法
 *  无参构造器
 *  除id[主键]之外其他参数组成的构造器
 *  所有参数组成的构造器
 */
 
 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功能
 需要导入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还不具备数据库连接能力
 为了让其具备数据库连接能力,需要为其提供DataSource 连接池、数据源
  setDataSource(DataSource ds)
 需要在ioc容器中再配置一个DataSource对象:
  driverClassName
  url
  username
  password
  maxIdle
  maxActive
  maxWait
 
 DataSource 接口
  1 实现类  
  BasicDataSource  commons-dbcp.jar
  spring框架自带了DataSource实现类
  DriverManagerDataSource
   setDriverClassName(String driver)
   setUrl(String url)
   setUsername(String u)
   setPassword(String p)
  [
   ref属性 : 表示调用该方法需要注入的数据类型 : 自定义类型/引用类型
   value属性 : 表示调用该方法需要注入的数据类型 : 基本数据类型/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表数据进行CRUD操作
 *  1 传统的jdbc开发方式 [ConFactory ...]
 *  2 spring提供的整合方案  JdbcTemplate
 */
public class StuDaoImpl {
 
 private JdbcTemplate jt;
 public void setJt(JdbcTemplate jt) {
 this.jt = jt;
 }
 
 /**
 * JdbcTemplate将连接数据库执行添加操作的流程封装在其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 执行当前操作影响的数据量
 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查询的对应哪个对象
 *  只能将查询出的关系型数据封装在一个Map集合中返回
 *  {字段名=字段值,...}
 *  map.get("id/name/age/phone") 
 * 注意 : 
 *  在使用queryForMap(sql)查询单条数据时
 *  必须能够确保根据传入的sql语句能够并且只能查询出单条数据
 *  否则使用该方法会抛出异常
 */
 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);
 }
 
}
您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们