> 文档中心 > Java EE---通过Spring JDBC实现数据库的增、删、改、查

Java EE---通过Spring JDBC实现数据库的增、删、改、查

Spring JDBC之update、query方法

  • 1、项目清单
  • 2、全部代码
    • 2.1、student
    • 2.2、stuDao
    • 2.3、applicationContext.xml
    • 2.4、test
  • 3、结果
  • 4、笔记
  • 5、源码下载

1、项目清单

在这里插入图片描述
所用jar包:
在这里插入图片描述

2、全部代码

2.1、student

package spring.jdbc.entity;public class student {//属性与数据库的student表的列名一样String Sno;String Sname;String Sex;String Sbrithday;String Sclass;//设置了有参构造方法,系统的默认的无参构造方法被覆盖,需自己手动建立一个无参构造方法以便在其他地方实例化对象public student() {}//设置有参构造方法,以便test中直接给对象赋值public student(String a,String b,String c,String d,String e) {Sno=a;Sname=b;Sex=c;Sbrithday=d;Sclass=e;}public String getSno() {return Sno;}public void setSno(String sno) {Sno = sno;}public String getSname() {return Sname;}public void setSname(String sname) {Sname = sname;}public String getSex() {return Sex;}public void setSex(String sex) {Sex = sex;}public String getSbrithday() {return Sbrithday;}public void setSbrithday(String sbrithday) {Sbrithday = sbrithday;}public String getSclass() {return Sclass;}public void setSclass(String sclass) {Sclass = sclass;}public void tostring() {System.out.println(Sno+" "+Sname+" "+Sex+" "+Sbrithday+" "+Sclass);}}

2.2、stuDao

package spring.jdbc.dao;import java.util.List;import java.util.Map;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.BeanPropertyRowMapper;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Repository;import com.mysql.cj.result.StringValueFactory;import spring.jdbc.entity.student;@Repository("stuDao")public class stuDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public int addstudent(student s) {String sql="insert into student(Sno,Sname,Sex,Sbirthday,Sclass) values(?,?,?,?,?)";//将参数s的键值存到数组对象中Object[] obj=new Object[] {s.getSno(),s.getSname(),s.getSex(),s.getSbrithday(),s.getSclass(),};//执行添加操作,update方法返回的是操作数据库后受影响的行数int num=this.jdbcTemplate.update(sql, obj);return num;}public int updateNamebySno(student s) {String sql="update student set Sname=? where Sno=?";Object[] obj =new Object[] {s.getSname(),s.getSno(),};int num =this.jdbcTemplate.update(sql, obj);return num;}public int deletestudentbySno(String sno) {String sql="delete from student  where Sno=?";int num =this.jdbcTemplate.update(sql, sno);return num;}public List<student> findAllstudent(){String sql="select * from student";RowMapper<student> rowMapper=new BeanPropertyRowMapper<student>(student.class);return this.jdbcTemplate.query(sql, rowMapper);}public String findNameBySno(String sno) {String sql="select Sname from student where Sno=?";String name=this.jdbcTemplate.queryForObject(sql, new Object[] {sno}, String.class);return name;}public int findNumbers() {String sql="select COUNT(*) from student";int total =this.jdbcTemplate.queryForObject(sql, Integer.class);return total;}public List<Map<String, Object>> findScBySno(String sno){String sql="select sc.Sno,Sname,Cname,grade from student,sc,course where student.sno=sc.sno and course.cno=sc.cno"+" and sc.sno=?";return this.jdbcTemplate.queryForList(sql, new Object[] {sno});}}

2.3、applicationContext.xml

<?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:aop="http://www.springframework.org/schema/aop"  xmlns:context="http://www.springframework.org/schema/context"  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd  http://www.springframework.org/schema/aop   http://www.springframework.org/schema/aop/spring-aop.xsd  http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context.xsd">    <!-- 配置数据源 --><bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/spring"></property><property name="username" value="root"></property><property name="password" value="root"></property></bean> <!-- 配置jdbc模板 --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 扫描包,使注解生效 --> <context:component-scan base-package="spring.jdbc"></context:component-scan></beans>

2.4、test

package spring.jdbc.test;import java.util.List;import java.util.Map;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import spring.jdbc.dao.stuDao;import spring.jdbc.entity.student;public class test {public static void main(String[] args) {ApplicationContext context=new FileSystemXmlApplicationContext("src/applicationContext.xml");student s=new student("027","猪猪侠","女","2001年2月16日","19软件本科3班");student s2=new student("027","猪猪猪侠","女","2001年2月16日","19软件本科3班");stuDao sd=(stuDao) context.getBean("stuDao");//System.out.println("添加学生信息受影响行数:"+sd.addstudent(s));//System.out.println("修改学生信息受影响行数:"+sd.updateNamebySno(s2));//System.out.println("删除学生信息受影响行数:"+sd.deletestudentbySno("001"));//System.out.println("查询所有学生记录");//List list=sd.findAllstudent();//for(student st:list) st.tostring();//System.out.println("查询025学生记录="+sd.findNameBySno("025"));//System.out.println("学生人数="+sd.findNumbers());List<Map<String, Object>> list2=sd.findScBySno("005");for(String k:list2.get(0).keySet()) System.out.print(k+" "); System.out.println();for(Map<String, Object> m:list2) {for(String k:m.keySet())  System.out.print(m.get(k)+" ");System.out.println();}}}

3、结果

4、笔记

  • 增、删、改所使用的都是update()方法,所返回的都是受影响的行数。
  • 对于增、删、改需要更改多个数据的操作,将其封装在一个数组当中再赋值。
  • query方法则是用于处理数据库表的各种查询操作。
  • public List findAllstudent方法中查找的是学生对象,所返回的是多个学生对象,所以返回值是List,query方法执行sql语句并通过参数rowMapper返回一个list数据类型的结果。
  • queryForObject(sql, new Object[] {sno}, String.class);方法中new Object[] {sno}是负责传递实参,String.class是指明该方法的返回类型为String类型。与之对应的有queryForObject(sql, Integer.class)方法,该方法没有实参传递,Integer.class是指该方法返回的是int数据类型。
  • public List<Map> findScBySno(String sno)方法返回的是List<Map>,它是指返回以键值对形式的列表。与之对应使用的queryForList方法返回的也是list类型的数据。

5、源码下载

源代码下载点击此处