Spring5--p命名空间和c命名空间注入

<?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:c="http://www.springframework.org/schema/c"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--可以使用p命名空间和c命名空间进行注入-->

<!--    xmlns:p="http://www.springframework.org/schema/p"-->
<!--    xmlns:c="http://www.springframework.org/schema/c"-->

    <!--p命名空间注入,可以直接注入属性的值:property-->
<!--    <bean id="user" class="com.wang.pojo.User" p:name="小明" p:age="25"/>-->

    <!--c命名空间注入,通过构造器注入:construct-args-->
    <bean id="user2" class="com.wang.pojo.User" c:age="18" c:name="小明"/>

</beans>

<!--Bean的作用域  singleton:单例模式(Spring默认机制)-->
<!--    <bean id="user2" class="com.wang.pojo.User" c:age="18" c:name="小明" scope="singleton"/>-->

    <!--Bean的作用域  prototype:原型模式   每次从容器中get的时候,都会产生一个新对象-->
    <bean id="user2" class="com.wang.pojo.User" c:age="18" c:name="小明" scope="prototype"/>

其余的request、session、application这些只能在web开发中使用到。

ApplicationContext context = new ClassPathXmlApplicationContext("userbeans.xml");
User user = context.getBean("user2", User.class);
User user2 = context.getBean("user2", User.class);
System.out.println(user);
System.out.println(user.hashCode());
System.out.println(user2.hashCode());
System.out.println(user==user2);  // True   singleton
System.out.println(user==user2);  // false   prototype
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring和MyBatis是两个非常流行的Java开发框架,它们可以很好地结合使用。 首先,你需要在你的项目中引入Spring和MyBatis的相关依赖。 对于Spring,你可以使用Maven或者Gradle来管理依赖。在你的项目的pom.xml(或者build.gradle)文件中,添加以下依赖: ```xml <!-- Spring核心依赖 --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.9</version> </dependency> <!-- Spring和MyBatis的集成依赖 --> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.7</version> </dependency> ``` 接下来,你需要配置Spring和MyBatis的相关配置文件。 首先是Spring的配置文件(比如applicationContext.xml),你可以在其中配置Spring的上下文和其他相关的Bean。 ```xml <!-- 配置Spring的上下文 --> <context:annotation-config/> <context:component-scan base-package="com.example"/> <!-- 配置数据源 --> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mydatabase"/> <property name="username" value="root"/> <property name="password" value="password"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置MyBatis的SqlSessionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> </bean> <!-- 扫描MyBatis的Mapper接口 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 在上面的配置中,你需要根据你的数据库设置正确的数据源和连接信息。 接下来是MyBatis的配置文件(比如mybatis-config.xml),你可以在其中配置MyBatis的相关设置。 ```xml <configuration> <settings> <!-- 开启驼峰命名转换 --> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration> ``` 在上面的配置中,我们开启了MyBatis的驼峰命名转换,这样可以方便地将数据库中的下划线命名转换为Java中的驼峰命名。 最后,你需要创建Mapper接口和对应的Mapper XML文件来定义SQL语句和映射关系。 这样,你就完成了Spring和MyBatis的配置。在你的代码中,你可以使用Spring的依赖注入来获取MyBatis的Mapper接口,并使用它来进行数据库操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值