package dao.impl;
import dao.UserDao;
import domain.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.sql.SQLException;
import java.util.List;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Autowired//自动按照类型注入数据
private QueryRunner runner;
@Override
public List<User> findAllUser() {
List<User> list = null;
try {
list = runner.query("select * from tab_user ",new BeanListHandler<User>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public User findUserById(int uid) {
User u = null;
try {
u = (User) runner.query("select * from tab_user where uid = ?",new BeanHandler<User>(User.class),uid);
} catch (SQLException e) {
e.printStackTrace();
}
return u;
}
@Override
public void saveUser(User user) {
try {
runner.update("insert into tab_user(uid,username,password,name,birthday,sex,telephone,email,status,code) values(?,?,?,?,?,?,?,?,?,?)",user.getUid(),user.getUsername(),user.getPassword(),user.getName(),user.getBirthday(),user.getSex(),user.getTelephone(),user.getEmail(),user.getStatus(),user.getCode());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void updateUser(User user) {
try {
runner.update("update tab_user set name=? where uid=?",user.getName(),user.getUid());
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void deleteUser(String username) {
try {
runner.update("delete from tab_user where username=? ",username);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
(9)项目目录结构
(10)测试
package test;
import domain.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import java.util.List;
public class UserServiceTest {
@Test
public void testFindAll() {
//获取容器
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
UserService us = ac.getBean("userService",UserService.class);
//执行方法
List<User> users = us.findAllUser();
for (User user : users) {
System.out.println(user);
}
}
@Test
public void testFindOne() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
UserService us = ac.getBean("userService",UserService.class);
//执行方法
User user = us.findUserById(9);//将id为9的用户信息查询出来
System.out.println(user.toString());
}
@Test
public void testSave() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
UserService us = ac.getBean("userService",UserService.class);
//执行方法
User user = new User();
user.setUid(88);
user.setBirthday(null);
user.setCode("cm");
user.setEmail("xjitcm@163.com");
user.setName("小明");
user.setPassword("123");
user.setSex("男");
user.setStatus("Y");
user.setTelephone("18208276041");
user.setUsername("dear");
us.saveUser(user);
}
@Test
public void testUpdate() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
UserService us = ac.getBean("userService",UserService.class);
//执行方法
User user = new User();
user.setUid(88);
user.setBirthday(null);
user.setCode("cc");
user.setEmail("xjitcm@163.com");
user.setName("xiaoming");
user.setPassword("123");
user.setSex("男");
user.setStatus("Y");
user.setTelephone("18208276041");
user.setUsername("dear");
us.updateUser(user);
}
@Test
public void testDelete() {
ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
//获取业务层对象
UserService us = ac.getBean("userService",UserService.class);
us.deleteUser("dear");
}
}