Spring学习笔记(二)

发布时间:2022-06-26 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Spring学习笔记(二)脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

Spring模块的拆解 通过import标签进行引入。

complicationContext.xml

<import resource="applicationContext-product.xml"></import>
<import resource="applicationontext-user.xml"></import>

相关API

ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

ApplicationContext 是一个接口,它的实现类: ==1 == ClassPathXmlApplicationContext–配置文件路径下resource下可以直接写名称 2 FileSystemXmlApplicationContext–绝对路径 3.AnnotationConfigApplicationContext-- 使用于注解配置容器对象,它用来读取注解

getBean()方法的使用 1.id的方式(适用于多个同类型的对象,因为id是唯一的)

UserService userService = (UserService) app.getBean("userService");

例如:

<bean id="userDao" class="com.itheima.spring.UserDaoImpl" >
    <property name="age" value="18"></property>
    <property name="username" value="张三"></property>
</bean>

    <bean id="userDao2" class="com.itheima.spring.UserDaoImpl" >
        <property name="age" value="20"></property>
        <property name="username" value="李四"></property>
    </bean>
2.字节码类型(适用于一个类型的单个对象)
UserService userService = app.getBean(UserService.class);

Spring配置数据源

Spring学习笔记(二)

数据源开发的步骤

1.导入数据源的坐标(c3p0、druid)和数据库驱动坐标(mysql-connector-java)
2.创建数据源对象
3.设置数据源的基本连接数据(驱动、数据库地址、用户名、密码最基本的四个)
4.使用数据源获取资源和归还连接资源

查看端口和数据库位置cmd指令:

mysql -u root -p
show global variables like 'port';
show variables like 'datadir';

c3p0数据源

@Test
    public void test1() throws Exception {//c3p0数据源
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Drive");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

Druid数据源

@Test
    public void test2() throws Exception {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName("com.mysql.hdbc.Driver");
        dataSource.setUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUsername("root");
        dataSource.setPassword("root");
        DruidPooledConnection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

存在耦合,抽取配置文件jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root
@Test
    public void test3() throws Exception {//c3p0
        ResourceBundle rb=ResourceBundle.getBundle("jdbc");
        String driver = rb.getString("jdbc.driver");
        String url = rb.getString("jdbc.url");
        String username = rb.getString("jdbc.username");
        String password = rb.getString("jdbc.password");
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }

Spring配置数据源

1.在pom.xml中导入spring-context
2.编写userDao、userService
3.编写配置文件
4.测试Spring容器产生数据源对象
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property><!--name的值是set后面的-->
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="root"></property>
        </bean>
@Test
    public void test4() throws Exception {

        ApplicationContext app=new ClassPathXmlApplicationContext("application-Context.xml");
//        System.out.println(DataSource.class);
        DataSource dataSource = (DataSource) app.getBean("dataSource");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
    }
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>
@Test
        public void test5() throws Exception {
        ApplicationContext app=new ClassPathXmlApplicationContext("application-Context.xml");
        DataSource dataSource = (DataSource) app.getBean("dataSource2");
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
        }

Spring加载properties配置文件

1.设置命名空间
2.设置context:property-placeholder标签,连接properties文件
3.通过${键名}获取值
<?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 http://www.springframework.org/schema/context/spring-context.xsd">

    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>


    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"></property>
        <property name="jdbcUrl" value="${jdbc.url}"></property>
        <property name="user" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>

脚本宝典总结

以上是脚本宝典为你收集整理的Spring学习笔记(二)全部内容,希望文章能够帮你解决Spring学习笔记(二)所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:数据库