一个简单的网站登陆注册web项目Demo,基于最新版的IDEA15+Maven3+Spring4.2.5+SpringMVC4.2.5+SpringJDBC4.2.5实现
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.zzkungroupId>
<artifactId>MySpringartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>1.8maven.compiler.source>
<maven.compiler.target>1.8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.apache.tomcatgroupId>
<artifactId>tomcat-catalinaartifactId>
<version>8.0.30version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-contextartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-webmvcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-jdbcartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.springframeworkgroupId>
<artifactId>spring-testartifactId>
<version>4.2.5.RELEASEversion>
dependency>
<dependency>
<groupId>org.aspectjgroupId>
<artifactId>aspectjweaverartifactId>
<version>1.8.9version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>5.1.38version>
dependency>
<dependency>
<groupId>com.mchangegroupId>
<artifactId>c3p0artifactId>
<version>0.9.5.2version>
dependency>
<dependency>
<groupId>jstlgroupId>
<artifactId>jstlartifactId>
<version>1.2version>
dependency>
dependencies>
project>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<servlet>
<servlet-name>springmvcservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:springmvc-servlet.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvcservlet-name>
<url-pattern>/url-pattern>
servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>utf-8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>CharacterEncodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:property-placeholder location="classpath:jdbc.properties"/>
<context:component-scan base-package="com.zzkun.controller"/>
<context:component-scan base-package="com.zzkun.service"/>
<context:component-scan base-package="com.zzkun.dao"/>
<mvc:annotation-driven/>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="minPoolSize" value="5"/>
<property name="maxPoolSize" value="30"/>
<property name="initialPoolSize" value="10"/>
<property name="maxIdleTime" value="60"/>
<property name="acquireIncrement" value="5"/>
<property name="maxStatements" value="0"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="acquireRetryAttempts" value="30"/>
<property name="breakAfterAcquireFailure" value="true"/>
<property name="testConnectionOnCheckout" value="false"/>
bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="dataSource" />
<aop:config proxy-target-class="true">
<aop:pointcut id="serviceMethod"
expression=" execution(* com.zzkun.service..*(..))" />
<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
aop:config>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" />
tx:attributes>
tx:advice>
beans>
jdbc.driverClassName = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/mytest1?characterEncoding=utf8&useSSL=true
jdbc.username = root
jdbc.password = ******
package com.zzkun.model;
import java.io.Serializable;
import java.io.SerializablePermission;
/**
* Created by kun on 2016/5/5.
*/
public class User implements Serializable {
private int id;
private String username;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.zzkun.dao;
import com.zzkun.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
/**
* Repository用于标注数据访问组件,即DAO组件
* Created by kun on 2016/5/5.
*/
@Repository
public class UserDao {
@Autowired
private JdbcTemplate jdbcTemplate;
public void register(User user) {
String sql = "insert into user(username, password) values(?, ?)";
jdbcTemplate.update(sql, user.getUsername(), user.getPassword());
}
public User findUserByUserName(String userName) {
String sql = "select id, username, password from user where username=?";
User user = new User();
jdbcTemplate.query(sql, new Object[]{userName}, new RowCallbackHandler() {
@Override
public void processRow(ResultSet resultSet) throws SQLException {
user.setId(resultSet.getInt("id"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
}
});
return user;
}
}
package com.zzkun.service;
import com.zzkun.dao.UserDao;
import com.zzkun.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Service用于标注业务层组件
* Created by kun on 2016/5/5.
*/
@Service
public class UserService {
@Autowired
private UserDao userDao;
public boolean register(User user) {
User u = userDao.findUserByUserName(user.getUsername());
if(u.getId() == 0) {
userDao.register(user);
return true;
}
return false;
}
public boolean loginCheck(User user) {
User u = userDao.findUserByUserName(user.getUsername());
return user.getPassword().equals(u.getPassword());
}
}
package com.zzkun.controller;
import com.zzkun.model.User;
import com.zzkun.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* Controller用于标注控制层组件
* Created by kun on 2016/5/5.
*/
@Controller
@RequestMapping(value = "/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/loginView")
public String loginView(){
return "login.jsp";
}
@RequestMapping("/login")
public ModelAndView login(User user){
ModelAndView mav = new ModelAndView();
System.out.println(user.getUsername() + " " + user.getPassword());
if(!userService.loginCheck(user)) {
mav.setViewName("login.jsp");
mav.addObject("errorMsg", "用户名或密码错误!");
} else {
mav.setViewName("success.jsp");
mav.addObject("user", user);
}
return mav;
}
}
package com.zzkun.service;
import com.zzkun.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import static org.junit.Assert.*;
/**
* Created by kun on 2016/5/5.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:springmvc-servlet.xml")
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void register() throws Exception {
User user = new User();
user.setUsername("zzk");
user.setPassword("123456");
assertEquals(true, userService.register(user));
}
@Test
public void loginCheck() throws Exception {
User user = new User();
user.setUsername("zzk");
user.setPassword("123456");
assertEquals(true, userService.loginCheck(user));
user.setPassword("111111");
assertEquals(false, userService.loginCheck(user));
}
}
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
Created by IntelliJ IDEA.
User: kun
Date: 2016/5/5
Time: 16:18
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>用户登录title>
head>
<body>
<c:if test="${!empty errorMsg}">
<h3>${errorMsg}h3>
c:if>
<form action="login" method="post">
<h2>欢迎登陆h2>
<p>
<label for="username">用户名label>
<input type="text" id="username" name="username">
p>
<p>
<label for="password">密码label>
<input type="password" id="password" name="password">
p>
<p>
<input type="submit" value="登陆">
p>
form>
body>
html>
<%--
Created by IntelliJ IDEA.
User: kun
Date: 2016/5/5
Time: 16:23
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>登陆成功title>
head>
<body>
<h3>恭喜${user.username}, 登陆成功!h3>
body>
html>
https://github.com/kun368/MySpring