noteless 头像

springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试

ab017006-a7d7-4215-ad2b-f396e6b74710[4]

包结构

305138d6-86ce-4359-9e92-85d0fe86e2e0[4]

所需要的jar包直接拷贝到lib目录下

dc3b5990-dc54-4bde-81d2-48ebae3bfb19[4]

然后选定 build path 

之后开始写项目代码

配置文件

ApplicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<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:p="http://www.springframework.org/schema/p"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">

 

 

 

<!--扫描类包,标注spring注解的类自动转换成bean,同时完成bean的注入 (这样类中定义的spring注解 比如@repository @autowired等才能起作用) -->

<context:component-scan base-package="com.bbs.dao" />

 

<!--扫描service包,应用spring注解配置 -->

<context:component-scan base-package="com.bbs.service" />

 

<!--配置事务管理器 -->

<bean id= "transactionManager"

class="org.springframework.jdbc.datasource.DataSourceTransactionManager"

p:dataSource-ref="dataSource"

/>

 

<!-- 通过AOP配置提供事务增强,让service包下所有的bean的所有方法拥有事务 -->

<aop:config proxy-target-class="true">

<aop:pointcut id="serviceMethod"

expression="execution(* com.bbs.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>

 

<!-- 定义一个使用DBCP实现的数据源 -->

<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"

destroy-method="close"

p:driverClassName="com.mysql.jdbc.Driver"

p:url="jdbc:mysql://localhost:3306/sampledb"

p:username="root"

p:password="123456"/>

 

<!--定义jdbc模板bean -->

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"

p:dataSource-ref="dataSource"/>

 

 

</beans>

 

两个实体

User.java

package com.bbs.domain;

 

import java.io.Serializable;

import java.util.Date;

 

public class User implements Serializable{

 

/**

*

*/

private static final long serialVersionUID = 1L;

private int userId;

private String userName;

private String password;

private int credits;

private String lastIp;

private Date lastVisit;

 

 

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

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;

}

public int getCredits() {

return credits;

}

public void setCredits(int credits) {

this.credits = credits;

}

public String getLastIp() {

return lastIp;

}

public void setLastIp(String lastIp) {

this.lastIp = lastIp;

}

public Date getLastVisit() {

return lastVisit;

}

public void setLastVisit(Date lastVisit) {

this.lastVisit = lastVisit;

}

 

 

}

 

LoginLog.java

package com.bbs.domain;

 

import java.io.Serializable;

import java.util.Date;

 

public class LoginLog implements Serializable{

/**

*

*/

private static final long serialVersionUID = 1L;

private int loginLogId;

private int userId;

private String ip;

private Date loginDate;

public int getLoginLogId() {

return loginLogId;

}

public void setLoginLogId(int loginLogId) {

this.loginLogId = loginLogId;

}

public int getUserId() {

return userId;

}

public void setUserId(int userId) {

this.userId = userId;

}

public String getIp() {

return ip;

}

public void setIp(String ip) {

this.ip = ip;

}

public Date getLoginDate() {

return loginDate;

}

public void setLoginDate(Date loginDate) {

this.loginDate = loginDate;

}

 

 

}

 

两个dao(用的jdbcTemplate)

LoginLogDao.java

package com.bbs.dao;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Repository;

 

import com.bbs.domain.LoginLog;

 

@Repository

public class LoginLogDao {

 

@Autowired

private JdbcTemplate jdbcTemplate;

 

public void insertLoginLog(LoginLog loginLog){

 

String sqlString ="INSERT INTO t_login_log(user_id,ip,login_datetime)"

+"VALUE(?,?,?)";

 

Object[] args={loginLog.getUserId(),loginLog.getIp(),loginLog.getLoginDate()};

 

jdbcTemplate.update(sqlString, args);

}

}

 

 

UserDao.java

package com.bbs.dao;

 

import java.sql.ResultSet;

import java.sql.SQLException;

 

import org.omg.CORBA.PUBLIC_MEMBER;

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 com.bbs.domain.User;

 

@Repository

public class UserDao {

 

@Autowired

private JdbcTemplate jdbcTemplate;

 

public int getMatchCount(String userName,String password){

 

String sqlString="SELECT count(*) FROM t_user "

+"WHERE user_name=? and password=?";

return jdbcTemplate.queryForObject(sqlString,new Object[]{ userName,password}, java.lang.Integer.class);

 

}

 

 

public User findUserByUserName(final String userName){

String sqlString ="SELECT user_id,user_name,credits "

+"FROM t_user WHERE user_name=?";

final User user = new User();

jdbcTemplate.query(sqlString, new Object[]{userName},

new RowCallbackHandler() {

 

@Override

public void processRow(ResultSet rs) throws SQLException {

// TODO Auto-generated method stub

user.setUserId(rs.getInt("user_id"));

user.setUserName(userName);

user.setCredits(rs.getInt("credits"));

 

}

});

 

return user;

 

}

 

public void updateLoginInfo(User user){

String sqlString="UPDATE t_user SET last_visit=?,last_ip=?,credits=?"

+"WHERE user_id=?";

jdbcTemplate.update(sqlString,new Object[]{user.getLastVisit(),user.getLastIp(),user.getCredits(),user.getUserId()});

 

}

 

}

 

还有一个service

UserService.java

 

package com.bbs.service;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.bbs.dao.LoginLogDao;

import com.bbs.dao.UserDao;

import com.bbs.domain.LoginLog;

import com.bbs.domain.User;

 

@Service

public class UserService {

 

@Autowired

private UserDao userDao;

 

@Autowired

private LoginLogDao loginLogDao;

 

public boolean hasMatchUser(String userName,String password){

int matchCount = userDao.getMatchCount(userName, password);

return matchCount > 0 ;

}

 

 

public User findUserByUserName(String userName){

return userDao.findUserByUserName(userName);

 

}

 

public void loginSucess(User user){

 

user.setCredits(5+user.getCredits());

 

 

LoginLog loginLog = new LoginLog();

 

loginLog.setUserId(user.getUserId());

loginLog.setIp(user.getLastIp());

loginLog.setLoginDate(user.getLastVisit());

 

 

userDao.updateLoginInfo(user);

loginLogDao.insertLoginLog(loginLog);

 

}

}

 

至此后台代码完毕

增加一个测试函数

TestUserService.java

 

package test.bbs.service;

 

import static org.junit.Assert.*;

 

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 com.bbs.domain.User;

import com.bbs.service.UserService;

 

@RunWith(SpringJUnit4ClassRunner.class)

@ContextConfiguration(locations={"/applicationContext.xml"})

 

public class TestUserService {

 

@Autowired

private UserService userService;

 

@Test

public void hasMAtchUser(){

boolean b1 = userService.hasMatchUser("admin", "123456");

boolean b2 = userService.hasMatchUser("admin", "11111");

 

assertTrue(b1);

//assertTrue(b2);

 

}

 

@Test

public void findUserByUserName(){

User user = userService.findUserByUserName("admin");

assertEquals(user.getUserName(),"admin");

}

 

}

 

运行测试,run  as JUnit test

测试成功

 

spring原理 实践解析-简单的helloworld

spring原理案例-基本项目搭建 01 spring framework 下载 官网下载spring jar包

spring原理案例-基本项目搭建 02 spring jar包详解 spring jar包的用途

spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例

springmvc整合mybatis完整项目示例

springmvc 项目完整示例01 需求与数据库表设计 简单的springmvc应用实例 web项目

springmvc 项目完整示例02 项目创建-eclipse创建动态web项目 配置文件 junit单元测试

springmvc 项目完整示例03 小结

springmvc 项目完整示例04 整合mybatis mybatis所需要的jar包 mybatis配置文件 sql语句 mybatis应用

springmvc 项目完整示例05  日志 --log4j整合 配置 log4j属性设置 log4j 配置文件 log4j应用

springmvc 项目完整示例06 日志–log4j 参数详细解析 log4j如何配置

springmvc 项目完整示例07 设置配置整合springmvc springmvc所需jar包springmvc web.xml文件配置

springmvc 项目完整示例08 前台页面以及知识点总结

maven项目整合springmvc整合mybatis

eclipse 创建maven 项目 动态web工程完整示例

eclipse 创建maven 项目 动态web工程完整示例 maven 整合springmvc整合

posted @ 2016-02-24 16:34  noteless  阅读(...)  评论(...编辑  收藏