當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring学习五(JDBC支持)
生活随笔
收集整理的這篇文章主要介紹了
Spring学习五(JDBC支持)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring的jdbc支持
?
1配置db.properties,將有關JDBC的信息載入
2bean文件配置數據源,這里用e3p0作為數據源載入db.properties
3配置template的bean 之后,可以直接使用。
JUnit的測試
代碼如下
db.proeprties
jdbc.username=root jdbc.password=s1127736971 jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://127.0.0.1:3306/school jdbc.iniPoolSize=5 jdbc.maxPoolSize=10 //強烈注意!!!!! 每行空格也要刪掉,因為載入的時候會讀取空格?
?
?
bean文件如下
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd"><!-- 配置配置文件 --> <context:property-placeholder location="classpath:db.properties"/><!-- 配置數據源文件 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="driverClass" value="${jdbc.driver}"></property> <property name="initialPoolSize" value="${jdbc.iniPoolSize}"></property> <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property> </bean><!-- 配置template --> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource"></property> </bean><context:component-scan base-package="Test"></context:component-scan> </beans>?
?
?
java文件如下
package Test;import java.sql.SQLException; import java.util.ArrayList; import java.util.List;import javax.sql.DataSource;import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper;public class test {private ApplicationContext ctx= null;private JdbcTemplate jdbcTemplate=null;private StudentDao dao;{ctx=new ClassPathXmlApplicationContext("SpringJDBC.xml");jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");//里面就已經配置了datasource的配置,所以可以更改。dao=ctx.getBean(StudentDao.class);}@Testpublic void testStudentDao(){System.out.println(dao.getById(2));}@Testpublic void testQueryForObject(){//用于數據統計類型String sql = "SELECT COUNT(id) FROM student ";long count = jdbcTemplate.queryForObject(sql, Long.class);System.out.println(count);}@Testpublic void testQueryForList(){//收集屬性集合類型String sql = "SELECT id,name,age,address FROM student WHERE id>? ";RowMapper<Student> rowMapper= new BeanPropertyRowMapper<Student>(Student.class);List<Student> students =jdbcTemplate.query(sql, rowMapper,1);//一個不同的集合語句 System.out.println(students);}/** 這里要用BeanPropertyRowMapper,不要在queryForObject里面用另一個,格式如下。* 1 可以在里面配置類對應的名字,比如 name name 前一個是類屬性名字,后一個是database里面名字* 2 不支持級聯屬性* 3 RowMapper 是映射屬性行,BeanPropertyRowMapper是其一個實現類* */@Testpublic void testQueryById(){String sql = "SELECT id,name,age,address FROM student WHERE id=? ";RowMapper<Student> rowMapper= new BeanPropertyRowMapper<Student>(Student.class);Student student =jdbcTemplate.queryForObject(sql, rowMapper,1);System.out.println(student);}@Testpublic void testUpdateBatch(){String sql = "INSERT INTO student(id,name,age,address) Values(?,?,?,?)";List<Object[]> date = new ArrayList<>();date.add(new Object[]{"5","xiaohong","52","shanghai"});date.add(new Object[]{"6","xiaobai","22","fujian"});jdbcTemplate.batchUpdate(sql, date);}@Test//這個更新可以是 UPDATE DELETE INSERTpublic void testUpdate (){String sql = "UPDATE student SET Address = ? where id=?";jdbcTemplate.update(sql, "beijing",4);}@Testpublic void testDataSource() throws SQLException {//測試 DataSource是否可行DataSource datasource = (DataSource) ctx.getBean(javax.sql.DataSource.class);//記一個非常不科學的錯誤,db.properties里面不要有空格,他會把空格也當成命令。 System.out.println(datasource.getConnection());}}?
?
?
注,一般用Dao類文件,是將Dao里面添加jdbcTemplate成員屬性。而不用另一種extends 的方法,不考慮。
package Test;import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.BeanPropertyRowMapper; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository;@Repository//自動裝配 public class StudentDao {@Autowiredprivate JdbcTemplate jdbcTemplate;public Student getById(int id){String sql = "SELECT id,name,age,address FROM student WHERE id=? ";RowMapper<Student> rowMapper= new BeanPropertyRowMapper<Student>(Student.class);Student student =jdbcTemplate.queryForObject(sql, rowMapper,id);return student;} }?
?
使用jdbc的具名參數設置
配置NamedParameterJdbcTemplate ? Bean文件
xml添加
<!-- 配置帶參template --> <bean id="namedtemplate" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <constructor-arg ref="dataSource"></constructor-arg> </bean>?
java代碼添加測試
@Test//這是利用對象來具名參數,要求Values(:id,:name,:age,:address)和類的名字要一樣,mysql的名字要一樣。public void testNamedTemplate2(){String sql = "INSERT INTO student(id,name,age,address) Values(:id,:name,:age,:address)";Student student = new Student();student.setId(11);student.setAge(4);student.setName("xiaoxiaobai");student.setAddress("hong shang lu ");SqlParameterSource ss = new BeanPropertySqlParameterSource(student);namedParameterJdbcTemplate.update(sql, ss);}@Test//利用具名參數來構造template 優點當參數多的時候維護比較容易 不需要按照固定順序。缺點是相對于estUpdateBatch()比較復雜public void testNamedTemplate(){String sql = "INSERT INTO student(id,name,age,address) Values(:id,:name,:age,:address)";Map<String,Object> paramMap = new HashMap<String,Object>();paramMap.put("id", 10);paramMap.put("name", "xiaobai");paramMap.put("age",51 );paramMap.put("address", "tongbei");namedParameterJdbcTemplate.update(sql, paramMap);}?
轉載于:https://www.cnblogs.com/s1127736971/p/7536557.html
總結
以上是生活随笔為你收集整理的Spring学习五(JDBC支持)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 5.1深入理解计算机系统——系统级I/O
- 下一篇: 字符串操作练习:星座、凯撒密码、99乘法