要對數據庫執行任何的JDBC操作
有幾種方法可以得到DataSource
一
public static DriverManagerDataSource dataSource = new DriverManagerDataSource()
public static DriverManagerDataSource getInstance() {
dataSource
dataSource
dataSource
dataSource
return dataSource;
}
@Test
public void test() {
DataSoureProvider
try {
dataSource
}
catch (SQLException e) {
e
}
}
第
二
我將數據庫連接信息配置在properties文件中
書寫配置文件applicationContext
xmlns=
xmlns:xsi=
xmlns:p=
xsi:schemaLocation=
<bean id=
<property name=
<value>connect
</property>
</bean>
<bean id=
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
</bean>
第
進行測試
@Test
public void connectTest() {
ApplicationContext context = new ClassPathXmlApplicationContext(
BasicDataSource dataSource = (BasicDataSource) context
try {
dataSource
System
}
catch (SQLException e) {
e
}
}
使用mons
Spring把JDBC中重復的操作建立成了一個模板類org
使用JdbcTemplate:
要使用JdbcTemplate
private JdbcTemplate jdbcTemplate;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this
}
如上
xmlns:xsi=
xsi:schemaLocation=
<bean id=
class=
<property name=
<value>connect
</property>
</bean>
<bean id=
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
<property name=
<value>${db
</property>
</bean>
<bean id=
<property name=
<ref bean=
</property>
</bean>
<bean id=
<property name=
<ref bean=
</property>
</bean>
第
使用JdbcTemplate插入數據
private JdbcTemplate jdbcTemplate;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this
}
public void insert(Student student)
{
String sql =
//設置傳遞給通配符的參數
Object[] params = new Object[]{student
jdbcTemplate
}
第
進行測試
@Test
public void insertTest() {
Student student = new Student()
student
student
student
ApplicationContext context = new ClassPathXmlApplicationContext(
StudentDaoImp studentDao = (StudentDaoImp) context
studentDao
}
數據庫中多了一條記錄
mysql> select * from student;
+
| sno | cno | name | score |
+
|
|
|
|
|
|
|
|
|
|
+
批量插入數據
批量插入數據需要用到org
修改StudentDaoImp:
private JdbcTemplate jdbcTemplate;
@Override
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this
}
public int insert(Student student)
{
String sql =
//設置傳遞給通配符的參數
Object[] params = new Object[]{student
return jdbcTemplate
}
public int[] batchInsert(final List<Student> list)
{
String sql =
BatchPreparedStatementSetter setter = new BatchPreparedStatementSetter() {
@Override
public void setValues(PreparedStatement ps
Student student = (Student) list
ps
ps
ps
}
//有多少條記錄要處理
@Override
public int getBatchSize() {
return list
}
};
return jdbcTemplate
}
第
進行測試
public void batchInsert() {
Student student
student
student
student
student
student
student
student
student
student
student
student
student
List<Student> list = new ArrayList<Student>()
list
list
list
ApplicationContext context = new ClassPathXmlApplicationContext(
StudentDaoImp studentDao = (StudentDaoImp) context
studentDao
}
插入結果
mysql> select * from student;
+
| sno | cno | name | score |
+
|
|
|
+
查詢一條記錄
執行一條數據的查詢
修改StudentDaoImp:
* 查詢一條記錄
*/
public Student getStudent(final int id) {
// 裝載查詢結果
final Student student = new Student()
String sql =
// 設置查詢參數
final Object[] params = new Object[] { new Integer(id) };
// 進行查詢
jdbcTemplate
@Override
public void processRow(ResultSet rs) throws SQLException {
student
student
student
}
})
return student;
}
進行測試
public void selectTest() {
ApplicationContext context = new ClassPathXmlApplicationContext(
StudentDaoImp studentDao = (StudentDaoImp) context
Student student = studentDao
System
}
查詢多條記錄
這裡需要用到org
修改StudentDaoImp:
* 查詢多條記錄
*/
public List<Student> getAllStudent() {
String sql =
return jdbcTemplate
@Override
public Student mapRow(ResultSet rs
Student student = new Student()
student
student
student
return student;
}
})
}
RowMapper接口負責把Result中的一條記錄映射成一個對象
進行測試
public void getAllStudent() {
ApplicationContext context = new ClassPathXmlApplicationContext(
StudentDaoImp studentDao = (StudentDaoImp) context
List<Student> list = new ArrayList<Student>()
list = studentDao
for (int i =
System
}
}
也可以使用這種方法查詢一條記錄
* 查詢一條記錄
*/
public Student getStudent(final int id) {
// 裝載查詢結果
final Student student = new Student()
String sql =
// 設置查詢參數
final Object[] params = new Object[] { new Integer(id) };
List<Student> list = jdbcTemplate
new RowMapper<Student>() {
@Override
public Student mapRow(ResultSet rs
throws SQLException {
Student student = new Student()
student
student
student
return student;
}
})
return list
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25910.html