使用 spring 的 IoC 的实现用户的 CRUD
1、需求 :实现用户的CRUD操作
2、代码准备
(1) 编写实体类
package com.mingqi.domin; import java.io.Serializable; public class Users implements Serializable { @Override public String toString() { return "Users{" + "ID='" + ID + '\'' + ", Name='" + Name + '\'' + ", Account='" + Account + '\'' + ", Password='" + Password + '\'' + ", Status=" + Status + ", Sort=" + Sort + ", Note='" + Note + '\'' + '}'; } private String ID; private String Name; private String Account; private String Password; private Integer Status; private Integer Sort; private String Note; public String getID() { return ID; } public void setID(String ID) { this.ID = ID; } public String getName() { return Name; } public void setName(String name) { Name = name; } public String getAccount() { return Account; } public void setAccount(String account) { Account = account; } public String getPassword() { return Password; } public void setPassword(String password) { Password = password; } public Integer getStatus() { return Status; } public void setStatus(Integer status) { Status = status; } public Integer getSort() { return Sort; } public void setSort(Integer sort) { Sort = sort; } public String getNote() { return Note; } public void setNote(String note) { Note = note; } }
(2)编写持久层代码
//持久层接口
public interface IUsersDao { /* * 保存 * */ void Save(Users users); /* * 更新 * */ void update(Users users); void delete(String ID); Users FindById(String ID); List<Users> FindAll(); } //持久层实现类 public class UsersDaoimpl implements IUsersDao { private QueryRunner runner; public void setRunner(QueryRunner runner) { this.runner = runner; } public void Save(Users users) { try{ runner.update("insert into Users (id,name,Account,Password) values(?,?,?,?)",users.getID(),users.getName(),users.getAccount(),users.getPassword()); } catch (Exception e) { throw new RuntimeException(e); } } public void update(Users users) { try{ runner.update("update Users set Account=? where id=?",users.getAccount(),users.getID()); } catch (Exception e) { throw new RuntimeException(e); } } public void delete(String ID) { try{ runner.update("delete from Users where id=?",ID); }catch (Exception e) { throw new RuntimeException(e); } } public Users FindById(String ID) { try{ return runner.query("select * from Users where id = ? ",new BeanHandler<Users>(Users.class),ID); }catch (Exception e) { throw new RuntimeException(e); } } public List<Users> FindAll() { try{ return runner.query("select * from Users",new BeanListHandler<Users>(Users.class)); }catch (Exception e) { throw new RuntimeException(e); } } }
(3)编写业务层代码
//业务层接口 public interface IUsersService { List<Users> findAllUsers(); Users FindUserById(String Id); void saveUsers(Users user); void updateUser(Users user); void deleteUsers(String id); } //业务层实现类 public class UsersServiceImpl implements IUsersService { public void setUsersDao(IUsersDao usersDao) { this.usersDao = usersDao; } private IUsersDao usersDao; //@Override public List<Users> findAllUsers() { return usersDao.FindAll(); } public Users FindUserById(String Id) { return usersDao.FindById(Id); } public void saveUsers(Users user) { usersDao.Save(user); } public void updateUser(Users user) { usersDao.update(user); } public void deleteUsers(String id) { usersDao.delete(id); } }
(4)创建配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置QueryRunner--> <bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"> <!--注入数据源--> <constructor-arg name="ds" ref="dataSource"></constructor-arg> </bean> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/> <property name="jdbcUrl" value="jdbc:sqlserver://localhost:1433;DatabaseName=test"/> <property name="user" value="sa"/> <property name="password" value="123sa"/> </bean> <bean id="usersDao" class="com.mingqi.dao.impl.UsersDaoimpl"> <property name="runner" ref="runner"></property> </bean> <bean id="usersService" class="com.mingqi.service.impl.UsersServiceImpl"> <property name="usersDao" ref="usersDao"></property> </bean> </beans>
(5)测试类代码
package com.mingqi; import com.mingqi.domin.Users; import com.mingqi.service.IUsersService; 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 java.util.List; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:bean.xml") public class springdemotest { @Autowired private IUsersService as; @Test public void testFindAll() { //3.执行方法 List<Users> users = as.findAllUsers(); for(Users user : users){ System.out.println(user.toString()); } } @Test public void testFindById() { Users u=as.FindUserById("23C3E9FC-6D8A-4EA0-925A-0A0671D61378"); System.out.println(u); } @Test public void testSave() { Users user =new Users(); user.setID("23C3E9FC-6D8A-4EA0-925A-0A0671D61379"); user.setName("李明奇"); user.setAccount("mingqi"); user.setPassword("7577447B52FB96045ADD91A8F2AB06FB"); as.saveUsers(user); } @Test public void testupdate() { Users user=as.FindUserById("23C3E9FC-6D8A-4EA0-925A-0A0671D61379"); // user.setID("23C3E9FC-6D8A-4EA0-925A-0A0671D61379"); user.setAccount("limingqi"); as.updateUser(user); } @Test public void testdelete() { as.deleteUsers("23C3E9FC-6D8A-4EA0-925A-0A0671D61379"); // user.setID("23C3E9FC-6D8A-4EA0-925A-0A0671D61379"); } }
@Test public void test() { ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); IUsersService usersService = ac.getBean("usersService",IUsersService.class); Users u= usersService.FindUserById("23C3E9FC-6D8A-4EA0-925A-0A0671D61378"); System.out.println(u); }