JavaEE 之 DBCP

來源:互聯網
上載者:User

標籤:管理   多表   users   void   email   get   obj   資料庫   pre   

1.DBCP

  a.定義:DBCP(DataBase Connection Pool)資料庫連接池,是java資料庫連接池的一種,由Apache開發,通過資料庫連接池,可以讓程式自動管理資料庫串連的釋放和斷開

  b.配置:

  <bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">        <property name="driverClassName" value="com.mysql.jdbc.Driver" />        <property name="url" value="jdbc:mysql://localhost:3306/emp" />        <property name="username" value="root" />        <property name="password"  value="admin"/>        <property name="maxActive" value="10"></property>        <property name="initialSize" value="2"></property>        <property name="minIdle" value="2"></property>        <property name="maxIdle" value="3"></property>    </bean>    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">        <property name="dataSource" ref="dataSource" />    </bean>

 

  c.使用:

@Repositorypublic class UserDao  {    @Resource    private JdbcTemplate template;        //增    public void addUser(String name,String pwd){        int userType=1;        String sql="insert into users(user_name,user_pwd,user_type) values(?,?,?)";        jdbcTemplate.update(sql,new Object[]{name,pwd,userType});    }        //刪    public void delUserById(int id){        String sql="delete from users where user_id=?";        jdbcTemplate.update(sql,new Object[]{id});    }        //改    public void updateUser(User user){        String sql="update users set user_name=?,user_pwd=?,user_type=? where user_id=?";        jdbcTemplate.update(sql,new Object[]{user.getUserName(),user.getUserPwd(),user.getUserType(),user.getUserId()});    }        //查——方法一            public List<User> findAllUser(){      String sql="select * from users";      List<User> list=jdbcTemplate.query(sql,new BeanPropertyRowMapper(User.class));      return list;    }            //查——方法二    public List<User> getAllUser(){        String sql="select * from users";        List<User> list=jdbcTemplate.query(sql,new RowMapper(){                    @Override            public Object mapRow(ResultSet arg0, int arg1) throws SQLException {                User u=new User();                if(arg0!=null){                u.setUserId(arg0.getInt("user_id"));                u.setUserName(arg0.getString("user_name"));                u.setUserPwd(arg0.getString("user_pwd"));                u.setUserId(arg0.getInt("user_type"));                }                return u;            }                    });        return list;    }    //多表聯查    public List<User> getAllUserAndInfo(){        String sql="select u.user_name,u.user_type,i.info_nickName,i.info_email from users u left join userInfo i on u.user_id=i.user_id ";        List<User> list=jdbcTemplate.query(sql,new RowMapper(){                    @Override            public Object mapRow(ResultSet arg0, int arg1) throws SQLException {                User u=new User();                if(arg0!=null){                u.setUserName(arg0.getString("user_name"));                u.setUserType(arg0.getInt("user_type"));                UserInfo info=new UserInfo();                info.setInfoEmail(arg0.getString("info_email"));                info.setInfoNickName(arg0.getString("info_nickName"));                u.setInfo(info);                }                return u;            }                    });        return list;    }}

 

JavaEE 之 DBCP

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.