spring 07 对jdbc的支持

需要新增的包
mysql-connector-java.jar(数据库连接包)
c3p0.jar(数据库连接池包)
spring-jdbc.jar(jdbc的支持)

beam.xml




    
    

    
        
        
        
        
    
    
    
    
        
    
    
    
        
     


db.properties

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///spring_db
user=root
password=221121
initialPoolSize=3
maxPoolSize=6
acquireIncrement=2

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    private int id;
    private String name;
    private String sex;
}
public interface UserDao {
    
    public void save(User user);
    public void update(User user);
    public void delete(Serializable id);//Serializable可能只是想用多态吧
    public User findById(Serializable id);
    public List getAll();
}
@Data
public class UserDaoImpl implements UserDao {
    
    // 接收容器注入的JdbcTemplate对象
    private JdbcTemplate jdbcTemplate;
    

    // 1. 原始jdbc代码
    public void save(User user) {
        jdbcTemplate.update("insert into user_t(name,sex) values(?,?)", user.getName(),user.getSex());
    }


    @Override
    public void delete(Serializable id) {
        jdbcTemplate.update("delete from user_t where id=?", id);
    }
    
    @Override
    public void update(User user) {
        jdbcTemplate.update("update user_t set name=? where id=?", user.getName(),user.getId());
    }


    @Override
    public User findById(Serializable id) {
        // queryForList 把每一行都封装为map对象,再添加到list中
//      List> list = jdbcTemplate.queryForList("select * from t_User");
        
        // 传入类型参数,表示查询的列的类型;  这里只能查询一列
//      List list = jdbcTemplate.queryForList("select UserName from t_User", String.class);

        List list = jdbcTemplate.query("select * from user_t where id=?",  new MyRowMapper(), id);
        
        return (list!=null&&list.size()>0)?list.get(0):null;
    }


    @Override
    public List getAll() {
        List list = jdbcTemplate.query("select * from user_t", new MyRowMapper());
        return list;
    }
    
    
    
    // 封装Springjdbc查询的结果集
    class MyRowMapper implements RowMapper{
        
        // 如何解析一行
        @Override
        public User mapRow(ResultSet rs, int rowNum) throws SQLException {
            User user = new User();
            user.setId(rs.getInt("id"));
            user.setName(rs.getString("name"));
            return user;
        }
        
    }
    
}
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {

    private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",App.class);
    
    @Test
    public void testApp() throws Exception {
        UserDao userDao = (UserDao) ac.getBean("userDaoImpl");
        
//      User user = new User();
//      user.setName("丁昌江");
//      user.setSex("男");
//      userDao.save(user);
        
//      userDao.delete(1);
        
//      User user = new User();
//      user.setId(2);
//      user.setName("丁丁");
//      userDao.update(user);
        
        // 查询方法
//      User user = userDao.findById(2);
//      System.out.println(user);
        
        System.out.println(userDao.getAll());
    }
}

你可能感兴趣的:(spring 07 对jdbc的支持)