介是啥米东东

Spring Access To JDBC

Java admin 1930℃ 0评论

使用Spring通过JDBC访问数据库,Spring版本4.03

首先需要的Jar包有(没有Web相关,只是测试)

360软件小助手截图20140502235213

 

beans.xml

[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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

<bean id="userDao" class="com.znn.dao.UserDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/mysql"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>

<!– <context:property-placeholder location="jdbc.properties"/>
–>
</beans>
[/xml]

 

UserDaoImpl.java

 

[java]
package com.znn.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import com.znn.vo.User;

public class UserDaoImpl implements UserDao {
private JdbcTemplate jdbcTemplate;
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
// this.jdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}
@Override
public List query(){
List list = this.jdbcTemplate.query("select * from spring_user", new RowMapper(){

@Override
public User mapRow(ResultSet resultSet, int rowNum) throws SQLException {
User user = new User();
user.setName(resultSet.getString("name"));
user.setId(resultSet.getInt("id"));
user.setAge(resultSet.getInt("age"));
return user;
}} );
return list;
}
@Override
public int save(User user) {
return this.jdbcTemplate.update("insert into spring_user (name,age) values(?,?)", user.getName(),user.getAge());
}
@Override
public int update(User user) {
return this.jdbcTemplate.update("update spring_user set name=?,age=? where id=?", user.getName(),user.getAge(),user.getId());
}
@Override
public int delete(int id) {
return this.jdbcTemplate.update("delete from spring_user where id=?",id);
}
@Override
public User queryAUser(int id) {
//若查不到或查到多条都会报异常
// User user1 = this.jdbcTemplate.queryForObject("select * from spring_user where id=?",new Object[]{id}, new RowMapper(){
//
// @Override
// public User mapRow(ResultSet resultSet, int rowNum) throws SQLException {
// User user = new User();
// user.setName(resultSet.getString("name"));
// user.setId(resultSet.getInt("id"));
// user.setAge(resultSet.getInt("age"));
// return user;
// }});
List users = this.jdbcTemplate.query("select * from spring_user where id=?",new Object[]{id}, new RowMapper(){

@Override
public User mapRow(ResultSet resultSet, int rowNum) throws SQLException {
User user = new User();
user.setName(resultSet.getString("name"));
user.setId(resultSet.getInt("id"));
user.setAge(resultSet.getInt("age"));
return user;
}});
if (users.size()==0) {
return null;
}else {
return users.get(0);
}
}
}</pre>
User.xml

&nbsp;
<pre lang="java">package com.znn.vo;

public class User {
private int id;
private String name;
private int age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}
[/java]

测试:

 

[java]
package com.znn.test;

import static org.junit.Assert.*;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.znn.dao.UserDao;
import com.znn.vo.User;

public class SpringJDBCTest {

@Test
public void test() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
UserDao dao = (UserDao) applicationContext.getBean("userDao");
// List list = dao.query();
// System.out.println(list);
User user= new User();
// user.setId(1);
user.setName("adminn");
user.setAge(22);
// affectNum = dao.save(user);
// affectNum = dao.update(user);
// affectNum = dao.delete(1);
// System.out.println(affectNum);

User u = dao.queryAUser(2);
System.out.println(u);
}

}

[/java]
源代码:

转载请注明:Z/RANDY » Spring Access To JDBC

喜欢 (0)or分享 (0)
发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址