当前位置:Gxlcms > 数据库问题 > SQL语言基础

SQL语言基础

时间:2021-07-01 10:21:17 帮助过:24人阅读

依赖注入(dependency injection):是Spring框架核心ioc(inversion of control)的具体实现

注入bean

通过set的方式

package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="com.example.demo.dao.UserDao" />

<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
    <property name="userDao" ref="userDao"/>
</bean>
    
</beans>

备注:

  1. name="userDao"中的userDao是根据setUserDao的UserDao(首字母变小写)来的

通过p命名空间的方式

p命名空间注入的本质就是set注入

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

<bean id="userDao" class="com.example.demo.dao.UserDao" />

<bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
 
</beans>

备注:

  1. userDao-ref中的userDao是根据setUserDao的UserDao(首字母变小写)来的

通过构造器的方式

package com.example.demo.service.impl;

import com.example.demo.dao.UserDao;
import com.example.demo.service.UserService;

public class UserServiceImpl implements UserService {

    private UserDao userDao;

    public UserServiceImpl(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    public void save() {
        userDao.save();
    }
}
<?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 http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="userDao" class="com.example.demo.dao.UserDao" />

    <bean id="userServiceImpl" class="com.example.demo.service.impl.UserServiceImpl">
        <constructor-arg name="userDao" ref="userDao"/>
    </bean>

</beans>

备注:

  1. name="userDao"中的userDao是根据public UserServiceImpl(UserDao userDao)(构造器参数名)来的

注入普通数据类型,集合等

SQL语言基础

标签:efi   不同的   修改   不同   tran   查询   工作   创建数据库   grant   

人气教程排行