新建web工程spring-netcross 1.导入spring文件夹里的七个jar包,另外还要导入jdbc-lib的四个jar包,ojdbc.jar,commoms-pool,commons-dbcp-1.2.1.jar,commons-collections4-4.0.jar
2.复制springmvc02下的spring-mvc.xml文件,新建com.tarena.oss包
3.com.tarena.oss包下新建entity包,新建Admin这个实体类,属性get/set方法,以及toString方法(方便测试输出)
package com.tarena.oss.entity;
import java.io.Serializable; import java.sql.Timestamp;
public class Admin implements Serializable{ private Integer adminId; private String adminCode; private String password; private String name; private String telephone; private String email; private Timestamp enrolldate; public Integer getAdminId() { return adminId; } public void setAdminId(Integer adminId) { this.adminId = adminId; } public String getAdminCode() { return adminCode; } public void setAdminCode(String adminCode) { this.adminCode = adminCode; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTelephone() { return telephone; } public void setTelephone(String telephone) { this.telephone = telephone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public Timestamp getEnrolldate() { return enrolldate; } public void setEnrolldate(Timestamp enrolldate) { this.enrolldate = enrolldate; } @Override public String toString() { return "Admin [adminId=" + adminId + ", adminCode=" + adminCode + ", password=" + password + ", name=" + name + ", telephone=" + telephone + ", email=" + email + ", enrolldate=" + enrolldate + "]"; }
}
4.com.tarena.oss包下新建dao包,新建AdminDAO接口,添加findByAdminCode方法,再新建dao包下的jdbc包,新建AdminDAOImpl实现接口
AdminDAO接口:
package com.tarena.oss.dao;
import com.tarena.oss.entity.Admin;
public interface AdminDAO { public Admin findByAdminCode(String adminCode); }
AdminDAOImpl实现接口:
package com.tarena.oss.dao.jdbc;
import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException;
import javax.annotation.Resource; import javax.sql.DataSource;
import org.springframework.stereotype.Repository;
import com.tarena.oss.dao.AdminDAO; import com.tarena.oss.entity.Admin;
@Repository("adminDAO") public class AdminDAOImpl implements AdminDAO{
/*数据源与连接池的关系 * * 数据源负责管理连接池*/
@Resource(name="ds") private DataSource ds;
public Admin findByAdminCode(String adminCode) { if(adminCode==null){ return null; } Connection conn = null; try { conn = ds.getConnection(); String sql = "select * from admin_info_lch where admin_code=?"; PreparedStatement ps = conn.prepareStatement(sql); ps.setString(1, adminCode); ResultSet rs = ps.executeQuery();
if(rs.next()){ Admin admin = new Admin(); admin.setAdminId(rs.getInt("admin_id")); admin.setAdminCode(rs.getString("admin_code")); admin.setPassword(rs.getString("password")); admin.setName(rs.getString("name")); admin.setTelephone(rs.getString("telephone")); admin.setEmail(rs.getString("email")); admin.setEnrolldate(rs.getTimestamp("enrolldate"));
return admin; } } catch (Exception e) {
e.printStackTrace(); throw new RuntimeException("根据编码查询管理员失败"); }finally{ try { conn.close(); } catch (SQLException e) {
e.printStackTrace(); } } return null; }
}
5.复制netctoss项目下的db.properties文件,到该项目下的src下
driver=oracle.jdbc.OracleDriver url=jdbc:oracle:thin:@localhost:1521:xe user=system pwd=1234
6.修改spring-mvc.xml文件,使用集合的方式读取db.properties文件的内容
<!-- 配置组件扫描 --> <context:component-scan base-package="com.tarena.oss" />
<!-- 配置spring mvc注解扫描 --> <mvc:annotation-driven/>
<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean>
<!-- 读取db.properties文件的内容 --> <util:properties > </util:properties>
7.新建test包,新建测试类test1用于测试读取db.properties文件的内容是否成功。
@Test //测试db.properties文件是否正确读取 public void test1(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml"); System.out.println(ac.getBean("jdbc")); }
8.在AdminDAOImpl添加成员变量数据源DadaSource ds,并且使用@Resource(name="ds")注入该属性。同时,在AdminDAOImpl这个类前面添加@Repository("adminDAO")。
9.在findByAdminCode方法中添加代码,根据管理员账号查询管理员。
10.在spring-xml中添加ds的参数配置。bean的id为ds是根据@Resource(name="ds")命名为ds的。然后根据db.properties为ds配置相关参数。
xml中修改代码如下:
<!-- 配置组件扫描 --> <context:component-scan base-package="com.tarena.oss" />
<!-- 配置spring mvc注解扫描 --> <mvc:annotation-driven/>
<!-- 配置视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/"/> <property name="suffix" value=".jsp"/> </bean>
<!-- 读取db.properties文件的内容 --> <util:properties /> </bean>
11.组件扫描中检查包名是否为com.tarena.oss,然后对AdminDAOImpl这个类中的方法进行测试。因为此时还没有涉及到springMVC模式(无controller类),故spring-mvc.xml不会自动读取,我们需要在测试代码中写上开启容器的代码。
@Test public void test2(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml"); AdminDAO dao = ac.getBean("adminDAO",AdminDAO.class); Admin admin = dao.findByAdminCode("caocao"); System.out.println(admin); }
12..com.tarena.oss包下新建Service包,在新建LoginService接口,添加checkLogin方法。再新建LoginServiceImpl实现类,实现该方法。首先添加注解@Service("loginService"),再在类中添加dao成员变量,并使用@Resource(name="daminDAO")注解,name是参考AdminDAOImpl类,它最开始就被注解为@Repository("adminDAO")。调用dao的方法,如果管理员为空或者密码错误,则抛出自定义异常,否则返回该admin。所以,此时,在Service包下新建spring包,再新建ApplicationException类。
LoginService 接口:
package com.tarena.oss.Service;
import com.tarena.oss.Service.spring.ApplicationException; import com.tarena.oss.entity.Admin; import com.tarena.oss.entity.ResultData;
/* * 业务层接口 * */ public interface LoginService {
public Admin checkLogin(String adminCode,String pwd) throws ApplicationException;
}
LoginServiceImpl实现类:
package com.tarena.oss.Service;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.tarena.oss.Service.spring.ApplicationException; import com.tarena.oss.dao.AdminDAO; import com.tarena.oss.entity.Admin; import com.tarena.oss.entity.ResultData;
/* * 业务层的实现 * */ @Service("loginService") public class LoginServiceImpl implements LoginService {
@Resource(name="adminDAO") private AdminDAO dao;
public Admin checkLogin(String adminCode, String pwd) throws ApplicationException{
System.out.println("checkLogin方法"); //保持好习惯,先定义为null,再初始化。 Admin admin = null;
admin = dao.findByAdminCode(adminCode); try{ if(admin==null){ throw new ApplicationException("账号不存在"); } if(!admin.getPassword().equals(pwd)){ throw new ApplicationException("密码错误"); } }catch(Exception e){ e.printStackTrace();
} return admin; } }
13.在测试中写test3,测试loginService
@Test public void test3(){ ApplicationContext ac = new ClassPathXmlApplicationContext("spring-mvc.xml"); LoginService service = ac.getBean("loginService",LoginService.class); Admin admin = null; try { admin = service.checkLogin("caocao", "123"); } catch (ApplicationException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(admin); }
14.复制netctoss中的styles文件夹,复制styles文件夹到WebRoot下。
15.在WebRoot/WEB-INF下新建login.jsp(拷贝原来netctoss项目做相应修改),添加<form></form>,修改<input>中的name,修改其代码提交表单时采用js脚本。
|
请发表评论