MYSQL+SSH integration sample, source code download

Source: Internet
Author: User

Project reference jar Download: http://download.csdn.net/detail/adam_zs/7262727

Project Source code: http://download.csdn.net/detail/adam_zs/7262749

Today took the time to integrate SSH, and once again to learn, I hope to help you!

I use the MySQL database, the table statement is relatively simple to stick out, when the establishment of the table when the setting ID for their own initiative to add? Oh.

Project file location, project reference jar package


Project configuration file

Xml

<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><welcome-file-list><welcome-file>index.jsp</ welcome-file></welcome-file-list><!--initializes the spring container with Contextloaderlistener--><listener>< Listener-class>org.springframework.web.context.contextloaderlistener</listener-class></listener ><!--Define the filter--><filter><filter-name>struts2</filter-name> for Struts2 filterdispathcer <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class></filter><!-- Filterdispatcher is used to initialize Struts2 and to process all Web requests. --><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</ Url-pattern></filter-mapping></web-app>

Struts.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "http://struts.apache.org/dtds/ Struts-2.0.dtd "><struts><!--configured with series constants--><constant name=" struts.i18n.encoding "value=" UTF-8 "/> <package name= "Wangzs" extends= "Struts-default" ><action name= "Login" class= "loginaction" ><result Name= "Error" >/error.jsp</result><result name= "Success" >/welcome.jsp</result></action ><!--give users direct access to the app when the full view page is listed--><action name= "" ><result>.</result></action></ Package></struts>

Applicationcontext.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE beans Public "-//spring//dtd BEAN 2.0//en" "Http://www.springframework.org/dtd/spring-beans-2.0.dtd" >< beans><!--Define the data source bean, implement--><bean id= "DataSource" class= "using the C3P0 data source Com.mchange.v2.c3p0.ComboPooledDataSource "destroy-method=" Close "><property name=" Driverclass "value=" Com.mysql.jdbc.Driver "/><property name=" Jdbcurl "value=" Jdbc:mysql://localhost/test "/><property name= "User" value= "root"/><property name= "password" value= "wzs_626750095"/><property name= "Maxpoolsize" Value= "/><property name=" Minpoolsize "value=" 1 "/><property name=" Initialpoolsize "value=" 1 "/> <property name= "MaxIdleTime" value= "/></bean><!--definition hibernate sessionfactory--><bean id=" Sessionfactory "class=" Org.springframework.orm.hibernate3.LocalSessionFactoryBean "><property name=" DataSource "ref=" DataSource "/><!--mappingresouces property is used to list all mapping files--><propertyName= "Mappingresources" ><list><value>com/wzs/bean/Person.hbm.xml</value></list>< /property><!--define Hibernate's Sessionfactory properties--><property name= "Hibernateproperties" ><props> <prop key= "Hibernate.dialect" >org.hibernate.dialect.mysqlinnodbdialect</prop><prop key= " Hibernate.hbm2ddl.auto ">update</prop><prop key=" Hibernate.show_sql ">true</prop><prop key = "Hibernate.format_sql" >true</prop></props></property></bean><bean id= " Loginaction "class=" Com.wzs.action.LoginAction "scope=" prototype "><property name=" MS "ref=" MyService "/> </bean><bean id= "MyService" class= "Com.wzs.service.impl.MyServiceImpl" ><property name= "Persondao" ref= "Persondao"/></bean><bean id= "Persondao" class= "Com.wzs.dao.impl.PersonDaoImpl" ><property Name= "Sessionfactory" ref= "Sessionfactory"/></bean></beans>

Java Code

Person.java

Package Com.wzs.bean;public class Person {private Integer id;private string name;private string Password;public Integer ge TId () {return ID;} public void SetId (Integer id) {this.id = ID;} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;}}

Person.hbm.xml

<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mappingpublic "-//hibernate/hibernate Mapping DTD 3.0//en" "http://hibernate.sourceforge.net/ Hibernate-mapping-3.0.dtd ">

Loginaction.java

Package Com.wzs.action;import Com.opensymphony.xwork2.actionsupport;import com.wzs.service.myservice;@ Suppresswarnings ("Serial") public class Loginaction extends Actionsupport {//The following are two properties used to encapsulate user request parameters private String name; private string password;//The property used to encapsulate the result of the private string tip;//the business logic component used by the system private MyService ms;// Set the setter method required to inject the business logic component public void Setms (MyService ms) {this.ms = ms;} /** * User Login * * @return * @throws Exception */public String Login () throws Exception {//Invoke the valid method of the business logic component to//validate user input Userna If me and password are correct if (Ms.valid (GetName (), GetPassword ())) {Settip ("Haha, integrated successfully!) "); return SUCCESS;} else {return ERROR;}} Public String GetName () {return name;} public void SetName (String name) {this.name = name;} Public String GetPassword () {return password;} public void SetPassword (String password) {this.password = password;} Public String Gettip () {return tip;} public void Settip (String tip) {this.tip = tip;} Public MyService Getms () {return MS;}}

Myservice.java

Package Com.wzs.service;public interface MyService {/** * check username password *  * @param name *            username * @param passwor d *            Password * @return true: Present, false: */boolean valid not present (string name, string password);}

Myserviceimpl.java

Package Com.wzs.service.impl;import Com.wzs.dao.persondao;import Com.wzs.service.myservice;public class Myserviceimpl implements MyService {private Persondao persondao;/** * Checksum username password *  * @param name *            username * @p Aram Password *            password * @return true: Present, False: */public Boolean valid not present (string name, string password) {return Persondao. Valid (name, password);} Public Persondao Getpersondao () {return persondao;} public void Setpersondao (Persondao persondao) {This.persondao = Persondao;}}

Persondao.java

Package Com.wzs.dao;import Java.util.list;import Com.wzs.bean.person;public interface Persondao {/** * check username password * * @ PARAM name * username * @param password * password * @return true: Present, False: */public Boolean valid not present (Stri ng name, String password);p ublic person get (Integer ID);/** * Save Person instance * @param person * required to save person instance * @ Return the Identity property value of the person instance you just saved */public Integer save (person person),/** * Change Person instance * * @param person * required changes pers On instance */public void Update (person person);/** * Delete person instance * * @param ID * The IDENTITY property value of the person instance that needs to be deleted */public void de Lete (Integer ID);/** * Delete person instance * * @param person * need to delete person instance */public void Delete (person person);/** * Find Person by username * * @param name * Query name * @return Specify all person */public list<person> Findbyn for the corresponding username Ame (String name);/** * queries All person instances * * @return All person instances */@SuppressWarnings ("unchecked") public List Findallperson (); * * Total number of person instances in query data table * * Total number of person instances in the @return data table */public long Getpersonnumber ();} 

Persondaoimpl.java

Package Com.wzs.dao.impl;import Java.util.list;import Org.hibernate.sessionfactory;import Org.springframework.orm.hibernate3.hibernatetemplate;import Com.wzs.bean.person;import Com.wzs.dao.PersonDao; public class Persondaoimpl implements Persondao {private hibernatetemplate ht = null;private sessionfactory sessionfactor y;//Dependency Injection Sessionfactory setter method public void Setsessionfactory (Sessionfactory sessionfactory) {this.sessionfactory = Sessionfactory;} Method of initializing Hibernatetemplate private hibernatetemplate gethibernatetemplate () {if (HT = = NULL) {HT = new Hibernatetemplate (s Essionfactory);} return HT;} /** * Verify username Password * * @param name * username * @param password * password * @return true: Present, false: not present */ @SuppressWarnings ("Unchecked") public boolean valid (string name, string password) {list<person> List = Gethibernatetemplate (). Find ("from person p where p.name=?"). And p.password=? ", new string[] {name, password}), if (list.size () > 0) {return true;} return false;} /** * Loading PErson instance * * @param ID * The IDENTITY property value of the person instance that needs to be loaded * @return The corresponding person instance of the specified ID */public person get (Integer ID) {return (person) gethibernatetemplate (). Get (Person.class, id);} /** * Save Person instance * @param person * need to save the person instance * @return The IDENTITY property value of the person instance you just saved */public Integer save (person person) {return (Integer) gethibernatetemplate (). Save (person);} /** * Change Person instance * * @param person * needs to change person instance */public void Update (person person) {gethibernatetemplate () . Update (person); /** * Delete Person instance * * @param ID * The identity attribute value of the person instance that needs to be deleted */public void delete (Integer id) {gethibernatetemplate (). Delete (get (ID));} /** * Delete Person instance * * @param person * need to delete person instance */public void Delete (person person) {gethibernatetemplate () . Delete (person);} /** * Find person by username * * @param name * Query name * @return Specify all of the username corresponding person */@SuppressWarnings ("Unchecke D ") Public list<person> findbyname (String name) {return (list<person>) GethibernatetemplAte (). Find ("from person p where p.name like?", name);} /** * Query All person instances * * @return All person instances */@SuppressWarnings ("unchecked") public List Findallperson () {return (list<pe rson>) Gethibernatetemplate (). Find ("from"); /** * Total number of person instances in the query data table * * @return The total number of person instances in the datasheet */public long Getpersonnumber () {return (long) gethibernatetemplate () . Find ("SELECT count (*) from person as P"). Get (0);}}

JSP Interface

login.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%>

welcome.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%><% @taglib prefix=" s "uri="/struts-tags "%>


error.jsp

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "%>


MYSQL+SSH integration sample, source code download

Related Article

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.