Generally, in applications involving databases, there are two primary key generation schemes for tables. One is to define a primary key table and place an auto-increment field in it to provide the primary key for other tables; the other is the sequence of oracle. Both solutions are troublesome. Spring provides an ID extender to simplify the specific steps. The following describes how to use it. The database used is mysql5.
Induction
Use the auto-growth ID generator of spring to complete the following three steps:
1) configure the auto-increment ID generator, which requires the support of a data source.
2) inject the self-growth ID generator into various types of Dao according to the configuration.
3) use the nextstringvalue, nextintvalue, or nextlongvalue method to obtain the ID.
For details, refer to the following code:
1. Configure the ID extender in the spring configuration file.
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd">
<Beans>
<! -- Data source -->
<Bean id = "datasource"
Class = "org. springframework. JDBC. datasource. drivermanagerdatasource">
<Property name = "driverclassname"
Value = "org. gjt. Mm. MySQL. Driver">
</Property>
<Property name = "url" value = "JDBC: mysql: // 127.0.0.1/test">
</Property>
<Property name = "username" value = "root"> </property>
<Property name = "password" value = "hy"> </property>
</Bean>
<! -- Jdbctemplate for various Dao classes
-->
<Bean id = "jdbctemplate"
Class = "org. springframework. JDBC. Core. jdbctemplate">
<Property name = "datasource">
<Ref bean = "datasource"
/>
</Property>
</Bean>
<! -- Auto-increment ID generator -->
<Bean id = "idgenarater"
Class = "org. springframework. JDBC. Support. incrementer. mysqlmaxvalueincrementer">
<Property name = "incrementername" value = "forumidtable"/>
<Property name = "columnname" value = "sequence_id"/>
<Property name = "cachesize" value = "10"/>
<Property name = "datasource" ref = "datasource"/>
</Bean>
<! -- User Service
-->
<Bean id = "userservice"
Class = "com. heyang. Service. userservice">
<Property name = "Dao" ref = "userdao"/>
<Property name = "table" value = "forumuser"/>
</Bean>
<! -- User Dao -->
<Bean id = "userdao"
Class = "com. heyang. Dao. userdao">
<Property name = "idgenarater" ref = "idgenarater"/>
<Property name = "table" value = "forumuser"/>
<Property name = "jdbctemplate">
<Ref bean = "jdbctemplate"
/>
</Property>
</Bean>
</Beans> 2. Auto-increment ID Generator configuration.
<! -- Auto-increment ID generator -->
<Bean id = "idgenarater"
Class = "org. springframework. JDBC. Support. incrementer. mysqlmaxvalueincrementer">
<Property name = "incrementername" value = "forumidtable"/> // name of the table where the auto-increment Id field is located: "forumidtable"
<Property name = "columnname" value = "sequence_id"/> // The auto-increment Id field "sequence_id"
<Property name = "cachesize" value = "10"/> // Number of IDS generated at a time. In this setting, ten IDs are generated at a time and will be retrieved from the cache later, you don't need to access the database every time, which improves efficiency.
<Property name = "datasource" ref = "datasource"/> // Data Source
</Bean>
3. Use the basedao class of the auto-increment ID generator, which is the base class of userdao.
The idgenarator can be used after being injected into the configuration file. The nextstringvalue method is used to generate an ID of the text type.
Package com. heyang. Dao. base;
Import java. util. List;
Import org. springframework. JDBC. Support. incrementer. datafieldmaxvalueincrementer;
Import com. heyang. domain. Base. basedomainobj;
/**
* Basic category of DAO class of the domain object
* @ Author he Yang (heyang78@gmail.com)
*
* @ Since 09:51:38
* @ Version 1.00
*/
Public Abstract
Class basedao extends Dao {
Protected string SQL;
/**
* Tables related to domain object access
*/
Protected string table;
/**
* Record primary key generator
*/
Protected datafieldmaxvalueincrementer idgenarater;
/**
* Create an object to the database
* @ Param OBJ
*/
Public void create (basedomainobj OBJ ){
If (obj. GETID () = NULL ){
OBJ. setid (idgenarater. nextstringvalue ());
}
Save (OBJ );
}
/**
* Save a domain object to the database.
* Forced subclass completion
* @ Param OBJ
*/
Protected Abstract
Void save (basedomainobj OBJ );
/**
* Query an object from a database
* Forced subclass completion
* @ Param SQL
* @ Return
*/
Public abstract list <basedomainobj> Search (string SQL );
/**
* Update an object
* Forced subclass completion
* @ Param OBJ
*/
Public Abstract
Void Update (basedomainobj OBJ );
/**
* Get an object by ID
* @ Param OBJ
*/
Public basedomainobj getbyid (string ID ){
SQL = "select * from" + Table + "where id = '" + ID + "'";
List <basedomainobj> ls = search (SQL );
If (LS. Size () = 1 ){
Return search (SQL). Get (0 );
}
Else {
Return NULL;
}
}
/**
* Delete an object
* @ Param OBJ
*/
Public void Delete (basedomainobj OBJ ){
String SQL = "delete from" + Table + "where id = '" + obj. GETID () + "'";
Jdbctemplate.exe cute (SQL );
}
Public String gettable (){
Return table;
}
Public void settable (string table ){
This. Table = table;
}
Public datafieldmaxvalueincrementer getidgenarater (){
Return idgenarater;
}
Public void setidgenarater (datafieldmaxvalueincrementer idgenarater ){
This. idgenarater = idgenarater;
}
}