package com.csdn.student.bean;
/**
* Student entity. @author MyEclipse Persistence Tools
*/
public class Student implements java.io.Serializable {
/**
* SRRID
*/
private static final long serialVersionUID = -5076741984769526094L;
// Fields
private String stuid;
private String stuname;
private String stupwd;
private String createtime;
// Constructors
/** default constructor */
public Student() {
}
/** minimal constructor */
public Student(String stuid) {
this.stuid = stuid;
}
/** full constructor */
public Student(String stuid, String stuname, String stupwd,
String createtime) {
this.stuid = stuid;
this.stuname = stuname;
this.stupwd = stupwd;
this.createtime = createtime;
}
// Property accessors
public String getStuid() {
return this.stuid;
}
public void setStuid(String stuid) {
this.stuid = stuid;
}
public String getStuname() {
return this.stuname;
}
public void setStuname(String stuname) {
this.stuname = stuname;
}
public String getStupwd() {
return this.stupwd;
}
public void setStupwd(String stupwd) {
this.stupwd = stupwd;
}
public String getCreatetime() {
return this.createtime;
}
public void setCreatetime(String createtime) {
this.createtime = createtime;
}
}
//
package com.csdn.student.dao;
public interface StudentDao {
public int insertStudent(String sql);
}
package com.csdn.student.dao;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class StudentDaoImpl implements StudentDao{
@Resource
private JdbcTemplate jdbcTemplate;
@Override
public int insertStudent(String sql) {
return jdbcTemplate.update(sql);
}
}
<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!-- springconfigStart -->
<!-- 加载多个资源配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:frame_jdbc.properties</value>
</list>
</property>
</bean>
<!-- 使用注解的方式装配置bean -->
<context:annotation-config />
<context:component-scan base-package="com.csdn"></context:component-scan>
<!-- 配置dbcp数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置jdbctemplate -->
<bean id="JdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- springconfigEnd springconfigEnd springconfigEnd springconfigEnd springconfigEnd springconfigEnd springconfigEnd springconfigEnd-->
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>spring001</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
################Oracle_JDBC################
jdbc.driverClassName=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcl
jdbc.username=oracle
jdbc.password=oracle2017
package com.csdn.student.test;
import static org.junit.Assert.*;
import javax.annotation.Resource;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.csdn.student.bean.Student;
import com.csdn.student.dao.StudentDao;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
public class StudentTest {
@Resource
private StudentDao dao;
//初始化student对象
private Student student;
@Before
public void setUp() throws Exception {
//做一些初始化操作 比如创建对象等
student=new Student();
student.setStuid("888");
student.setStuname("zhagnsan");
student.setStupwd("123456");
student.setCreatetime("2017-1-31");
}
@Test
public void insertStudent() {
try {
StringBuffer sb=new StringBuffer();
sb.append("insert into STUDENT(stuid,stuname,stupwd,createtime) ");
sb.append("values('"+student.getStuid()+"','"+student.getStuname()+"','"+student.getStupwd()+"','"+"2017"+"') ");
System.out.println(sb.toString());
int result= dao.insertStudent(sb.toString());
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}
//运行结果
//源码下载 http://pan.baidu.com/s/1eSDyQnK