Spring Beginners (1)

Source: Internet
Author: User
Tags tortoisesvn

to change a good job in the future, learn some spring knowledge. Will PHP, C + +, Java, who if there is a good job, please introduce me, not good.

I am a beginner, so a lot of people do not understand, write these things, but also just for the convenience of their later look. There is nothing wrong with the place, we communicate more.

1.1 Spring's Package download,

Download a lot of versions of the package, I think this is necessary, because there will always be a few. jar, omitted in a certain version. I am under the 3.2.7, but later I also downloaded other versions, I will upload to my download, convenient for everyone to download.



1.2 Creating a simple program structure

A student entity class, a student abstract interface, a student interface implementation class. The structure is very clear: the interface is mainly used for unified invocation of the interface implementation class, the interface implementation class is used to encapsulate the student entity class (Here said encapsulation does not know the right). You will then need to add a service class.

The basic structure is as follows:


1.3 Student Entity Class introduction

Package com.cn.objinstance;/* * Student Class */public class Students {private string sid;//studnet idprivate string name; Student Nameprivate int    age;//student agepublic String getsid () {return SID;} public void Setsid (String sid) {this.sid = SID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} public int getage () {return age;} public void Setage (int.) {this.age = age;}}

Introduction to the abstract interface class for 1.4 students:

Package Com.cn.objinterface;import Com.cn.objinstance.students;public Interface Studentsdao {/* *  */public Boolean savestudent (Students stu);}

1.5 Student's abstract interface implementation class

This class is primarily used to implement the operation of saving student information. The original example is System.out output information. But in that case, I'm not too small for pediatrics. It is then modified to submit student information to the database. In fact, I also have a lot of details do not understand.

MySQL database is installed on my computer, so I decided to use MySQL database. To create a table:

1.5.1 Create a table, of course, this kind of hand-created I am also very uncomfortable, behind all the changes into SQL to create it.



1.5.2 Preview After the creation is complete:



1.5.3 Interface Implementation Class Introduction

Package Com.cn.objimplement;import Javax.sql.datasource;import Org.springframework.dao.dataaccessexception;import Org.springframework.jdbc.core.jdbctemplate;import Com.cn.objinstance.students;import Com.cn.objinterface.studentsdao;public class Studentsdaoimplements implements Studentsdao {//Manipulate database data source private JdbcTemplate JdbcTemplate; /* * @see com.cn.objinterface.studentsdao#savestudent (com.cn.objInstance.Students) */public Boolean savestudent ( Students Stu) {if (stu!=null) {try{jdbctemplate.update ("INSERT into Baseinfo (STUDENTSNAME,STUDENTSAGE,STUDENTSID) VALUES (?,?,?) ", New Object[]{stu.getname (), Stu.getage (), Stu.getsid ()},new Int[]{java.sql.types.varchar, Java.sql.types.integer,java.sql.types.varchar});} catch (DataAccessException ex) {System.out.println (Ex.getmessage ());} System.out.print (Stu.getname () + "-" +stu.getage ()); return True;//save Students}return false;} /* * Set the injection interface of the data source */public void Setdatasource (DataSource DataSource) {this.jdbctemplate = new JdbcTemplate (Datasou    RCE);  }} 

1.6 is called the service layer, service class, to provide services for the outside world.

Package Com.cn.service;import com.cn.objinstance.students;import com.cn.objinterface.studentsdao;/* *  */public Class Studentsservices {private Studentsdao studao;public Studentsdao Getstudao () {return studao;} public void Setstudao (Studentsdao studao) {This.studao = Studao;} /* * Save Students * Service Layer Call Interface layer */public Boolean savestudents (Students stu) {return studao.savestudent (stu);}}

The basic framework is this, it should be obvious.

1.7 Create a test class. For testing code implementations:

Package Com.cn.objtest;import Junit.framework.testcase;import Org.springframework.context.ApplicationContext; Import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.util.Assert ; Import Com.cn.objinstance.students;import com.cn.service.studentsservices;/* *  */public class Teststudentsservice extends TestCase {public void testsavestudents () {ApplicationContext stx=new Classpathxmlapplicationcontext ("Frameworkcontext2.xml"); Students stu= (Students) Stx.getbean ("Students"); Stu.setname ("Job search"); Stu.setage ("2014-01"); Studentsservices sve= (studentsservices) Stx.getbean ("service");//assert. Assertequals (True,sve.savestudents (Stu)); Sve.savestudents (Stu);}}

1.8 The above generates a student object and then calls the service layer class to save the student information. Here's a look at the configuration file, I'm an excerpt from the online example.

<?xml version= "1.0" encoding= "UTF-8"? ><beansxmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans-2.0.xsd "><bean id=" Dbsource "class=" Org.apache.commons.dbcp.BasicDataSource "destroy-method=" Close "> <property name=" driverclassname "value=            "Com.mysql.jdbc.Driver"/> <property name= "url" value= "Jdbc:mysql://localhost:3306/studentinfo"/> <property name= "username" value= "root"/> <property name= "password" value= "/> </bean& Gt;<bean id= "Students" class= "com.cn.objInstance.Students"/><bean id= "Studentsdao" class= " Com.cn.objImplement.StudentsDaoImplements "><property name=" DataSource "><ref bean=" Dbsource "/>< /property></bean><bean id= "service" class= "Com.cn.service.StudentsServices" ><propErty name= "Studao" ><ref bean= "Studentsdao"/></property></bean></beans> 

Creates a data object Dbsource, and assigns a value to the Studentsdao property.

1.9 Run function testsavestudents (), function execution.



2.0 Although it is a small application, but next I will install SVN, to his revision, after all, this thing is very water, such as the database of the transaction mechanism, such as the database of complex operations, need to improve, I hope you give me good advice.

Download VISUALSVN installation, specify the name of the code warehouse, create a project, here I also do not understand, first of all, do not do the wrong hope you point out to modify the exchange:


2.1 Download TORTOISESVN, installed by default, the turtle is automatically loaded into the right-click Properties after installation.

In the previous step, we created a project and assigned an editable user to the project. Now, we check it out locally (let's call it that).



2.1.0 Check out the results as follows:

2.1.1 below to put the previously created students are placed in the directory branches, upload to the repository, here we first put here, because I do not know how to define, exactly what to do a thing. Right-click Add in TortoiseSVN, and then click Commit. At this point you will see that the repository already has its own project information:



I personally prefer English, if you have a special love of Chinese, you can download TortoiseSVN language installation.

I am also the initiator, there are many do not understand, I hope you help me quickly improve themselves.

Spring Beginners (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.