• spring jdbc支持demo


    public class dept {
    private int deptid;
    private String deptname;
    public int getDeptid() {
        return deptid;
    }
    public void setDeptid(int deptid) {
        this.deptid = deptid;
    }
    public String getDeptname() {
        return deptname;
    }
    public void setDeptname(String deptname) {
        this.deptname = deptname;
    }
    
    }

    bean.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:p="http://www.springframework.org/schema/p"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
        
    <bean id="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
    <property name="jdbcUrl" value="jdbc:mysql:///hib_demo"></property>
    <property name="user"  value="root"></property>
    <property name="password" value="root"></property>
    <property name="initialPoolSize" value="3"></property>
    <property name="maxPoolSize" value="10"></property>
    <property name="maxStatements" value="100"></property>
    <property name="acquireIncrement" value="2"></property>
    
    </bean>
        <bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <constructor-arg ref="datasource"> </constructor-arg>
        </bean>
        <bean id="userdao" class="cn.itcast.hjdbc.userdao">
        <property name="jdbctemplate" ref="jdbctemplate"></property>
        </bean>
    </beans>   
     <constructor-arg   如果写为  <property name="dataSource" ref="dataSource"></property>  提示找不到datasource


    userdao.java
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    import java.util.Map;
    
    import javax.sql.DataSource;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowCallbackHandler;
    import org.springframework.jdbc.core.RowMapper;
    
    public class UserDao {
        
        // IOC容器注入
    //    private DataSource dataSource;
    //    public void setDataSource(DataSource dataSource) {
    //        this.dataSource = dataSource;
    //    }
        
        private JdbcTemplate jdbcTemplate;
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        
        public void save() {
            String sql = "insert into t_dept(deptName) values('test');";
            jdbcTemplate.update(sql);
        }
        
        public Dept findById(int id) {
            String sql = "select * from t_dept where deptId=?";
            List<Dept> list = jdbcTemplate.query(sql,new MyResult(), id);
            return (list!=null && list.size()>0) ? list.get(0) : null;
        }
        
        public List<Dept> getAll() {
            String sql = "select * from t_dept";
            List<Dept> list = jdbcTemplate.query(sql, new MyResult());
            return list;
        }
        
        
        
        
        class MyResult implements RowMapper<Dept>{
    
            // 如何封装一行记录
            @Override
            public Dept mapRow(ResultSet rs, int index) throws SQLException {
                Dept dept = new Dept();
                dept.setDeptId(rs.getInt("deptId"));
                dept.setDeptName(rs.getString("deptName"));
                return dept;
            }
            
        }
    }
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class app {
        ApplicationContext ac = new ClassPathXmlApplicationContext("cn/itcast/hjdbc/bean.xml");
        @Test
        public void testApp() throws Exception {
            userdao ud = (userdao) ac.getBean("userdao");
    //        ud.save();
            System.out.println(ud.findbyid(9));
            System.out.println(ud.getAll());
        }
        
    }




  • 相关阅读:
    path.join()和path.resolve()
    __dirname和__filename
    使用css-loader
    博客主题
    Python使用pandas库读取txt文件中的Json数据,并导出到csv文件
    为什么一个星期工作量的工作,我做了一个多月,还没结束 (基于socket的分布式数据处理程序Java版)
    Docker 命令
    Python使用pandas库读取csv文件,并分组统计的一个例子
    Linux 进程守护脚本
    Linux 安装 JDK
  • 原文地址:https://www.cnblogs.com/yimian/p/7919476.html
Copyright © 2020-2023  润新知