美文网首页
Springjdbc

Springjdbc

作者: _FireFly_ | 来源:发表于2020-11-27 20:45 被阅读0次

    Student

    public class Student {
    
        private Integer sid;
        private String sname;
        private String ssex;
        private Integer sage;
    
        public Student() {
        }
    
        public Student(Integer sid, String sname, String ssex, Integer sage) {
            this.sid = sid;
            this.sname = sname;
            this.ssex = ssex;
            this.sage = sage;
        }
    
        @Override
        public String toString() {
            return "Student{" +
                    "sid=" + sid +
                    ", sname='" + sname + '\'' +
                    ", ssex='" + ssex + '\'' +
                    ", sage=" + sage +
                    '}';
        }
    
        public Integer getSid() {
            return sid;
        }
    
        public void setSid(Integer sid) {
            this.sid = sid;
        }
    
        public String getSname() {
            return sname;
        }
    
        public void setSname(String sname) {
            this.sname = sname;
        }
    
        public String getSsex() {
            return ssex;
        }
    
        public void setSsex(String ssex) {
            this.ssex = ssex;
        }
    
        public Integer getSage() {
            return sage;
        }
    
        public void setSage(Integer sage) {
            this.sage = sage;
        }
    }
    

    ApplicationContext.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"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--0.创建一个数据源对象 原生DriverManager 获取一个连接-->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <!--数据库驱动类-->
            <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
            <!--连接数据库的URL-->
            <property name="url" value="jdbc:mysql://localhost:3306/newtest?useSSL=false&amp;characterEncoding=UTF-8"></property>
            <!--连接数据库的账号名-->
            <property name="username" value="root"></property>
            <!--连接数据库的密码-->
            <property name="password" value="123456"></property>
        </bean>
    
        <!--1.想要使用SpringJDBC提供的操作 需要配置一个JDBC模板 JdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <!--需要给当前这个template对象配置一个数据源-->
            <property name="dataSource" ref="dataSource"></property>
        </bean>
    </beans>
    

    TestMain

    public class TestMain {
    
        public static void main(String[] args) {
            BeanFactory factory = new ClassPathXmlApplicationContext("ApplicationContext.xml");
            //通过Spring来操作数据库
            //  JDBC原生技术
            //  有点像我们自己封装的那个ORM框架
            //  一种方案是策略模式(SpringJDBC)   JdbcTemplate
            //  一种方案是反射方式(MyBatis)      SqlSession
            //  增删改 容易
            //  查询   处理结果
    
            //1.跟Spring的bean工厂要一个对象  JdbcTemplate
            JdbcTemplate template = (JdbcTemplate)factory.getBean("jdbcTemplate");
            //2.让template对象执行数据库操作
            //  增
    //        String sql = "insert into student values(?,?,?,?)";
    //        int count = template.update(sql,9,"小拓","男",17);
    //        System.out.println(count);
    
            //删
    //        String sql = "delete from student where sid = ?";
    //        int count = template.update(sql,8);
    //        System.out.println(count);
    
            //改
    //        String sql = "update student set sname = ? where sid = ?";
    //        int count = template.update(sql,"小七",7);
    //        System.out.println(count);
    
    
            //查询
            // 一个列(变量存储)
    //        String sql = "select sname from student where sid = ?";
    //        Student sage = template.queryForObject(sql,Student.class,1);//只能查询一个列
    //        System.out.println(sage);
    
            //查询
            //  一行数据(domain对象里)  数据原有表格不存在 计算出来的(平均值 最大值 map集合)
    //        String sql = "select * from student where sid = ?";
    //        Student student = template.queryForObject(sql,new RowMapper<Student>(){
    //            public Student mapRow(ResultSet resultSet, int i) throws SQLException {
    //                Student student = new Student();
    //                student.setSid(resultSet.getInt("sid"));
    //                student.setSname(resultSet.getString("sname"));
    //                student.setSsex(resultSet.getString("ssex"));
    //                student.setSage(resultSet.getInt("sage"));
    //                return student;
    //            }
    //        },1);
    //        System.out.println(student);
    
    
            //查询多行记录
    //        String sql = "select * from student";
    //        List list = template.queryForList(sql);//   List<Map<String,Object>>
    //        System.out.println(list);
    
            String sql = "select * from student";
            List<Student> list = template.query(sql,new RowMapper<Student>() {
                public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                    Student student = new Student();
                    student.setSid(resultSet.getInt("sid"));
                    student.setSname(resultSet.getString("sname"));
                    student.setSsex(resultSet.getString("ssex"));
                    student.setSage(resultSet.getInt("sage"));
                    return student;
                }
            });
            for(Student student : list){
                System.out.println(student);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:Springjdbc

          本文链接:https://www.haomeiwen.com/subject/mxwciktx.html