Spring教程____Spring简单小列子

来源:互联网 发布:阿里云服务器 密钥 编辑:程序博客网 时间:2024/05/17 09:33
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;// Fieldsprivate 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 accessorspublic 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;@Repositorypublic class StudentDaoImpl implements StudentDao{@Resource private JdbcTemplate jdbcTemplate;@Overridepublic 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.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsdhttp://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.OracleDriverjdbc.url=jdbc:oracle:thin:@127.0.0.1:1521:orcljdbc.username=oraclejdbc.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 {@Resourceprivate StudentDao dao;//初始化student对象private Student student; @Beforepublic void setUp() throws Exception {//做一些初始化操作 比如创建对象等student=new Student();student.setStuid("888");student.setStuname("zhagnsan");student.setStupwd("123456");student.setCreatetime("2017-1-31");}@Testpublic 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


原创粉丝点击