MyBatis-Spring integration of the way 3

By integrating Notes

1. New Interface UserMapper UserDao in the same directory, as follows:

public interface UserMapper {
    @Select("select * from user")
    public List<User> selectUser();
}

2. Create a new service package, and its implementation class rewrite UserDao UserService class and its implementation as a class, as follows:

public interface UserService {
    public List<User> selectUser();
}



public class UserServiceImp implements UserService {
    private UserMapper userMapper;
    @Override
    public List<User> selectUser() {
        return userMapper.selectUser();
    }

    public void setUserMapper(UserMapper userMapper) {
        this.userMapper = userMapper;
    }
}

3.beans.xml new bean labels, modify the original UserDao label, as follows:

<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
    <property name="mapperInterface" value="com.lxy.dao.UserMapper"/>
    <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
<bean id="userService" class="com.lxy.service.imp.UserServiceImp">
    <property name="userMapper" ref="userMapper"/>
</bean>

4. Modify the new type of test, as follows:

public class test {
    public static void main(String[] args) throws IOException {
        ApplicationContext context=new ClassPathXmlApplicationContext("Beans.xml");
        UserService userService= (UserService) context.getBean("userService");
        System.out.println(userService.selectUser().size());
    }
}

Guess you like

Origin www.cnblogs.com/inkqx/p/12316360.html