當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
数据库操作技术--Spring jdbcTemplate
生活随笔
收集整理的這篇文章主要介紹了
数据库操作技术--Spring jdbcTemplate
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
一丶jdbcTemplate是什么
????? ? ? ?jdbcTemplate是Spring提供的一種操作數(shù)據(jù)庫的技術(shù),是對Jdbc的封裝。語法非常接近DBUtils.
? ? ? ? 使用JdbcTemplate可以直接操作數(shù)據(jù)庫,加快效率。
二丶jdbcTemplate完整實現(xiàn)步驟
? ? ? ? 2.1)創(chuàng)建web項目
? ? ? ? ??2.2)添加String jar包
? ? ? ? ? ? ? ? ? ?????????2.3)添加String Template支持(配置applicationContext.xml文件)
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">//配置數(shù)據(jù)源<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver"/><property name="username" value="root"/><property name="password" value="root"/><property name="url" value="jdbc:mysql:///tb_test"/></bean>//添加jdbcTemplate對應(yīng)的bean<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"><property name="dataSource" ref="dataSource"/></bean> </beans>? ? ? ? 2.4)創(chuàng)建一個Dept類(dept.java)
public class dept {private String deptno;private String dname;private String loc;public String getDeptno() {return deptno;}public void setDeptno(String deptno) {this.deptno = deptno;}public String getDname() {return dname;}public void setDname(String dname) {this.dname = dname;}public String getLoc() {return loc;}public void setLoc(String loc) {this.loc = loc;}public dept(String deptno, String dname, String loc) {this.deptno = deptno;this.dname = dname;this.loc = loc;}public dept(){super();}@Overridepublic String toString() {return "dept{" +"deptno='" + deptno + '\'' +", dname='" + dname + '\'' +", loc='" + loc + '\'' +'}';} }? ? ? ?2.5)創(chuàng)建一個對Dept類進行CURD操作的類(deptService.java)
public class DeptService {private JdbcTemplate jdbcTemplate;//注入jdbcTemplate//代碼塊,在執(zhí)行方法之前獲取jdbcTemplate{ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");jdbcTemplate = (JdbcTemplate) act.getBean("jdbcTemplate");//對應(yīng)applicationContext.xml文件中配置的 jdbcTemplateSystem.out.println("jdbcTemplate:"+jdbcTemplate);}//查詢操作public void selectAllDepts()throws SQLException {BeanPropertyRowMapper<dept> rowMapper = new BeanPropertyRowMapper<>(dept.class);//使用Spring的JdbcTemplate查詢數(shù)據(jù)庫,獲取List結(jié)果列表,數(shù)據(jù)庫表字段和實體類自動對應(yīng),可以使用BeanPropertyRowMapperList<dept> depts = jdbcTemplate.query("select * from tb_dept", rowMapper);for(dept item:depts){System.out.println(item);}}//使用jdbcTemplate進行插入操作public void addDept(dept dept) throws SQLException{int add = jdbcTemplate.update("insert into tb_dept values (?,?,?)", dept.getDeptno(), dept.getDname(), dept.getLoc());if (add==1){System.out.println("insert success");}else{System.out.println("insert failed");}}//更新操作public void updateDept(dept dept) throws SQLException{int update = jdbcTemplate.update("update tb_dept set dname=?,loc=?,where deptno=?");if (update==1){System.out.println("insert success");}else{System.out.println("insert failed");}}//刪除操作public void deleteDept(dept dept)throws SQLException{int delete = jdbcTemplate.update("delete from tb_dept where deptno =? ",dept.getDeptno());if (delete==1){System.out.println("insert success");}else{System.out.println("insert failed");}}//批量添加public void batchAdd()throws SQLException{String sql="insert into tb_dept values(?,?,?)";List<Object[]> depts = new ArrayList<>();depts.add(new Object[]{"a","aa","aaa"});depts.add(new Object[]{"b","bb","bbb"});depts.add(new Object[]{"c","cc","ccc"});int[] batchadd = jdbcTemplate.batchUpdate(sql, depts);}//查詢數(shù)據(jù)庫中記錄的數(shù)量public long selectCount()throws SQLException{String sql="select count(*) from tb_dept";Long res = jdbcTemplate.queryForObject(sql, long.class);return res;} }? ? ? ?2.6)創(chuàng)建一個測試類(DeptTest.java)
public class DeptTest {public static void main(String[] args) throws SQLException {DeptService deptService = new DeptService();deptService.addDept(new dept("g","ggg","gggg"));deptService.batchAdd();deptService.selectAllDepts();System.out.println(deptService.selectCount());deptService.deleteDept("g");} }//時間原因,只測試幾個三丶最后給大家推薦一個jar包下載網(wǎng)站:search.maven.org
總結(jié)
以上是生活随笔為你收集整理的数据库操作技术--Spring jdbcTemplate的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库连接池技术--c3p0
- 下一篇: 如何整一个厉害的产品slogan?