An example of spring + hibernate

Source: Internet
Author: User
Development tools and environment: Windows XP, Tomcat 5.5.17, eclipse 3.2, myeclipse 5.0m2, MySQL 5.17 1. Create a web project; 2. Add spring capabinities and select the required packages. next, add the hibernate package. note that the order of addition is very important. 3. The following code is: Admin. java

Package com. yxy. entity ;/**
* Admin generated by myeclipse-hibernate tools
*/Public class admin implements Java. io. serializable {Private Static final long serialversionuid = 1l; private long ID; private string uname; private string upsw; private string ulevel; // Constructors/** default constructor */
Public Admin (){
}/** Full constructor */
Public Admin (string uname, string upsw, string ulevel ){
This. uname = uname;
This. upsw = upsw;
This. ulevel = ulevel;
} // Property accessors public long GETID (){
Return this. ID;
} Public void setid (long ID ){
This. ID = ID;
} Public String getuname (){
Return this. uname;
} Public void setuname (string uname ){
This. uname = uname;
} Public String getupsw (){
Return this. upsw;
} Public void setupsw (string upsw ){
This. upsw = upsw;
} Public String getulevel (){
Return this. ulevel;
} Public void setulevel (string ulevel ){
This. ulevel = ulevel;
} Admin. HBM. xml <? XML version = "1.0"?>
<! Doctype hibernate-mapping public "-// hibernate/hibernate mapping DTD 3.0 // en"
Http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<! --
Mapping File autogenerated by myeclipse-hibernate tools
-->
<Hibernate-mapping>
<Class name = "com. yxy. entity. admin" table = "admin">
<ID name = "ID" type = "long">
<Column name = "ID"/>
<Generator class = "increment"/>
</ID>
<Property name = "uname" type = "string">
<Column name = "uname" length = "20"/>
</Property>
<Property name = "upsw" type = "string">
<Column name = "upsw" length = "20"/>
</Property>
<Property name = "ulevel" type = "string">
<Column name = "ulevel" length = "1"/>
</Property>
</Class>
</Hibernate-mapping> adminimpl. Java package com. yxy. impl; import com. yxy. entity. admin; public interface adminimpl {
Void insertuser (Admin admin );
Boolean islogin (string uname, string upwd );
} Admindao package com. yxy. springdao; import java. util. iterator;
Import java. util. List; import org. springframework. Orm. hibernate3.support. hibernatedaosupport; import com. yxy. entity. admin;
Import com. yxy. impl. adminimpl; public class admindao extends hibernatedaosupport implements adminimpl {public void insertuser (Admin admin ){
// Todo auto-generated method stub
Super. gethibernatetemplate (). Save (Admin );
}
 
Public Boolean islogin (string uname, string upwd ){
List list = gethibernatetemplate (). find ("from Admin as a where. uname = '"+ uname +"' and. upsw = '"+ upwd + "'");
Iterator it = List. iterator ();
If (! It. hasnext ()){
System. Out. println ("Logon Failed ");
Return false;
}
Else {
System. Out. println ("Login successful ");
Return true;
}
} Unit test unittest. Java package com. yxy. Unit; import org. springframework. Beans. Factory. beanfactory;
Import org. springframework. Beans. Factory. xml. xmlbeanfactory;
Import org. springframework. Core. Io. classpathresource;
Import org. springframework. Core. Io. Resource; import com. yxy. entity. admin;
Import com. yxy. impl. adminimpl; import JUnit. Framework. testcase; public class unittest extends testcase {
Public void testcase () throws exception {
Resource resource = new classpathresource ("applicationcontext. xml ");
Beanfactory factory = new xmlbeanfactory (Resource );
Adminimpl Al = (adminimpl) Factory. getbean ("admindao ");
Admin admin = new admin ();
Admin. setuname ("admin ");
Admin. setupsw ("admin ");
Admin. setulevel ("0 ");
Al. insertuser (Admin );
Al. islogin ("admin", "admin ");
}
} Configuration file applicationcontext. xml <? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>
<Bean id = "datasource"
Class = "org. Apache. commons. DBCP. basicdatasource">
<Property name = "driverclassname">
<Value> com. MySQL. JDBC. Driver </value>
</Property>
<Property name = "url">
<Value> JDBC: mysql: // localhost: 3306/infoweb </value>
</Property>
<Property name = "username">
<Value> root </value>
</Property>
</Bean>
 
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref bean = "datasource"/>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
Org. hibernate. dialect. mysqldialect
</Prop>
<Prop key = "hibernate. show_ SQL">
True
</Prop>
<Prop key = "hibernate. hbm2ddl. Auto"> Create </prop>
</Props>
</Property>
<Property name = "mappingresources">
<Value> COM/yxy/entity/admin. HBM. xml </value>
</Property>
</Bean>
 
<Bean id = "admindao" class = "com. yxy. springdao. admindao">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>
 
</Beans> Add log4j. Property.
 

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.