是对dao层的实现
AdminDaoImpl
1 package com.jsj.dao.Impl; 2 3 import com.jsj.dao.AdminDao; 4 import com.jsj.entity.Admin; 5 import com.jsj.utils.BeanHandler; 6 import com.jsj.utils.JdbcTemplate; 7 8 public class AdminDaoImpl implements AdminDao { 9 //实现上面对应的接口的方法 10 //接收到username和password参数 11 @Override 12 public Admin getAdminByPassword(String username, String password) throws Exception { 13 //sql语句,?可以看做是变量,你可以添加数据的 14 String sql = "select * from admin where username = ? and password = ?"; 15 //然后调用工具类,(Admin)这是强制转换, 16 //类名.方法名,是因为方法是static类型的,所以不要创建对象,然后在调用 17 //query的参数有:sql语句,new BeanHandler(Admin.class)是对象、username、password 18 //Admin.class是:返回的Adimin的Class对象,如果后面加newINnstace()就好创建对象 19 return (Admin) JdbcTemplate.query(sql, new BeanHandler(Admin.class), username, password); 20 } 21 }
NewCateDaoImpl
1 package com.jsj.dao.Impl; 2 3 import com.jsj.dao.NewsCateDao; 4 import com.jsj.entity.NewsCate; 5 import com.jsj.utils.BeanHandler; 6 import com.jsj.utils.BeanListHandler; 7 import com.jsj.utils.JdbcTemplate; 8 9 import java.util.List; 10 11 public class NewsCateDaoImpl implements NewsCateDao { 12 13 @Override 14 public List<NewsCate> getAll() throws Exception { 15 //sql语句,实现查找 16 String sql = "select * from news_cate"; 17 //就是直接调用工具类 18 return (List<NewsCate>) JdbcTemplate.query(sql,new BeanListHandler(NewsCate.class)); 19 } 20 21 @Override 22 public NewsCate getById(Integer id) throws Exception { 23 String sql = "select * from news_cate where id = ?"; 24 return (NewsCate) JdbcTemplate.query(sql, new BeanHandler(NewsCate.class),id); 25 } 26 27 @Override 28 public int insert(NewsCate newsCate) throws Exception { 29 String sql = "insert into news_cate(name) values (?)"; 30 return JdbcTemplate.update(sql, newsCate.getName()); 31 } 32 33 @Override 34 public int update(NewsCate newsCate) throws Exception { 35 String sql = "update news_cate set name=? where id=?"; 36 return JdbcTemplate.update(sql, newsCate.getName(),newsCate.getId()); 37 } 38 39 @Override 40 public int deleteById(Integer id) throws Exception { 41 String sql = "delete from news_cate where id = ?"; 42 return JdbcTemplate.update(sql, id); 43 } 44 }
NewDaoImpl
1 package com.jsj.dao.Impl; 2 3 import com.jsj.dao.NewsDao; 4 import com.jsj.entity.News; 5 import com.jsj.utils.BeanHandler; 6 import com.jsj.utils.BeanListHandler; 7 import com.jsj.utils.JdbcTemplate; 8 9 import java.sql.*; 10 import java.util.List; 11 12 public class NewsDaoImpl implements NewsDao { 13 14 @Override 15 public int getCount() throws Exception { 16 String sql = "select count(*) from news"; 17 return JdbcTemplate.queryCount(sql); 18 } 19 20 @Override 21 public int getCountByCate(Integer cateId) throws Exception { 22 String sql = "select count(*) from news where cate_id = ? order by time desc"; 23 return JdbcTemplate.queryCount(sql, cateId); 24 } 25 26 @Override 27 public List<News> getNewsList(Integer beginIndex, Integer length) throws Exception { 28 String sql = "select * from news order by time desc limit ?, ?"; 29 return (List<News>) JdbcTemplate.query(sql,new BeanListHandler(News.class),beginIndex, length); 30 } 31 32 @Override 33 public List<News> getNewsListByCate(Integer cateId,Integer beginIndex,Integer length) throws Exception { 34 String sql = "select * from news where cate_id = ? limit ?, ?"; 35 return (List<News>) JdbcTemplate.query(sql, new BeanListHandler(News.class),cateId,beginIndex, length); 36 } 37 38 @Override 39 public int insert(News news) throws Exception { 40 String sql = "insert into news(cate_id,title,author,time,content) values (?,?,?,?,?)"; 41 return JdbcTemplate.update(sql, news.getCateId(),news.getTitle(),news.getAuthor(), 42 new Date(news.getTime().getTime()),news.getContent()); 43 } 44 45 @Override 46 public News getById(Integer id) throws Exception { 47 String sql = "select * from news where id = ?"; 48 return (News) JdbcTemplate.query(sql, new BeanHandler(News.class),id); 49 } 50 51 @Override 52 public List<News> getAll() throws Exception { 53 String sql = "select * from news"; 54 return (List<News>) JdbcTemplate.query(sql, new BeanListHandler(News.class)); 55 } 56 57 @Override 58 public int deleteById(Integer id) throws SQLException { 59 String sql = "delete from news where id = ?"; 60 return JdbcTemplate.update(sql, id); 61 } 62 }
UserDaoImpl
1 package com.jsj.dao.Impl; 2 3 import com.jsj.dao.UserDao; 4 import com.jsj.entity.User; 5 import com.jsj.utils.BeanHandler; 6 import com.jsj.utils.JdbcTemplate; 7 8 public class UserDaoImpl implements UserDao { 9 10 @Override 11 public int insert(User user) throws Exception { 12 String sql = "insert into user(username,password,birthday,email,tel_number) values (?,?,?,?,?)"; 13 return JdbcTemplate.update(sql, user.getUsername(),user.getPassword(),user.getBirthday(),user.getEmail(),user.getTelNumber()); 14 } 15 16 @Override 17 public User getUserByPassword(String username, String password) throws Exception { 18 String sql = "select * from user where username = ? and password = ?"; 19 return (User) JdbcTemplate.query(sql,new BeanHandler(User.class),username, password); 20 } 21 22 }