Spring(七)
程序员文章站
2023-11-24 22:51:34
Spring JDBC 1.需要的实体类和数据库 2.需要的dao层 3.需要的service层 4.需要的jdbc.properties 5.需要的核心配置文件 6.测试类 未完待续!!! ......
spring jdbc
1.需要的实体类和数据库
2.需要的dao层
1 package com.xdf.dao; 2 3 import com.xdf.bean.student; 4 import org.springframework.jdbc.core.rowmapper; 5 import org.springframework.jdbc.core.support.jdbcdaosupport; 6 7 import java.io.serializable; 8 import java.sql.resultset; 9 import java.sql.sqlexception; 10 import java.util.list; 11 12 /** 13 * dao的实现类 14 * 书写sql 15 * jdbcdaosupport====>basedao 16 * jdbcdaosupport有两个重要的属性 17 * 01.jdbctemplate 模版 18 * 02.需要 datasource ===>数据源 19 * 20 * jdbcdaosupport 所有的增删改 都是update 21 * 查询是query 22 * 23 */ 24 public class studentdaoimpl extends jdbcdaosupport implements studentdao { 25 26 public int add(student student) { 27 string sql="insert into student values(?,?)"; 28 return getjdbctemplate().update(sql,student.getsid(),student.getsname()); 29 } 30 31 public int del(serializable id) { 32 string sql="delete from student where sid=?"; 33 return getjdbctemplate().update(sql,id); 34 } 35 36 public int update(student student) { 37 string sql="update student set sname=? where sid=?"; 38 return getjdbctemplate().update(sql,student.getsname(),student.getsid()); 39 } 40 41 public list<student> getall() { 42 string sql="select * from student"; 43 //使用匿名内部类 44 return getjdbctemplate().query(sql,new rowmapper<student>(){ 45 /** 46 * 这里的resultset是返回一个对象! 47 * 我们之前使用的是 一个结果集! 48 */ 49 public student maprow(resultset rs, int rownum) throws sqlexception { 50 //创建student对象 依次返回 51 student student=new student(); 52 student.setsid(rs.getint("sid")); 53 student.setsname(rs.getstring("sname")); 54 return student; 55 } 56 }); 57 } 58 }
3.需要的service层
1 package com.xdf.service; 2 3 import com.xdf.bean.student; 4 import com.xdf.dao.studentdao; 5 6 import java.io.serializable; 7 import java.util.list; 8 9 public class studentserviceimpl implements studentservice { 10 11 //交给spring容器实例化dao层对象 12 private studentdao dao; 13 14 public studentdao getdao() { 15 return dao; 16 } 17 18 public void setdao(studentdao dao) { 19 this.dao = dao; 20 } 21 22 public int add(student student) { 23 return dao.add(student); 24 } 25 26 public int del(serializable id) { 27 return dao.del(id); 28 } 29 30 public int update(student student) { 31 return dao.update(student); 32 } 33 34 public list<student> getall() { 35 return dao.getall(); 36 } 37 }
4.需要的jdbc.properties
5.需要的核心配置文件
1 <?xml version="1.0" encoding="utf-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:tx="http://www.springframework.org/schema/tx" 7 xmlns:p="http://www.springframework.org/schema/p" 8 xmlns:c="http://www.springframework.org/schema/c" 9 xsi:schemalocation=" 10 http://www.springframework.org/schema/beans 11 http://www.springframework.org/schema/beans/spring-beans.xsd 12 http://www.springframework.org/schema/context 13 http://www.springframework.org/schema/context/spring-context.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd 16 http://www.springframework.org/schema/aop 17 http://www.springframework.org/schema/aop/spring-aop.xsd"> 18 <!--引入需要的jdbc.properties文件--> 19 <context:property-placeholder location="classpath:jdbc.properties"/> 20 21 <!--01.使用spring自带的数据源 22 <bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> 23 <property name="driverclassname" value="${jdbc.driverclassname}"/> 24 <property name="url" value="${jdbc.url}"/> 25 <property name="username" value="${jdbc.username}"/> 26 <property name="password" value="${jdbc.password}"/> 27 </bean>--> 28 29 <!--02.使用c3p0自带的数据源 30 <bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"> 31 <property name="driverclass" value="${jdbc.driverclassname}"/> 32 <property name="jdbcurl" value="${jdbc.url}"/> 33 <property name="user" value="${jdbc.username}"/> 34 <property name="password" value="${jdbc.password}"/> 35 </bean>--> 36 37 38 <!--03.使用dbcp自带的数据源--> 39 <bean id="datasource" class="org.apache.commons.dbcp.basicdatasource"> 40 <property name="driverclassname" value="${jdbc.driverclassname}"/> 41 <property name="url" value="${jdbc.url}"/> 42 <property name="username" value="${jdbc.username}"/> 43 <property name="password" value="${jdbc.password}"/> 44 </bean> 45 <!--配置dao层对象--> 46 <bean id="studentdao" class="com.xdf.dao.studentdaoimpl"> 47 <property name="datasource" ref="datasource"/> 48 </bean> 49 50 <!-- 配置service层对象--> 51 <bean id="studentservice" class="com.xdf.service.studentserviceimpl"> 52 <property name="dao" ref="studentdao"/> 53 </bean> 54 </beans>
6.测试类
未完待续!!!
下一篇: Photoshop制作绚丽大气的彩色海报