spring通过jdbc连接数据库

网友投稿 213 2022-12-30


spring通过jdbc连接数据库

本文实例为大家分享了spring通过jdbc连接数据库的具体代码,供大家参考,具体内容如下

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

需要的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配置文件

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd">

xmlns:context="http://springframework.org/schema/context"

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.2.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.2.xsd">

四、编写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(http://) , 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 每一个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);

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:物业管理系统接口设计方案(小区物业管理系统的设计)
下一篇:前后端接口测试工具(前后端接口测试工具)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~