Spring JDBC 是spring所提供的持久层技术。它的主要目的是降低使用JDBC API的门槛,以一种更直接,更简洁的方式使用JDBC API 。 在Spring JDBC 里,用户仅需要做那些必不可少的时,而将资源获取,Statement创建,异常处理,资源释放等繁杂而乏味的工作交给Spring。
虽然ORM的框架已经成熟丰富,但JDBC灵活直接的特性,依然让它拥有自己的用武之地。例如,在完全依赖数据库配置产生查询方法的综合查询系统中,Hibernate,ibatis,JPA等框架都无法使用,这时JDBC是唯一的选择。
Spring JDBC提供了三个模板类对数据库进行操作。 JdbcTemolate
NamedParameterJdbcTemplate SimpleJdbcTemplate(过时) SimpleJdbcInsert(执行insert操作)
公用代码:
A. 新建applicationContext_jdbc.xml 配置文件 B. 在其中添加alibaba dataSource 配置信息
Cost实体类:(table=king_cost)
public class Cost { private Integer id; private String name; private Integer baseDuration; private Double baseCost; private Double unitCost; private String status; private String descr; private Date createTime; private Date startTime; } private String costType;//自己提供getter,setter方法
Account实体类:(table=king_account)
public class Account { private Integer id; private Integer recommenderId; private String loginName; } private String loginPassword; private String status; private Date createDate; private Date pauseDate; private Date closeDate; private String realName; private String idcardNo; private Date birthdate; private String gender; private String occupation; private String telephone; private String email; private String mailaddress; private String zipcode; private String qq; private Date lastLoginTime; private String lastLoginIp;//提供getter,setter方法
4.1 使用JdbcTemplete
第一种获取JdbcTemplete对象 继承JdbcDaoSupport
public class CostDaoImpl extends JdbcDaoSupport{ private JdbcTemplate jdbcTemplate; @Resource public void setMyDataSource(DataSource dataSource){ setDataSource(dataSource); jdbcTemplate=super.getJdbcTemplate(); } } 那么在类中可以使用jdbcTemplate对象 第二种获取JdbcTemplete对象
public class CostDaoImpl extends JdbcDaoSupport{ } private JdbcTemplate jdbcTemplate; @Resource public void setMyDataSource(DataSource dataSource){ jdbcTemplate=super.createJdbcTemplate(dataSource); }
在项目中为了开发简便,我们可以创建一个dao基类
public class BasicDao { protected JdbcTemplate jdbcTemplate; @Resource public void setDataSource(DataSource dataSource){ jdbcTemplate=new JdbcTemplate(dataSource); } }
那么所有的dao实现类都继承该基类那么在dao实现类中可以直接使用jdbcTemplate方便我们编程。
queryForObject 方法的使用:
1. 查询所有记录数(返回基本类型)
/** * 查询所有记录数 */ public int getCount() { return jdbcTemplate.queryForObject( \"select count(*) from king_cost\", Integer.class); }
int queryForInt(String sql)Deprecated. int queryForInt(String sql, Object... args)Deprecated. int queryForInt(String sql, Object[] args, int[] argTypes)Deprecated.
long queryForLong(String sql)Deprecated. long queryForLong(String sql, Object... args)Deprecated. long queryForLong(String sql, Object[] args, int[] argTypes)Deprecated.
以上六种方法在spring jdbc新版本中不推荐使用了,使用queryForObject 类型来替换以上方法 有参数的查询语句
public int getCount() { return jdbcTemplate.queryForObject( \"select count(*) from cost where id>:id\", Integer.class, new Object[]{0}); }
2. 获取实体类(通过条件获取实体类)
通过RowMapper 获取实体类对象
public Cost getById(Integer id) { return jdbcTemplate.queryForObject( \"select id,name from cost where id=?\", new Object[]{id}, new RowMapper 3. Update操作 /** * 更新数据 update 操作 * @param cost */ public int update(Cost cost){ int num=jdbcTemplate.update( \"update king_cost set\" + \" name=?,descr=?\" + \" where id=?\", new Object[]{ cost.getName(), cost.getDescr(), cost.getId()}); return num; } 4. Insert 操作 /** * insert操作 * @param cost */ public void save(Cost cost){ jdbcTemplate.update(\"insert into \" + \"king_cost(id,name,descr)\" + \" values (?,?,?)\", new Object[]{ cost.getId(), cost.getName(), cost.getDescr()}); } 5. Delete 操作 /** * 删除操作 * @param id */ public void delete(int id){ jdbcTemplate.update( \"delete from \" + \"king_cost \" + \"where id=?\", new Object[]{id}); } 6. 分页语句 private static final String PAGE_SQL=\"select * from (select t.*,rownum rn from king_cost t where rownum>=?) where rn<=?\"; public List 4.2 使用NamedParameterJdbcTemplate 上面我们已经演示了JdbcTeplate的使用方法,那么同理我们演 示下NamedParameterJdbcTemplate的使用方法。 第一步我们需要注入NamedParameterJdbcTemplate对象 private NamedParameterJdbcTemplate namedParameterJdbcTemplate; /** * 注入dataSource,创建NamedParameterJdbcTemplate对象 * @param dataSource */ @Resource public void setDataSource(DataSource dataSource){ namedParameterJdbcTemplate=new NamedParameterJdbcTemplate(dataSource); } 主要代码如下: public interface IAccountDao { public int count(); public void save(Account account); public void update(Account account); public void delete(Object id); public int[] batchUpdate(final List 下面代码是用NamedParameterJdbcTemplate 对数据表的CURD操作 @Repository(value=\"accountDao\") public class AccountDaoImpl implements IAccountDao { private NamedParameterJdbcTemplate namedParameterJdbcTemplate; /** * 注入dataSource,创建NamedParameterJdbcTemplate对象 * @param dataSource */ @Resource public void setDataSource(DataSource dataSource){ namedParameterJdbcTemplate=new NamedParameterJdbcTemplate(dataSource); } private static final String SAVE_SQL=\"insert into king_account (id,loginName,loginPwd,realname,idCardNo,telephone) values(:id,:loginName,:loginPwd,:realName,:idCard,:telephone)\"; /** * insert 操作 */ public void save(Account account) { Map public class AccountDaoImplTest { public static ApplicationContext act=null; static String[] configs=new String[]{\"applicationContext_jdbc.xml\"}; private static IAccountDao dao; @BeforeClass public static void beforeClass(){ act=new ClassPathXmlApplicationContext(configs); dao=(IAccountDao) act.getBean(\"accountDao\"); } @Test public void testSave() { Account account=new Account(); account.setId(10240); } account.setLoginPassword(\"密码....\"); account.setLoginName(\" 手动添加loginName\"); account.setRealName(\"真实姓名为:king\"); account.setIdcardNo(\"411524\"); account.setTelephone(\"181\"); dao.save(account); @Test public void testUpdate() { Account account=new Account(); account.setId(1024); account.setLoginName(\" 手动修改loginName\"); dao.update(account); } @Test public void testDelete() { dao.delete(1025); } @Test public void testGetById() { try { System.out.println(dao.getById(1024)); } catch (Exception e) { e.printStackTrace(); } } @Test public void testGetListByPage() { List 因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- huatuoyibo.net 版权所有 湘ICP备2023021910号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务