不懂關(guān)于JDBC Template基本使用?其實(shí)想解決這個(gè)問(wèn)題也不難,下面讓小編帶著大家一起學(xué)習(xí),希望大家閱讀完這篇文章后大所收獲。
在營(yíng)山等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需求定制設(shè)計(jì),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站設(shè)計(jì),成都全網(wǎng)營(yíng)銷推廣,成都外貿(mào)網(wǎng)站制作,營(yíng)山網(wǎng)站建設(shè)費(fèi)用合理。
1.使用maven引用依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/MySQL/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> </dependencies>
2.編寫spring的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/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置dataSource--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://localhost:3306/demo1?serverTimezone=UTC"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!--使用jdbcTemplate工具類--> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean> </beans>
3.ddl操作(建表之類)
import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.JdbcTemplate; public class test { @Test public void test(){ ApplicationContext applicationContext=new ClassPathXmlApplicationContext("myApplication.xml"); JdbcTemplate springTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); springTemplate.execute("create table test(id int,username varchar(10))"); } }
4.增刪改:
對(duì)應(yīng)的使用例子
public void testUpdate(){ String sql = "insert into student(name,sex) values(?,?)"; jdbcTemplate.update(sql,new Object[]{"張飛","男"}); } public void testUpdate2(){ String sql = "update student set sex=? where id=?"; jdbcTemplate.update(sql,"女",1003); } public void testBatchUpdate(){ String[] sqls={ "insert into student(name,sex) values('關(guān)羽','女')", "insert into student(name,sex) values('劉備','男')", "update student set sex='女' where id=2001" }; jdbcTemplate.batchUpdate(sqls); } public void testBatchUpdate2(){ String sql = "insert into selection(student,course) values(?,?)"; List<Object[]> list = new ArrayList<Object[]>(); list.add(new Object[]{1005,1001}); list.add(new Object[]{1005,1003}); jdbcTemplate.batchUpdate(sql,list); }
5.查詢
public void testQuerySimple1(){ String sql = "select count(*) from student"; int count = jdbcTemplate.queryForObject(sql,Integer.class); System.out.println(count); } public void testQuerySimple2(){ String sql = "select name from student where sex=?"; List<String> names = jdbcTemplate.queryForList(sql,String.class,"女"); System.out.println(names); } public void testQueryMap1(){ String sql = "select * from student where id = ?"; Map<String,Object> stu = jdbcTemplate.queryForMap(sql,1003); System.out.println(stu); } public void testQueryMap2(){ String sql = "select * from student"; List<Map<String,Object>> stus = jdbcTemplate.queryForList(sql); System.out.println(stus); } public void testQueryEntity1(){ String sql = "select * from student where id = ?"; Student stu = jdbcTemplate.queryForObject(sql, new StudentRowMapper(), 1004); System.out.println(stu); } @org.junit.Test public void testQueryEntity2(){ String sql = "select * from student"; List<Student> stus = jdbcTemplate.query(sql,new StudentRowMapper()); System.out.println(stus); } private class StudentRowMapper implements RowMapper<Student>{ public Student mapRow(ResultSet resultSet, int i) throws SQLException { Student stu = new Student(); stu.setId(resultSet.getInt("id")); stu.setName(resultSet.getString("name")); stu.setSex(resultSet.getString("sex")); stu.setBorn(resultSet.getDate("born")); return stu; } }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享關(guān)于JDBC Template基本使用介紹內(nèi)容對(duì)大家有幫助,同時(shí)也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,遇到問(wèn)題就找創(chuàng)新互聯(lián),詳細(xì)的解決方法等著你來(lái)學(xué)習(xí)!
網(wǎng)頁(yè)題目:關(guān)于JDBCTemplate基本使用介紹
瀏覽路徑:http://muchs.cn/article30/jpcspo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、小程序開發(fā)、全網(wǎng)營(yíng)銷推廣、定制開發(fā)、云服務(wù)器、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)