美文网首页
4.2 基于XML配置方式 / 整合三层架构组件

4.2 基于XML配置方式 / 整合三层架构组件

作者: 大也 | 来源:发表于2023-11-30 10:48 被阅读0次
    image.png

    三层架构 概念
    界面层(User Interface layer) viewcontroller 层
    业务逻辑层(Business Logic Layer)service
    数据访问层(Data Access layer) dao 数据库取数据

    • model 层 Student
    public class StudentDaoImpl implements StudentDao {
    
        private JdbcTemplate jdbcTemplate;
    
        //注入我们的jdbctemplate对象
        public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
            this.jdbcTemplate = jdbcTemplate;
        }
    
        /**
         * 数据库查询全部学生数据
         * */
        @Override
        public List<Student> queryAll() {
            //jdbctemplate 进行数据库查询 ioc容器进行装配 不要自己去实例化
            String sql = "select id,name,gender,age,class as classes from students ";
            List<Student> students = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Student.class));
            System.out.println("studentDao"+students);
            return students;
        }
    }
    
    <?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:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           https://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:property-placeholder location="classpath:jdbc.properties"/>
    
        <!--druid-->
        <bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
            <property name="url" value="${atguigu.url}"/>
            <property name="driverClassName" value="${atguigu.driver}"/>
            <property name="username" value="${atguigu.username}"/>
            <property name="password" value="${atguigu.password}"/>
        </bean>
    
        <!--jdbcTemplate-->
        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
            <property name="dataSource" ref="druidDataSource"/>
        </bean>
    
        <!--dao 配置 di jdbcTemplate-->
        <bean id="studentDao" class="com.atguigu.dao.impl.StudentDaoImpl" >
            <property name="jdbcTemplate" ref="jdbcTemplate"/>
        </bean>
    
        <!--service 配置 di dao-->
        <bean id="studentService" class="com.atguigu.service.impl.StudentServiceImpl">
            <property name="studentDao" ref="studentDao"/>
        </bean>
    
        <!--controller 配置 di service-->
        <bean id="studentController" class="com.atguigu.controller.StudentController">
            <property name="studentService" ref="studentService"/>
        </bean>
    </beans>
    
    public class JdbcTemplateTest {
        public void testForJava(){
            /****
             * 1.实例化对象即可
             * 2.调用方法即可 DDL DML DCL
             * JdbcTemplate 简化数据库的crud 不提供连接池
             * DruiDataSource 负责连接的创建和数据库的创建等等
             */
    
            //创建一个连接池对象
            DruidDataSource druidDataSource = new DruidDataSource();
            druidDataSource.setUrl("jdbc:mysql:///studb");
            druidDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
            druidDataSource.setUsername("root");
            druidDataSource.setPassword("a123456");
    
    
            JdbcTemplate jdbcTemplate = new JdbcTemplate();
            jdbcTemplate .setDataSource(druidDataSource);
    
            //调用的方法
    //        jdbcTemplate.update()             //非查询语句
    //        jdbcTemplate.queryForObject();    // 查询单个对象
    //        jdbcTemplate.query();             //查询集合
        }
    
        /**
         * 通过ioc容器读取配置的jdbcTemplate 组件
         * 1.创建ioc容器
         * 2。获取jdbc组件
         * 3。进行数据库cur动作
         * */
        @Test
        public  void  testForIoc(){
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring_01.xml");
            System.out.println(classPathXmlApplicationContext);
            JdbcTemplate jdbcTemplate = classPathXmlApplicationContext.getBean(JdbcTemplate.class);
    
            String sql = "insert into students(name,gender,age,class)values(?,?,?,?)";
            int row = jdbcTemplate.update(sql,"xugougou","男","18","3年2班");
            System.out.println(row);
    
            String sql1 = "select * from students where id = ?";
            /**
             * sql 可以使用?
             * rowMapper 列名和属性名的映射器接口
             * object...param 可变参数占位符的值
             * 返回rowMapper 指定的对象
             * */
            Student student1 =  jdbcTemplate.queryForObject(sql1, new RowMapper<Student>() {
    
                @Override
                public Student mapRow(ResultSet resultSet, int i) throws SQLException {
                    //写结果集 i 行数
                    Student student = new Student();
                    student.setId(resultSet.getInt("id"));
                    student.setName(resultSet.getString("name"));
                    student.setAge(resultSet.getInt("age"));
                    student.setGender(resultSet.getString("gender"));
                    student.setClasses(resultSet.getString("class"));
    
                    return student;
                }
    
            }, 1);
    
            Student student2 =jdbcTemplate.queryForObject(sql1,  (resultSet,  i) -> {
                    //写结果集 i 行数
                    Student student = new Student();
                    student.setId(resultSet.getInt("id"));
                    student.setName(resultSet.getString("name"));
                    student.setAge(resultSet.getInt("age"));
                    student.setGender(resultSet.getString("gender"));
                    student.setClasses(resultSet.getString("class"));
    
                    return student;
                }, 1);
    
    //        //查询所有学生数据
            String sql2 = "select id,name,age,class as classes from students";
            List<Student> studentList = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<Student>(Student.class));
            //Mapped class was not specified  是BeanPropertyRowMapper的构造有问题 点进去发现 参数要传一个class类
    //        List<Student> studentList = jdbcTemplate.query(sql2, new BeanPropertyRowMapper<Student>());
            System.out.println(student1);
            System.out.println(student2);
            System.out.println(studentList);
        }
    
        /**
         * 从ioc容器中获取controller并且调用业务 内部都是ioc容器进行组装
         * */
        @Test
        public void testQueryAll(){
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring_02.xml");
            StudentController studentController = classPathXmlApplicationContext.getBean(StudentController.class);
            studentController.findAll();
    
        }
    }
    

    相关文章

      网友评论

          本文标题:4.2 基于XML配置方式 / 整合三层架构组件

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