首页 > 代码库 > 使用Jdbc Template的基本操作步骤

使用Jdbc Template的基本操作步骤

 1.appliactionContext.xml配置

<?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http://www.springframework.org/schema/beans"         xmlns:p="http://www.springframework.org/schema/p"         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">            <!--数据源的配置 -->      <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">         <property name="driverClassName" value="http://www.mamicode.com/com.mysql.jdbc.Driver"></property>         <property name="url" value="http://www.mamicode.com/jdbc:mysql:///spring"></property>         <property name="username" value="http://www.mamicode.com/root"></property>         <property name="password" value=""></property>     </bean>                  <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">         <property name="dataSource" ref="dataSource"></property>     </bean>               <bean id="userDao" class="cn.happy.impl.UserDAOImpl">        <property name="jdbcTemplate" ref="jdbcTemplate"></property>     </bean>  </beans>

  2.接口:IUserDAO.java

 public interface IUserDAO {        public void addUser(User user);      public void deleteUser(int id);      public void updateUser(User user);      public String searchUserName(int id);          public User searchUser(int id);          public List<User> findAll();  }

3.接口实现类:UserDAOImpl.java

Spring提供了JdbcDaoSupport支持类,所有DAO继承这个类,就会自动获得JdbcTemplate(前提是注入DataSource)。

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">         <property name="dataSource" ref="dataSource"></property>     </bean>               <bean id="userDao" class="cn.happy.impl.UserDAOImpl">         <property name="jdbcTemplate" ref="jdbcTemplate"></property>     </bean>

JdbcTemplate主要提供下列方法:

  1、execute方法:可以用于执行任何SQL语句,一般用于执行DDL语句;

  2、update方法及batchUpdate方法:update方法用于执行新增、修改、删除等语句;batchUpdate方法用于执行批处理相关语句;

  3、query方法及queryForXXX方法:用于执行查询相关语句;

  4、call方法:用于执行存储过程、函数相关语句。

public class UserDAOImpl extends JdbcDaoSupport implements IUserDAO {      public void addUser(User user) {         String sql = "insert into user values(?,?,?)";         this.getJdbcTemplate().update(sql, user.getId(), user.getUsername(),                 user.getPassword());     }      public void deleteUser(int id) {         String sql = "delete from user where id=?";         this.getJdbcTemplate().update(sql, id);      }      public void updateUser(User user) {         String sql = "update user set username=?,password=? where id=?";         this.getJdbcTemplate().update(sql, user.getUsername(),                 user.getPassword(), user.getId());     }      public String searchUserName(int id) {// 简单查询,按照ID查询,返回字符串         String sql = "select username from user where id=?";         // 返回类型为String(String.class)         return this.getJdbcTemplate().queryForObject(sql, String.class, id);      }      public List<User> findAll() {// 复杂查询返回List集合         String sql = "select * from user";         return this.getJdbcTemplate().query(sql, new UserRowMapper());      }      public User searchUser(int id) {         String sql="select * from user where id=?";         return this.getJdbcTemplate().queryForObject(sql, new UserRowMapper(), id);     }      class UserRowMapper implements RowMapper<User> {      //rs为返回结果集,以每行为单位封装着         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;         }     }  }

4.测试类:UserTest.java

public class UserTest {          @Test//     public void demo1(){         User user=new User();         user.setId(3);         user.setUsername("admin");         user.setPassword("123456");                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         dao.addUser(user);              }          @Test//     public void demo2(){         User user=new User();         user.setId(1);         user.setUsername("admin");        user.setPassword("admin");                  ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         dao.updateUser(user);     }          @Test//     public void demo3(){         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         dao.deleteUser(3);     }          @Test//查(简单查询,返回字符串)     public void demo4(){         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         String name=dao.searchUserName(1);         System.out.println(name);     }          @Test//查(简单查询,返回对象)     public void demo5(){         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         User user=dao.searchUser(1);         System.out.println(user.getUsername());     }          @Test//查(复杂查询,返回对象集合)     public void demo6(){         ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");         IUserDAO dao=(IUserDAO) applicationContext.getBean("userDao");         List<User> users=dao.findAll();         System.out.println(users.size());     }            }

 

使用Jdbc Template的基本操作步骤