这里介绍一下通过Spring JDBC的方式进行数据库的增删改查的操作。
在进行程序的编写之前我们需要在本地MySQL数据库中创建一张User表,如下所示:
create database user_db; use user_db; create table userinfo ( id int, username varchar(30), password varchar(30) );
创建完这个表以后,我们需要创建一个User类与之对应:
package com.myspring.obj; public class User { private int id; private String username; private String password; public User() {} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "[" + id + "," + username + "," + password + "]"; } }
然后我们需要创建一个Dao类UseDao用于操作数据:
package com.myspring.dao; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import org.springframework.jdbc.core.RowMapper; import org.springframework.jdbc.core.support.JdbcDaoSupport; import com.myspring.obj.User; public class UserDao extends JdbcDaoSupport { class UserRowMapper implements RowMapper<User> { public User mapRow(ResultSet rs, int rowNum) throws SQLException { User user = new User(); user.setId(rs.getInt("id")); user.setUsername(rs.getString("username")); user.setPassword(rs.getString("password")); return user; } } public List<User> queryAll() { return getJdbcTemplate().query("select * from userinfo", new UserRowMapper()); } public List<User> queryByUsername(String username) { return getJdbcTemplate().query("select * from userinfo where username=?", new Object[] { username }, new UserRowMapper()); } public void create(User user) { getJdbcTemplate().update("insert into userinfo (id,username,password) values (?,?,?)", new Object[] { user.getId(), user.getUsername(), user.getPassword() }); } public void update(User user) { getJdbcTemplate().update("update userinfo set username=?,password=? where id=?", new Object[] { user.getUsername(), user.getPassword(), user.getId() }); } public void delete(int id) { getJdbcTemplate().update("delete from userinfo where id=?", new Object[] { id }); } }
在src/main/resources/目录下创建一个spring-jdbc.xml文件用于配置数据库的相关信息,这些信息将会保存到一个dataSource Bean中,这个dataSource又作为内部的属性保存在了jdbcTemplateUserDao Bean中,这个Bean是一个UserDao对应一个UserDao类型。
<?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-3.0.xsd"> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/user_db" /> <property name="username" value="root" /> <property name="password" value="password" /> </bean> <bean id="jdbcTemplateUserDao" class="com.myspring.dao.UserDao"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
最后创建一个测试类SpringJdbcPractice类用于测试:
package com.myspring.study; import java.util.List; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.myspring.dao.UserDao; import com.myspring.obj.User; public class SpringJdbcPractice { @SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-jdbc.xml"); UserDao userDao = (UserDao) context.getBean("jdbcTemplateUserDao"); User user = new User(); for (int i = 1; i <= 3; i ++) { user.setId(i); user.setUsername("user" + i); user.setPassword("passwd" + i); userDao.create(user); } System.out.println("begin to query all ..."); List<User> users = userDao.queryAll(); for (User u : users) System.out.println(u); System.out.println("begin to query user with name 'user2' ..."); users = userDao.queryByUsername("user2"); for (User u : users) System.out.println(u); System.out.println("begin to update 'user2' ..."); user.setId(2); user.setUsername("user2"); user.setPassword("moonlit"); userDao.update(user); System.out.println("get user2 after update ..."); users = userDao.queryByUsername("user2"); for (User u : users) System.out.println(u); System.out.println("delete 'user3' ..."); userDao.delete(3); System.out.println("query all after delete ..."); users = userDao.queryAll(); for (User u : users) System.out.println(u); } }
输出效果如下:
begin to query all ... [1,user1,passwd1] [2,user2,passwd2] [3,user3,passwd3] begin to query user with name 'user2' ... [2,user2,passwd2] begin to update 'user2' ... get user2 after update ... [2,user2,moonlit] delete 'user3' ... query all after delete ... [1,user1,passwd1] [2,user2,moonlit]