简单Spring整合JdbcTemplate

实体类:

public class User implements Serializable{
    private Integer id;
    private String username;
    private String password;
    public Integer getId() {
        return id;
    }
    public String getUsername() {
        return username;
    }
    public String getPassword() {
        return password;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    @Override
    public String toString() {
        return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
    }
}

UserDao接口

public interface UserDao {
    /**
     * 增加用户
     */
    Integer addUser(User user);
    
    /**
     * 修改用户信息
     */
    Integer updateUser(User user);
    
    /**
     * 查询用户
     */
    List<User> findAll();
}

UserDaoImpl实现类

JdbcTemplate提供一种更简单的方式“update(String sql, Object... args)”来实现设值!

RowMapper接口提供mapRow(ResultSet rs, int rowNum)方法来获取数据库得到的结果集

@Repository(value="userDaoImpl")
public class UserDaoImpl implements UserDao{
    /**
     * 添加用户
     */
    @Resource(name="jt")
    private JdbcTemplate jt;
    @Override
    public Integer addUser(User user) {
        //获取连接数据
        Object obj[] = {user.getUsername()};
        return jt.update("insert into user values(null,?)",obj);
    }

    @Override
    public Integer updateUser(User user) {
        Object obj[] = {user.getUsername()};
        return jt.update("update user set username=?",obj);
    }

    @Override
    public List<User> findAll() {
        return jt.query("select * from user",new RowMapper<User>() {
            @Override
            public User mapRow(ResultSet rs, int arg1) throws SQLException {
                User user = new User();
                user.setUsername(rs.getString("username"));
                user.setPassword(rs.getString("password"));
                return user;
            }
        });
    }

}

测试类:

@Test
    public void Test2() {
        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        //ComboPooledDataSource spd = (ComboPooledDataSource) ac.getBean("cpd");
        
        UserDao ud = (UserDaoImpl) ac.getBean("userDaoImpl");
        List<User> list = ud.findAll();
        for (User user : list) {
            System.out.println(user);
        }
    }

applicationContext.xml配置文件:

<!-- 使用注解实现 -->
  <!--创建c3p0对象--> <bean id="cpd" class=" com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"></property> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/myuser"></property> <property name="user" value="root"></property> <property name="password" value="123456"></property> </bean>
  <!--创建jdbcTemplate对象--> <bean id="jt" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="DataSource" ref="cpd"></property> </bean>
  <!--开启注解扫描--> <context:component-scan base-package="com.spring"></context:component-scan>

 

posted @ 2018-11-27 17:19  DreamLM  阅读(421)  评论(0编辑  收藏  举报