包结构
所需要的jar包直接拷贝到lib目录下
然后选定 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注解 [email protected] @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
测试成功
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/15650.html