User实体类:
public class User { private String uid;//id主键 private String loginname;//登录名 private String loginpass;//登录密码 private String email;//邮箱 private Integer status;//激活状态 private String activationCode;//激活码,唯一的 private String gender;//性别 private String nickname;//昵称 private String phone;//电话 // 提供set/get方法
userDao
public interface IUserDao { //登录 User login(String username,String password) throws Exception; //姓名是否存在 int findName(String name) throws Exception; //注册 int register(User user) throws Exception; //查询所有信息 List<User> findAll() throws Exception; //根据姓名删除 int delUser(String name) throws Exception;
userDaoImpl
public class UserDaoImpl implements IUserDao { private QueryRunner runner; //提供set方法,注入属性 public void setRunner(QueryRunner runner) { this.runner = runner; } //登录 @Override public User login(String username, String password) throws Exception { return runner.query("SELECT * FROM T_USER where loginname = ? and loginpass = ?",
new BeanHandler<User>( User.class),username,password); } //判断姓名是否存在 @Override public int findName(String name) throws Exception { String sql ="SELECT count(1) n FROM T_USER WHERE loginname = ?"; Object query = runner.query(sql , new ScalarHandler<Object>("n"),name); return Integer.valueOf(query.toString()); } //注册 @Override public int register(User user) throws Exception { Object[] params = {user.getUid(),user.getLoginname(),user.getLoginpass(),user.getEmail(), user.getStatus(),user.getActivationCode(),user.getGender(),user.getNickname(),user.getPhone()}; return runner.update("insert into t_user values(?,?,?,?,?,?,?,?,?)",params); } //查询所有用户信息 @Override public List<User> findAll() throws Exception { return runner.query("select * from t_user",new BeanListHandler<User>(User.class)); } //删除 @Override public int delUser(String name) throws Exception { return runner.update("delete from t_user where loginname = ?",name); }
userService
public interface IUserService { User login(String username,String password) throws Exception; boolean findName(String name) throws Exception; boolean register(User user) throws Exception; List<User> findAll() throws Exception; int delUser(String name) throws Exception;
userServiceImpl
public class UserServiceImpl implements IUserService { private IUserDao userDao; //提供set方法,注入属性 public void setUserDao(IUserDao userDao) { this.userDao = userDao; } @Override public User login(String username, String password) throws Exception { return userDao.login(username, password); } @Override public boolean findName(String name) throws Exception { return userDao.findName(name)>0?true:false; } @Override public boolean register(User user) throws Exception { return userDao.register(user)>0?true:false; } @Override public List<User> findAll() throws Exception { return userDao.findAll(); } @Override public int delUser(String name) throws Exception { return userDao.delUser(name); }
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:p="http://www.springframework.org/schema/p" 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" 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"> <!-- 带连接池的数据源: c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 通过set方法赋值 --> <property name="driverClass" value="oracle.jdbc.OracleDriver"/> <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:orcl"/> <property name="user" value="yosebook"/> <property name="password" value="123"/> </bean> <!-- 给Dbutils工具类,注入添加数据源 --> <bean id="queryRunner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> <!-- 通过有参构造方法 --> <constructor-arg name="ds" ref="dataSource"/> </bean> <!-- 给UserDaoImpl注入值 --> <bean id="userDao" class="com.zl.spring.dao.impl.UserDaoImpl"> <property name="runner" ref="queryRunner"/> </bean> <!-- 给UserServiceImpl注入值 --> <bean id="userService" class="com.zl.spring.service.impl.UserServiceImpl"> <property name="userDao" ref="userDao"></property> </bean> </beans>