http://www.open-open.com/lib/view/open1349272132291.html
SpringMVC +Spring+ SpringJDBC整合实例。文件结构:


3S3(3Spring3.x)的整合大致如下:
1)web.xml的配置:
01 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
02 |
< web-app xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version = "3.0" > |
03 |
< display-name ></ display-name > |
05 |
< welcome-file >index.jsp</ welcome-file > |
08 |
< listener-class >org.springframework.web.util.IntrospectorCleanupListener</ listener-class > |
11 |
< servlet-name >spring</ servlet-name > |
12 |
< servlet-class >org.springframework.web.servlet.DispatcherServlet</ servlet-class > |
14 |
< param-name >contextConfigLocation</ param-name > |
15 |
< param-value >classpath:*applicationContext.xml</ param-value > |
17 |
< load-on-startup >1</ load-on-startup > |
20 |
< servlet-name >spring</ servlet-name > |
21 |
< url-pattern >*.do</ url-pattern > |
25 |
< filter-name >setcharacter</ filter-name > |
26 |
< filter-class >org.springframework.web.filter.CharacterEncodingFilter</ filter-class > |
28 |
< param-name >encoding</ param-name > |
29 |
< param-value >utf-8</ param-value > |
33 |
< filter-name >setcharacter</ filter-name > |
34 |
< url-pattern >/*</ url-pattern > |
2)applicationContext.xml的配置:
001 |
<? xml version = "1.0" encoding = "UTF-8" ?> |
002 |
< beans xmlns = "http://www.springframework.org/schema/beans" |
003 |
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns:p = "http://www.springframework.org/schema/p" |
004 |
xmlns:context = "http://www.springframework.org/schema/context" |
005 |
xmlns:aop = "http://www.springframework.org/schema/aop" xmlns:tx = "http://www.springframework.org/schema/tx" |
006 |
xsi:schemaLocation="http://www.springframework.org/schema/beans |
007 |
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd |
008 |
http://www.springframework.org/schema/context |
009 |
http://www.springframework.org/schema/context/spring-context-3.0.xsd |
010 |
http://www.springframework.org/schema/tx |
011 |
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd |
012 |
http://www.springframework.org/schema/aop |
013 |
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> |
015 |
< context:property-placeholder location = "classpath:jdbc.properties" /> |
018 |
< context:component-scan base-package = "com.controller" /> |
019 |
< context:component-scan base-package = "com.service" /> |
020 |
< context:component-scan base-package = "com.dao" /> |
023 |
< bean id = "dataSource" class = "com.mchange.v2.c3p0.ComboPooledDataSource" |
024 |
destroy-method = "close" > |
025 |
< property name = "driverClass" > |
026 |
< value >${jdbc.driverClassName}</ value > |
028 |
< property name = "jdbcUrl" > |
029 |
< value >${jdbc.url}</ value > |
031 |
< property name = "user" > |
032 |
< value >${jdbc.username}</ value > |
034 |
< property name = "password" > |
035 |
< value >${jdbc.password}</ value > |
038 |
< property name = "minPoolSize" > |
042 |
< property name = "maxPoolSize" > |
046 |
< property name = "initialPoolSize" > |
050 |
< property name = "maxIdleTime" > |
054 |
< property name = "acquireIncrement" > |
059 |
< property name = "maxStatements" > |
063 |
< property name = "idleConnectionTestPeriod" > |
067 |
< property name = "acquireRetryAttempts" > |
072 |
< property name = "breakAfterAcquireFailure" > |
077 |
< property name = "testConnectionOnCheckout" > |
083 |
< bean id = "jdbcTemplate" class = "org.springframework.jdbc.core.JdbcTemplate" > |
084 |
< property name = "dataSource" ref = "dataSource" ></ property > |
088 |
< bean id = "transactionManager" |
089 |
class = "org.springframework.jdbc.datasource.DataSourceTransactionManager" |
090 |
p:dataSource-ref = "dataSource" /> |
093 |
< aop:config proxy-target-class = "true" > |
094 |
< aop:pointcut id = "serviceMethod" |
095 |
expression = "
execution(* com.service..*(..))" /> |
096 |
< aop:advisor pointcut-ref = "serviceMethod" advice-ref = "txAdvice" /> |
098 |
< tx:advice id = "txAdvice" transaction-manager = "transactionManager" > |
100 |
< tx:method name = "*" /> |
106 |
class = "org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /> |
111 |
class = "org.springframework.web.servlet.view.InternalResourceViewResolver" |
112 |
p:viewClass = "org.springframework.web.servlet.view.JstlView" p:prefix = "/WEB-INF/jsp/" |
3)jdbc.properties的配置:
1 |
jdbc.driverClassName=com.mysql.jdbc.Driver |
4)log4j.properties的配置:
1 |
log4j.rootLogger=DEBUG,A1 |
2 |
#
\u8f93\u51fa\u5230\u63a7\u5236\u53f0 |
3 |
log4j.appender.A1=org.apache.log4j.ConsoleAppender |
4 |
log4j.appender.A1.layout=org.apache.log4j.PatternLayout |
5 |
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd
HH\:mm\:ss} [\u65E5\u5FD7\u4FE1\u606F] %m%n |
5)User.java
03 |
import java.io.Serializable; |
05 |
@SuppressWarnings ( "serial" ) |
06 |
public class User implements Serializable
{ |
09 |
private String
userName; |
10 |
private String
password; |
12 |
public String
getUserName() { |
15 |
public void setUserName(String
userName) { |
16 |
this .userName
= userName; |
18 |
public String
getPassword() { |
21 |
public void setPassword(String
password) { |
22 |
this .password
= password; |
27 |
public void setId( int id)
{ |
6)UserController.java
01 |
package com.controller; |
03 |
import org.springframework.beans.factory.annotation.Autowired; |
04 |
import org.springframework.stereotype.Controller; |
05 |
import org.springframework.web.bind.annotation.RequestMapping; |
06 |
import org.springframework.web.servlet.ModelAndView; |
08 |
import com.model.User; |
09 |
import com.service.UserService; |
12 |
public class UserComtroller
{ |
15 |
private UserService
userService; |
17 |
@RequestMapping ( "/user/loginView" ) |
18 |
public String
loginView(){ |
22 |
@RequestMapping ( "/user/registerView" ) |
23 |
public String
registerView(){ |
27 |
@RequestMapping ( "/user/login" ) |
28 |
public ModelAndView
login(User user){ |
29 |
ModelAndView
mav = new ModelAndView(); |
30 |
User
u = userService.loginCheck(user); |
32 |
mav.setViewName( "login" ); |
33 |
mav.addObject( "errorMsg" , "用户名或密码有误!" ); |
37 |
mav.setViewName( "success" ); |
38 |
mav.addObject( "user" ,
u); |
43 |
@RequestMapping ( "/user/register" ) |
44 |
public ModelAndView
register(User user){ |
45 |
ModelAndView
mav = new ModelAndView(); |
46 |
if (userService.register(user)){ |
47 |
mav.setViewName( "register_succ" ); |
51 |
mav.setViewName( "register" ); |
52 |
mav.addObject( "errorMsg" , "用户名已被占用,请更换!!" ); |
7)UserService Interface
6 |
public interface UserService
{ |
7 |
public boolean register(User
user); |
8 |
public User
loginCheck(User user); |
8)UserServiceImp.java
01 |
package com.service.implement; |
03 |
import org.springframework.beans.factory.annotation.Autowired; |
04 |
import org.springframework.stereotype.Service; |
06 |
import com.dao.UserDao; |
07 |
import com.model.User; |
08 |
import com.service.UserService; |
11 |
public class UserServiceImpl implements UserService
{ |
14 |
private UserDao
userDao; |
17 |
public User
loginCheck(User user) { |
18 |
User
u = userDao.findUserByUserName(user.getUserName()); |
19 |
System.out.println( "id=" +u.getId()+ ",
userName=" +u.getUserName()+ ",
password=" +u.getPassword()); |
20 |
if (user.getPassword().equals(u.getPassword())){ |
29 |
public boolean register(User
user) { |
30 |
User
u = userDao.findUserByUserName(user.getUserName()); |
32 |
userDao.register(user); |
36 |
System.out.println( "id=" +u.getId()+ ",
userName=" +u.getUserName()+ ",
password=" +u.getPassword()); |
9)UserDao Interface
5 |
public interface UserDao
{ |
6 |
public void register(User
user); |
7 |
public User
findUserByUserName( final String
userName); |
10)UserDaoImpl.java
01 |
package com.dao.implement; |
03 |
import java.sql.ResultSet; |
04 |
import java.sql.SQLException; |
06 |
import org.springframework.beans.factory.annotation.Autowired; |
07 |
import org.springframework.jdbc.core.JdbcTemplate; |
08 |
import org.springframework.jdbc.core.RowCallbackHandler; |
09 |
import org.springframework.stereotype.Repository; |
11 |
import com.dao.UserDao; |
12 |
import com.model.User; |
15 |
public class UserDaoImpl implements UserDao
{ |
18 |
private JdbcTemplate
jdbcTemplate; |
21 |
public void register(User
user) { |
22 |
String
sqlStr = "insert
into user(uname,pwd) values(?,?)" ; |
23 |
Object[]
params = new Object[]{user.getUserName(),user.getPassword()}; |
24 |
jdbcTemplate.update(sqlStr,
params); |
28 |
public User
findUserByUserName(String userName) { |
29 |
String
sqlStr = "select
id,uname,pwd from user where uname=?" ; |
30 |
final User
user = new User(); |
31 |
jdbcTemplate.query(sqlStr, new Object[]{userName}, new RowCallbackHandler()
{ |
33 |
public void processRow(ResultSet
rs) throws SQLException
{ |
34 |
user.setId(rs.getInt( "id" )); |
35 |
user.setUserName(rs.getString( "uname" )); |
36 |
user.setPassword(rs.getString( "pwd" )); |
11)单元测试:test/com.service.implement
-UserServiceImplTest.java
01 |
package com.service.implement; |
03 |
import org.junit.Test; |
04 |
import org.junit.runner.RunWith; |
05 |
import org.springframework.beans.factory.annotation.Autowired; |
06 |
import org.springframework.test.context.ContextConfiguration; |
07 |
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
09 |
import com.model.User; |
10 |
import com.service.UserService; |
13 |
@RunWith (SpringJUnit4ClassRunner. class ) |
14 |
@ContextConfiguration (locations= "/applicationContext.xml" ) |
15 |
public class UserServiceTest
{ |
18 |
private UserService
userService; |
21 |
public void testLoginCheck(){ |
22 |
User
user = new User(); |
23 |
user.setUserName( "manager2" ); |
24 |
user.setPassword( "123" ); |
25 |
if ( null !=userService.loginCheck(user)) |
26 |
System.out.println( "------OK!!-----" ); |
28 |
System.out.println( "------Sorry!!-----" ); |
32 |
public void testRegister(){ |
33 |
User
user = new User(); |
34 |
user.setUserName( "manager" ); |
35 |
user.setPassword( "123" ); |
36 |
System.out.println(userService.register(user)); |
12)测试结果:

