|
Posted on 2006-12-02 16:05 码农cz 阅读(269) 评论(0) 编辑 收藏 所属分类: Spring Around
工程结构如此图:
在web-inf下如此图:
这里,没有用test-case因为比较简单,所以,我们赤膊上阵:
1
#authorities.sql如下:
2
3
CREATE
TABLE
`authorities` (
4
`AUTH_ID`
INTEGER
(
11
)
NOT
NULL
DEFAULT
'
0
'
,
5
`AUTHORITY`
VARCHAR
(
255
)
NOT
NULL
,
6
`AUTH_TYPE`
VARCHAR
(
32
)
NOT
NULL
,
7
`PROTECTED_RES`
VARCHAR
(
64
)
NOT
NULL
,
8
`DISPLAY`
VARCHAR
(
64
)
NOT
NULL
,
9
`NOTE`
VARCHAR
(
64
)
DEFAULT
NULL
,
10
PRIMARY
KEY
(`AUTH_ID`),
11
UNIQUE
KEY
`AUTH_ID` (`AUTH_ID`)
12
);
13
14
COMMIT
;
15
16
INSERT
INTO
`authorities` (`AUTH_ID`, `AUTHORITY`, `AUTH_TYPE`, `PROTECTED_RES`, `DISPLAY`, `NOTE`)
VALUES
17
(
1
,
'
AUTH_FUNC_ContactManager.create
'
,
'
FUNCTION
'
,
'
sample.service.IContactManager.create
'
,
'
创建联系人
'
,
NULL
),
18
(
2
,
'
AUTH_FUNC_ContactManager.delete
'
,
'
FUNCTION
'
,
'
sample.service.IContactManager.delete
'
,
'
删除联系人
'
,
NULL
),
19
(
3
,
'
AUTH_FUNC_ContactManager.getAll
'
,
'
FUNCTION
'
,
'
sample.service.IContactManager.getAll
'
,
'
取联系人列表
'
,
NULL
),
20
(
4
,
'
AUTH_FUNC_ContactManager.getById
'
,
'
FUNCTION
'
,
'
sample.service.IContactManager.getById
'
,
'
根据ID取联系人
'
,
NULL
),
21
(
5
,
'
AUTH_FUNC_ContactManager.update
'
,
'
FUNCTION
'
,
'
sample.service.IContactManager.update
'
,
'
更新联系人信息
'
,
NULL
),
22
(
0
,
'
AUTH_USER
'
,
'
USER
'
,
'
USER
'
,
'
一般用户权限
'
,
NULL
);
23
24
25
##contacts.sql如下:
26
27
CREATE
TABLE
`contacts` (
28
`ID`
INTEGER
(
11
)
NOT
NULL
AUTO_INCREMENT,
29
`CONTACT_NAME`
VARCHAR
(
50
)
NOT
NULL
,
30
`EMAIL`
VARCHAR
(
50
)
DEFAULT
NULL
,
31
PRIMARY
KEY
(`ID`),
32
UNIQUE
KEY
`ID` (`ID`)
33
);
34
35
COMMIT
;
36
37
INSERT
INTO
`contacts` (`ID`, `CONTACT_NAME`, `EMAIL`)
VALUES
38
(
1
,
'
contact1
'
,
'
contact1@sample.com
'
),
39
(
2
,
'
contact2
'
,
'
contact2@sample.com
'
),
40
(
3
,
'
contact3
'
,
'
contact3@sample.com
'
),
41
(
5
,
'
contact22asdasd
'
,
'
contact1@sample.com
'
),
42
(
10
,
'
1`23123123
'
,
'
aaaa
'
);
43
44
45
##user_auth.sql如下:
46
47
CREATE
TABLE
`user_auth` (
48
`
USER_ID
`
INTEGER
(
11
)
NOT
NULL
DEFAULT
'
0
'
,
49
`AUTH_ID`
INTEGER
(
11
)
NOT
NULL
DEFAULT
'
0
'
,
50
PRIMARY
KEY
(`
USER_ID
`, `AUTH_ID`)
51
);
52
53
COMMIT
;
54
55
INSERT
INTO
`user_auth` (`
USER_ID
`, `AUTH_ID`)
VALUES
56
(
1
,
0
),
57
(
1
,
1
),
58
(
1
,
2
),
59
(
1
,
3
),
60
(
1
,
4
),
61
(
1
,
5
),
62
(
2
,
0
),
63
(
2
,
3
),
64
(
2
,
4
);
65
66
67
##userinfo.sql如下:
68
69
CREATE
TABLE
`userinfo` (
70
`
USER_ID
`
INTEGER
(
11
)
NOT
NULL
AUTO_INCREMENT,
71
`USERNAME`
VARCHAR
(
10
)
NOT
NULL
,
72
`PASSWORD`
VARCHAR
(
30
)
DEFAULT
NULL
,
73
`ENABLED`
TINYINT
(
1
)
NOT
NULL
DEFAULT
'
0
'
,
74
PRIMARY
KEY
(`
USER_ID
`),
75
UNIQUE
KEY
`
USER_ID
` (`
USER_ID
`),
76
UNIQUE
KEY
`USERNAME` (`USERNAME`)
77
);
78
79
COMMIT
;
80
81
INSERT
INTO
`userinfo` (`
USER_ID
`, `USERNAME`, `PASSWORD`, `ENABLED`)
VALUES
82
(
1
,
'
root
'
,
'
root
'
,
1
),
83
(
2
,
'
readonly
'
,
'
readonly
'
,
1
);
84
85
86
下面是java代码,太多了都放在一起吧 1IContactDao接口: 2 3 package sample.dao; 4 5import sample.Contact; 6 7import java.util.List; 8 9public interface IContactDao{ 10 public Contact getById(Integer id); 11 public void create(Contact contact); 12 public void delete(Integer contactId); 13 public List findAll(); 14 public void update(Contact contact); 15} 16 17 18ContactDao: 19 20/**//* 21 * @(#)ContactDao.java 22 */ 23package sample.dao.impl; 24 25import org.springframework.jdbc.core.support.JdbcDaoSupport; 26import org.springframework.jdbc.core.SqlParameter; 27import org.springframework.jdbc.object.SqlUpdate; 28import org.springframework.jdbc.object.MappingSqlQuery; 29import sample.dao.IContactDao; 30import sample.Contact; 31 32import javax.sql.DataSource; 33import java.sql.Types; 34import java.sql.ResultSet; 35import java.sql.SQLException; 36import java.util.List; 37 38public class ContactDao extends JdbcDaoSupport implements IContactDao{ 39 private ContactDelete contactDelete; 40 private ContactInsert contactInsert; 41 private ContactUpdate contactUpdate; 42 private ContactsAllQuery contactsAllQuery; 43 private ContactsByIdQuery contactsByIdQuery; 44 45 //~ Methods ================================================================ 46 47 public Contact getById(Integer id){ 48 List list=contactsByIdQuery.execute(id.intValue()); 49 if(list.size()==0){ 50 return null; 51 } else{ 52 return (Contact)list.get(0); 53 } 54 } 55 56 public void create(Contact contact){ 57 contactInsert.insert(contact); 58 } 59 60 public void delete(Integer contactId){ 61 contactDelete.delete(contactId); 62 } 63 64 public List findAll(){ 65 return contactsAllQuery.execute(); 66 } 67 68 public void update(Contact contact) { 69 contactUpdate.update(contact); 70 } 71 72 protected void initDao() throws Exception{ 73 contactInsert=new ContactInsert(getDataSource()); 74 contactUpdate=new ContactUpdate(getDataSource()); 75 contactDelete=new ContactDelete(getDataSource()); 76 contactsAllQuery=new ContactsAllQuery(getDataSource()); 77 contactsByIdQuery=new ContactsByIdQuery(getDataSource()); 78 } 79 80 81 protected class ContactDelete extends SqlUpdate{ 82 protected ContactDelete(DataSource ds){ 83 super(ds, "DELETE FROM contacts WHERE id = ?"); 84 declareParameter(new SqlParameter(Types.INTEGER)); 85 compile(); 86 } 87 88 protected void delete(Integer contactId){ 89 super.update(contactId.intValue()); 90 } 91 } 92 93 protected class ContactInsert extends SqlUpdate{ 94 protected ContactInsert(DataSource ds){ 95 super(ds, "INSERT INTO contacts ( CONTACT_NAME, EMAIL) VALUES (?,?)"); 96 declareParameter(new SqlParameter(Types.VARCHAR)); 97 declareParameter(new SqlParameter(Types.VARCHAR)); 98 compile(); 99 } 100 101 protected void insert(Contact contact){ 102 Object[] objs=new Object[]{contact.getName(),contact .getEmail()}; 103 super.update(objs); 104 } 105 } 106 107 protected class ContactUpdate extends SqlUpdate{ 108 protected ContactUpdate(DataSource ds){ 109 super(ds, 110 "UPDATE contacts SET contact_name = ?, email = ? WHERE id = ?" 111 ); 112 declareParameter(new SqlParameter(Types.VARCHAR)); 113 declareParameter(new SqlParameter(Types.VARCHAR)); 114 declareParameter(new SqlParameter(Types.INTEGER)); 115 compile(); 116 } 117 118 protected void update(Contact contact){ 119 Object[] objs=new Object[]{contact.getName(),contact.getEmail(),contact 120 .getId()}; 121 super.update(objs); 122 } 123 } 124 125 protected class ContactsAllQuery extends MappingSqlQuery{ 126 protected ContactsAllQuery(DataSource ds){ 127 super(ds, "SELECT id, contact_name, email FROM contacts ORDER BY id"); 128 compile(); 129 } 130 131 protected Object mapRow(ResultSet rs, int rownum) 132 throws SQLException{ 133 Contact contact=new Contact(); 134 contact.setId(new Integer(rs.getInt("id"))); 135 contact.setName(rs.getString("contact_name")); 136 contact.setEmail(rs.getString("email")); 137 138 return contact; 139 } 140 } 141 142 protected class ContactsByIdQuery extends MappingSqlQuery{ 143 protected ContactsByIdQuery(DataSource ds){ 144 super(ds, 145 "SELECT id, contact_name, email FROM contacts WHERE id = ? ORDER BY id" 146 ); 147 declareParameter(new SqlParameter(Types.INTEGER)); 148 compile(); 149 } 150 151 protected Object mapRow(ResultSet rs, int rownum) 152 throws SQLException{ 153 Contact contact=new Contact(); 154 contact.setId(new Integer(rs.getInt("id"))); 155 contact.setName(rs.getString("contact_name")); 156 contact.setEmail(rs.getString("email")); 157 158 return contact; 159 } 160 } 161 162} 163 164 165 166IContactManager: 167 168/**//* 169 * @(#)IContactManager.java 170 * 171 * Copyright 2003 Copyright.com, Inc. All rights reserved. 172 * Use is subject to license terms. 173 */ 174package sample.service; 175 176import sample.Contact; 177 178import java.util.List; 179 180 181public interface IContactManager{ 182 public List getAll(); 183 public Contact getById(Integer id); 184 public void create(Contact contact); 185 public void update(Contact contact); 186 public void delete(Contact contact); 187} 188 189 190ContactManager: 191 192/**//* 193 * @(#)ContactManager.java 194 * 195 * Copyright 2003 Copyright.com , Inc. All rights reserved. 196 * Use is subject to license terms. 197 */ 198package sample.service.impl; 199 200import sample.service.IContactManager; 201import sample.Contact; 202import sample.dao.impl.ContactDao; 203 204import java.util.List; 205 206public class ContactManager implements IContactManager{ 207 private ContactDao contactDao; 208 public void setContactDao(ContactDao contactDao) { 209 this.contactDao = contactDao; 210 } 211 212 public ContactDao getContactDao() { 213 return contactDao; 214 } 215 216 public void create(Contact contact){ 217 contactDao.create(contact); 218 } 219 220 public void delete(Contact contact){ 221 contactDao.delete(contact.getId()); 222 } 223 224 public List getAll(){ 225 return contactDao.findAll(); 226 } 227 228 public Contact getById(Integer id){ 229 return contactDao.getById(id); 230 } 231 232 public void update(Contact contact){ 233 contactDao.update(contact); 234 } 235} 236 237 238DataSourceMethodDefinitionSourceEditor 239 240/**//* 241 * @(#)DataSourceMethodDefinitionSourceEditor.java 242 * 243 * Copyright 2003 Copyright.com , Inc. All rights reserved. 244 * Use is subject to license terms. 245 */ 246package sample.util; 247 248import net.sf.acegisecurity.intercept.method.MethodDefinitionMap; 249import net.sf.acegisecurity.ConfigAttributeEditor; 250import net.sf.acegisecurity.ConfigAttributeDefinition; 251 252import java.util.List; 253import java.util.Iterator; 254import java.util.Map; 255import java.beans.PropertyEditorSupport; 256 257import org.springframework.jdbc.core.support.JdbcDaoSupport; 258import org.springframework.jdbc.core.JdbcTemplate; 259 260 261public class DataSourceMethodDefinitionSourceEditor extends PropertyEditorSupport{ 262 protected JdbcTemplate jdbcTemplate; 263 //~ Methods ================================================================ 264 public JdbcTemplate getJdbcTemplate(){ 265 return jdbcTemplate; 266 } 267 268 public void setJdbcTemplate(JdbcTemplate jdbcTemplate){ 269 this.jdbcTemplate=jdbcTemplate; 270 } 271 272 public void setAsText(String s) throws IllegalArgumentException{ 273 MethodDefinitionMap source=new MethodDefinitionMap(); 274 List rs; 275 try{ 276 rs=jdbcTemplate.queryForList(s); 277 } catch(Exception e){ 278 setValue(source); 279 e.printStackTrace(); 280 return; 281 } 282 if((rs==null)||rs.size()==0){ 283 // Leave value in property editor null 284 } else{ 285 // Now we have properties, process each one individually 286 ConfigAttributeEditor configAttribEd=new ConfigAttributeEditor(); 287 288 for(Iterator iter=rs.iterator();iter.hasNext();){ 289 Map row=(Map)iter.next(); 290 String authority=(String)row.get("AUTHORITY"); 291 String resource=(String)row.get("PROTECTED_RES"); 292 if((null==authority)||(resource==null)){ 293 continue; 294 } 295 // Convert value to series of security configuration attributes 296 configAttribEd.setAsText(authority); 297 ConfigAttributeDefinition attr=(ConfigAttributeDefinition)configAttribEd .getValue(); 298 // Register name and attribute 299 source.addSecureMethod(resource, attr); 300 } 301 } 302 setValue(source); 303 } 304 305} 306 307 308Contact : 309 310package sample; 311 312import java.io.Serializable; 313/** *//** 314 * @author @2006-11-10">cuizhen@risesoft.net">@2006-11-10 上午10:34:01 315 */ 316public class Contact implements Serializable{ 317 //~ Instance fields ======================================================== 318 private Integer id; 319 private String email; 320 private String name; 321 //~ Constructors =========================================================== 322 323 public Contact(String name, String email){ 324 this.name=name; 325 this.email=email; 326 } 327 328 public Contact(){ 329 } 330 //~ Methods ================================================================ 331 332 /** *//** 333 * DOCUMENT ME! 334 * 335 * @param email The email to set. 336 */ 337 public void setEmail(String email){ 338 this.email=email; 339 } 340 341 /** *//** 342 * DOCUMENT ME! 343 * 344 * @return Returns the email. 345 */ 346 public String getEmail(){ 347 return email; 348 } 349 350 public void setId(Integer id){ 351 this.id=id; 352 } 353 354 /** *//** 355 * DOCUMENT ME! 356 * 357 * @return Returns the id. 358 */ 359 public Integer getId(){ 360 return id; 361 } 362 363 /** *//** 364 * DOCUMENT ME! 365 * 366 * @param name The name to set. 367 */ 368 public void setName(String name){ 369 this.name=name; 370 } 371 372 /** *//** 373 * DOCUMENT ME! 374 * 375 * @return Returns the name. 376 */ 377 public String getName(){ 378 return name; 379 } 380 381 public String toString(){ 382 StringBuffer sb=new StringBuffer(); 383 sb.append(super.toString()+": "); 384 sb.append("Id: "+this.getId()+"; "); 385 sb.append("Name: "+this.getName()+"; "); 386 sb.append("Email: "+this.getEmail()); 387 388 return sb.toString(); 389 } 390} 391 392 393log4j: 394 395# Global logging configuration 396log4j.rootLogger=WARN, stdout, fileout 397# Console output 398log4j.appender.stdout=org.apache.log4j.ConsoleAppender 399log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 400log4j.appender.stdout.layout.conversionPattern=[%p,%c{1},%t] %m%n 401 402# Rolling log file output 403log4j.appender.fileout=org.apache.log4j.RollingFileAppender 404log4j.appender.fileout.File=contacts.log 405#log4j.appender.fileout.File=${webapp.root}/WEB-INF/log4j.log 406log4j.appender.fileout.MaxFileSize=1024KB 407log4j.appender.fileout.MaxBackupIndex=1 408log4j.appender.fileout.layout=org.apache.log4j.PatternLayout 409log4j.appender.fileout.layout.conversionPattern=%d{ABSOLUTE} %5p %c{1},%t:%L - %m%n 410 411 412 累死了……下面是配置xml 1WebRoot/WEB-INF/applicationContext-basic.xml 2 3 4 5 6 7 8<?xml version="1.0" encoding="UTF-8"?> 9 10 11<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 12 13 14 15 16 17 18<beans> 19 20 21 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> 22 23 24 <property name="driverClass"> 25 26 27 <value>com.mysql.jdbc.Driver</value> 28 29 30 </property> 31 32 33 <property name="jdbcUrl"> 34 35 36 <value>jdbc:mysql://localhost:3306/tt</value> 37 38 39 </property> 40 41 42 <property name="user"> 43 44 45 <value>root</value> 46 47 48 </property> 49 50 51 <property name="password"> 52 53 54 <value>123</value> 55 56 57 </property> 58 59 60 <property name="initialPoolSize"> 61 62 63 <value>10</value> 64 65 66 </property> 67 68 69 <property name="minPoolSize"> 70 71 72 <value>10</value> 73 74 75 </property> 76 77 78 <property name="maxPoolSize"> 79 80 81 <value>50</value> 82 83 84 </property> 85 86 87 <property name="checkoutTimeout"> 88 89 90 <value>5000</value> 91 92 93 </property> 94 95 96 <property name="maxIdleTime"> 97 98 99 <value>1800</value> 100 101 102 </property> 103 104 105 <property name="idleConnectionTestPeriod"> 106 107 108 <value>3000</value> 109 110 111 </property> 112 113 114 <property name="acquireIncrement"> 115 116 117 <value>5</value> 118 119 120 </property> 121 122 123 </bean> 124 125 126 127 128 129 130 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 131 132 133 <property name="dataSource"> 134 135 136 <ref local="dataSource"/> 137 138 139 </property> 140 141 142 </bean> 143 144 145 146 147 148 149 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> 150 151 152 <property name="dataSource"> 153 154 155 <ref bean="dataSource"/> 156 157 158 </property> 159 160 161 </bean> 162 163 164 165 166 167 168 <bean id="businessAccessDecisionManager" class="net.sf.acegisecurity.vote.AffirmativeBased"> 169 170 171 <property name="allowIfAllAbstainDecisions"><value>false</value></property> 172 173 174 <property name="decisionVoters"> 175 176 177 <list> 178 179 180 <ref bean="roleVoter"/> 181 182 183 </list> 184
|