Spring整合jdbc

注意:

在spring容器中可以给对象设置属性,如果有父类的话,也可以给他的父类设置属性

表:

create table userinfo(

    uid int primary key auto_increment,

    username varchar(20),

    password varchar(20)

)

JdbcDaoSupport :工具类是spring提供的整合jdbc的工具类

1.将配置的数据源加入当前操作

2.提供标准的jdbc模板JdbcTemplate --->jdbc操作数据库的方法

DaoImpl:

public class UserDaoImpl extends JdbcDaoSupport implements UserDao {

    @Override

    public void add(UserBean u) {

        JdbcTemplate template=getJdbcTemplate();

        String sql="insert userinfo values(null,?,?)";

        template.update(sql, u.getUsername(), u.getPassword());

    }

 

    @Override

    public void del(int uid) {

        String sql="delete from userinfo where uid=?";

 

        getJdbcTemplate().update(sql,uid);

    }

 

    @Override

    public void update(UserBean u) {

        String sql="update userinfo set username=? , password=? where uid=?";

 

        getJdbcTemplate().update(sql,u.getUsername(),u.getPassword(),u.getUid());

    }

 

    @Override

    public List<UserBean> getAll() {

        String sql="select*from userinfo";

        List<UserBean> list = getJdbcTemplate().query(sql, new RowMapper<UserBean>() {

            @Override

            public UserBean mapRow(ResultSet resultSet, int i) throws SQLException {

                UserBean u = new UserBean();

                u.setUid(resultSet.getInt(1));

                u.setUsername(resultSet.getString("username"));

                u.setPassword(resultSet.getString("password"));

                return u;

            }

        });

        return list;

    }

}

 

<bean name="useDao" class="com.ujiuye.dao.UserDaoImpl">

        <property name="dataSource" ref="ds"/>

    </bean>

    <bean name="ds" class="com.mchange.v2.c3p0.ComboPooledDataSource">

        <property name="driverClass" value="com.mysql.jdbc.Driver"/>

        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"/>

        <property name="user" value="root"/>

        <property name="password" value="123456"/>

    </bean> 

    <bean name="userService" class="com.ujiuye.service.UserServiceImpl">

        <property name="dao" ref="useDao"/>

    </bean>

 

Service:

public class UserServiceImpl  implements  UserService {

    UserDao dao;

 

    public UserDao getDao() {

        return dao;

    }

 

    public void setDao(UserDao dao) {

        this.dao = dao;

    }

 

    @Override

    public void add(UserBean userBean) {

        dao.add(userBean);

    }

 

    @Override

    public void del(int uid) {

        dao.del(uid);

    }

 

    @Override

    public void update(UserBean userBean) {

        dao.update(userBean);

    }

 

    @Override

    public List<UserBean> getAll() {

        return dao.getAll();

    }

}

 

测试:

/**

     * 增删改查

     */

    @Test

    public void test3(){

        ApplicationContext app=new ClassPathXmlApplicationContext("bean.xml");

 

        UserService service= (UserService) app.getBean("userService");

 

       // service.add(new UserBean(0,"张三2","1223"));

 

//        service.del(2);

 

        service.update(new UserBean(3,"李四","110"));

 

        List<UserBean> list = service.getAll();

        System.out.println(list);

    }

Spring整合jdbc

上一篇:git提交身份验证失败: Incorrect username or password (access token)


下一篇:在Java中调用C/C++本地库