• Spring对jdbc支持


    使用步骤:

             1)引入jar文件

        spring-jdbc-3.2.5.RELEASE.jar

        spring-tx-3.2.5.RELEASE.jar

             2) 优化


    package loaderman.h_jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    import javax.sql.DataSource;
    
    public class UserDao02 {
    
        // IOC容器注入
        private DataSource dataSource;
        public void setDataSource(DataSource dataSource) {
            this.dataSource = dataSource;
        }
    
    
        public void save() {
            try {
                String sql = "insert into t_dept(deptName) values('test');";
                Connection con = null;
                Statement stmt = null;
                // 连接对象
                con = dataSource.getConnection();
                // 执行命令对象
                stmt =  con.createStatement();
                // 执行
                stmt.execute(sql);
    
                // 关闭
                stmt.close();
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package loaderman.h_jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.Statement;
    
    public class UserDao01 {
    
    
        /*
         *  保存方法
         *  需求优化的地方:
         *      1. 连接管理
         *      2. jdbc操作重复代码封装
         */
        public void save() {
            try {
                String sql = "insert into t_dept(deptName) values('test');";
                Connection con = null;
                Statement stmt = null;
                Class.forName("com.mysql.jdbc.Driver");
                // 连接对象
                con = DriverManager.getConnection("jdbc:mysql:///hib_demo", "root", "root");
                // 执行命令对象
                stmt =  con.createStatement();
                // 执行
                stmt.execute(sql);
    
                // 关闭
                stmt.close();
                con.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    package loaderman.h_jdbc;
    
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.RowMapper;
    
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.util.List;
    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;
            }
    
        }
    }
    package loaderman.h_jdbc;
    
    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;
        }
        
    }
    package loaderman.h_jdbc;
    
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    
    public class App {
    
        // 容器对象
        ApplicationContext ac = new ClassPathXmlApplicationContext("loaderman/h_jdbc/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());
        }
    }
    <?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">
        
        <!-- 1. 数据源对象: C3P0连接池 -->
        <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>
        
        <!-- 2. 创建JdbcTemplate对象 -->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="dataSource"></property>
        </bean>
        
        <!-- dao 实例 -->
        <bean id="userDao" class="loaderman.h_jdbc.UserDao">
            <property name="jdbcTemplate" ref="jdbcTemplate"></property>
        </bean>
    </beans>      
      
  • 相关阅读:
    【转并修改】VS2013 MVC Web项目使用内置的IISExpress支持局域网内部机器(手机、PC)访问、调试
    ASP.NET MVC4 UEditor 的上传图片配置路径
    转:Java图形化界面设计——布局管理器之FlowLayout(流式布局)其他请参考转载出处网址
    转载:win7JDK环境配置
    转载:java保留2位小数
    转载:遍历Map的四种方法
    架构模式: 远程过程调用
    架构模式: 外部配置化
    架构模式: 微服务的基底
    架构模式: 服务部署平台
  • 原文地址:https://www.cnblogs.com/loaderman/p/10042895.html
Copyright © 2020-2023  润新知