dao idea: establish a data interface:
Step: 1) create a database
2) Set up the package com > shadow > domian in the project (for storing object classes)
Establish object classes according to the tables in the database (in accordance with the javabean specification)
3) Set up the package com? Shadow? Dao in the project (the interface specially used to store operation classes)
4) In the project, set up the package com, set up the package com, set up the implementation class for storing the operation class
5) Set up package com > shadow > daotest (test class for storing test interface) in the project
Example
Ninjia.java
package com_xyit_shadow_domain; public class Ninjia { public Long id; public String name; public int age; public Ninjia() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "ninjia [id=" + id + ", name=" + name + ", age=" + age + "]"; } public Ninjia(String name, int age) { super(); this.name = name; this.age = age; } }
INinjiaDao.java
package com_xyit_shadow_Dao; import java.util.List; import com_xyit_shadow_domain.Ninjia; public interface INinjiaDao { //ddl Ninjia get(Long id); List<Ninjia> getall(); //dml Long save(Ninjia ninjia); Long update(Long id,Ninjia ninjia); Long delete(Long id); }
NinjiadaoImp.java
package com_xyit_shadow_Dao.imp; import java.util.List; import com_xyit_shadow_Dao.INinjiaDao; import com_xyit_shadow_Dao.IResultSetHandle; import com_xyit_shadow_domain.Ninjia; public class NinjiaDaoimp implements INinjiaDao{ public Ninjia get(Long id){ String sql="SELECT * FROM `ninjia` WHERE `id` = ?"; IResultSetHandle<Ninjia> IRS=new BeanHandler<>(Ninjia.class); return JdbcTemplate.get(sql,IRS, id).get(0); } public List<Ninjia> getall() { String sql="SELECT * FROM `ninjia`"; IResultSetHandle<Ninjia> IRS=new BeanHandler<>(Ninjia.class); return JdbcTemplate.get(sql,IRS); } public Long save(Ninjia ninjia){ //INSERT INTO `student` (`id`, `name`, `age`) VALUES ('3', '3', '5') String sql="INSERT INTO `ninjia` (`name`, `age`) VALUES (?, ?)"; Object[] params=new Object[] {ninjia.name,ninjia.age}; return JdbcTemplate.update(sql, params); } public Long update(Long id, Ninjia ninjia) { String sql="UPDATE `ninjia` SET `name`=?, `age`=? WHERE (`id`=?)"; return JdbcTemplate.update(sql, ninjia.name,ninjia.age,id); } public Long delete(Long id) { String sql="DELETE FROM `ninjia` WHERE (`id`=?)"; return JdbcTemplate.update(sql, id); } }
package com_xyit_shadow_test; import java.util.List; import org.junit.Test; import com_xyit_shadow_Dao.INinjiaDao; import com_xyit_shadow_Dao.imp.NinjiaDaoimp; import com_xyit_shadow_domain.Ninjia; public class INinjiaDaoTest { @Test public void testGet() throws Exception { Long id=(long) 1; INinjiaDao ninjiaDao=new NinjiaDaoimp(); Ninjia ninjia=ninjiaDao.get(id); System.out.println(ninjia); } @Test public void testGetall() { INinjiaDao ninjiaDao=new NinjiaDaoimp(); List<Ninjia> ninjias=ninjiaDao.getall(); System.out.println(ninjias); } @Test public void testSave() throws Exception { INinjiaDao ninjiaDao=new NinjiaDaoimp(); Ninjia ninjia=new Ninjia("Five kills", 5); System.out.println(ninjiaDao.save(ninjia)); } @Test public void testUpdate() { INinjiaDao ninjiaDao=new NinjiaDaoimp(); Ninjia ninjia=new Ninjia("Five kills", 5); System.out.println(ninjiaDao.update((long) 6, ninjia)); } @Test public void testDelete() { INinjiaDao ninjiaDao=new NinjiaDaoimp(); ninjiaDao.delete((long) 6); } }